ASSIGNMENT 2
C PROGRAM THAT ANALYSES TRUSS
MEMBERS
MICHAEL JUNIOR JACOB: ENM213-01582023
ELIEZER KIRUBI: ENM213-0147-2023
BEAVON MANDERE: ENM213-0160-2023
NASHON KIMEU:ENM213-0160-2023
#include <stdio.h>
#include <stdlib.h> // For file handling
#include <math.h> // For mathematical operations
#include <string.h>
#define MAX_EQUATIONS 10 // Max number of equations
#define MAX_UNKNOWNS 7 // Max number of unknown forces
typedef struct {
char name[5]; // Node name
double x, y; // Coordinates of the node
double Fx, Fy; // External forces applied to this node
} Node;
typedef struct {
char name[7]; // Member name
int NodeA, NodeB; // Nodes connected by the member
double length; // Length of the member
double angle; // Angle of the member
} Member;
// Gaussian Elimination Function
void GaussianElimination(double matrix[MAX_EQUATIONS][MAX_EQUATIONS + 1], int n) {
int row, column, pivot;
double pivotfactor;
for (pivot = 0; pivot < n; pivot++) {
// Making the diagonal (pivot) element 1
pivotfactor = matrix[pivot][pivot];
for (column = 0; column <= n; column++) {
matrix[pivot][column] /= pivotfactor;
}
// Making other rows zero in the current pivot column
for (row = 0; row < n; row++) {
if (row != pivot) {
double eliminationFactor = matrix[row][pivot];
for (column = 0; column <= n; column++) {
matrix[row][column] -= eliminationFactor * matrix[pivot][column];
}
}
}
}
// Printing the results
printf("\nSolved Forces:\n");
for (int forceIndex = 0; forceIndex < n; forceIndex++) {
printf("Force %d: %.2f N\n", forceIndex + 1, matrix[forceIndex][n]);
}
}
int main() {
FILE *file;
file = fopen("truss_data.txt", "r"); // Open file in read mode
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
int n; // Number of unknown forces to solve
double matrix[MAX_EQUATIONS][MAX_EQUATIONS + 1]; // Augmented matrix
fscanf(file, "%d", &n); // Read the number of equations
for (int row = 0; row < n; row++) {
for (int column = 0; column <= n; column++) {
fscanf(file, "%lf", &matrix[row][column]); // Read values into matrix
}
}
fclose(file); // Close the file after reading
// Printing the loaded augmented matrix
printf("\nLoaded Augmented Matrix:\n");
for (int row = 0; row < n; row++) {
for (int column = 0; column <= n; column++) {
printf("%8.2f", matrix[row][column]);
}
printf("\n");
}
// Perform Gaussian elimination to solve for forces
GaussianElimination(matrix, n);
}