Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_STR_LENGTH 255
typedef struct {
int rows;
int cols;
int** element;
} matrix;
void displayMenu(void);
void addMatrix(char *filename, matrix *matrices, matrix *resMatrix);
int readMatrix(char *filename, matrix *matrices, int num);
void freeMalloc(matrix *matrices, int num);
int main(void){
int choice, n;
char inputBuffer[MAX_STR_LENGTH], filename[MAX_STR_LENGTH];
matrix matrices[2];
matrix resMatrix[1];
do{
displayMenu();
do{
gets(inputBuffer);
n = sscanf(inputBuffer, "%d", &choice);
}while (n != 1);
puts("");
switch(choice){
case 1:
puts("System >> Enter matrices for addition operation:");
printf("User >> ");
fflush(stdin);
gets(inputBuffer);
sscanf(inputBuffer, "%s", filename);
addMatrix(filename, matrices, resMatrix);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
puts("Please enter a choice from 1 to 5.");
}
}while(choice != 5);
puts("Exiting Program ... ...");
freeMalloc(matrices, 2);
return 0;
}
void displayMenu(void){
puts("");
puts("Welcome to simple matrix program.");
puts("Please select an option.");
puts("");
puts("(1) Add two matrices");
puts("(2) Subtract two matrices");
puts("(3) Multiplication of two matrices");
puts("(4) Assign a matrix to another");
puts("(5) Exit");
puts("");
printf("Your choice: ");
}
int readMatrix(char *filename, matrix *matrices, int num){
int read, orders = 1;
int i, j, rows, cols;
char inputBuffer[MAX_STR_LENGTH];
FILE *fp;
if ((fp = fopen(filename, "r")) != NULL){
for (read = 0; read < num; read++) {
do {
fgets(inputBuffer, MAX_STR_LENGTH, fp);
} while(strstr(inputBuffer, "<matrix>") == NULL);
fgets(inputBuffer, MAX_STR_LENGTH, fp);
matrices[read].rows = rows = atoi(strchr(inputBuffer, '=') + 2);
fgets(inputBuffer, MAX_STR_LENGTH, fp);
matrices[read].cols = cols = atoi(strchr(inputBuffer, '=') + 2);
matrices[read].element = (int**) malloc(sizeof(int) * rows);
printf("System >> Displaying matrix %d\n", orders);
orders++;
for (i = 0; i < rows; i++) {
matrices[read].element[i] = (int*) malloc(sizeof(int) * cols);
for (j = 0; j < cols; j++) {
fscanf(fp, "%d", &matrices[read].element[i][j]);
printf("%d ", matrices[read].element[i][j]);
}
printf("\n");
}
do {
fgets(inputBuffer, MAX_STR_LENGTH, fp);
} while (strstr(inputBuffer, "</matrix>") == NULL);
}
}
else {
printf("%s do not exist!", filename);
puts("");
return orders;
}
fclose(fp);
return orders;
}
void freeMalloc(matrix *matrices, int num) {
int current, i;
for (current = 0; current < num; current++) {
for (i = 0; i < matrices[current].rows; i++)
free(matrices[current].element[i]);
free(matrices[current].element);
}
}
void addMatrix(char *filename, matrix *matrices, matrix *resMatrix){
int i, j, rows, cols;
if (readMatrix(filename, matrices, 2) != 1){
resMatrix[0].rows = rows = matrices[0].rows;
resMatrix[0].rows = cols = matrices[0].cols;
puts("System >> Displaying resultant matrix after addition");
resMatrix[0].element = (int**) malloc(sizeof(int) * (rows+1));
for(i = 0; i < rows; i++){
if (i != 0) printf("\n");
for(j = 0; j < cols; j++){
resMatrix[0].element[i][j] = (matrices[0].element[i][j] +
matrices[1].element[i][j]);
printf("%d ", resMatrix[0].element[i][j]);
}
}}
}
The 2 by 2 array file matrix2-2.txt