So far I have this to work with, I'm suppose to read in a file that has a 3x3 matrix of values such as:
1 2 3
4 5 6
7 8 9
The program will prompt to input a 3 numbers such as a vector to do vector multiplication. So if that matrice was given along with vector 1,2,3 the output should be
(1*1) + (1*2) + (1*3) = row1 value
(2*4) + (2*5) + (2*6) = row2 value
(3*7) + (3*8) + (3*9) = row3 value
The individual multiplications should be resaved into the matrix for further use later on, so the values should be modifying. I've gotten some of the output but I don't think the loop is working 100%. Any suggestions ?
Code:
#include <stdio.h>
#define MAX 256
int main(int argc, char **argv)
{
static const char *filename;
filename = argv[1];
FILE *file = fopen(filename, "r");
double matrix[3][3];
char input[MAX];
double val0,val1,val2,row0,row1,row2;
int i,j;
//checks the file to make sure its no empty and copy the values to a 1D array
if ( file != NULL )
{
int i, j;
for ( i = 0; i < sizeof matrix / sizeof *matrix; ++i )
{
for ( j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
{
if ( fscanf(file, "%lf", &matrix[i][j]) != 1 )
{
puts("Input Error");
}
}
}
fclose(file);
//prints out the matrix that was read from the file
for(i = 0; i < sizeof matrix / sizeof *matrix; ++i )
{
for(j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
{
printf("%g ", matrix[i][j]);
}
putchar('\n');
}
}
printf("\n");
printf("Input Values to Multiply ");
fflush(stdout);
fgets(input,sizeof(input),stdin);
sscanf(input,"%lf %lf %lf",&val0, &val1, &val2);
//multiplying the rows and summing them up
for(i = 0; i <3 ; i++)
for(j = 0; j < 3; j++) {
matrix[j][i] = matrix[j][i] * val0;
matrix[j+1][i+1] = matrix[j][i] * val1;
matrix[j+2][i+2] = matrix[j][i] * val2;
}
printf("matrix after multiplication\n");
//prints out the matrix that was read from the file
for(i = 0; i < sizeof matrix / sizeof *matrix; ++i )
{
for(j = 0; j < sizeof *matrix / sizeof **matrix; ++j )
{
printf("%g ", matrix[i][j]);
}
putchar('\n');
}
printf("\n");
printf("%1.1f %1.1f %1.1f %\n",row0,row1,row2);
return(0);
}