Hi, I need help with an assignment I have. I am a pure beginner/novice in C programming and please bear with me if my lack of knowledge frustrates you.
So basically I am to create a program that will read two saved text files; one is [2x4] ~ (matrixA.txt) and another is [4x2] ~ (matrixB.txt). The program is supposed to read both text files, multiply them, and generate an output that will be saved as ~ (matrixC.txt).
So here is my horrible attempt at it:
Code:
#include <stdio.h>
int main()
{
FILE * fileA;
FILE * fileB;
FILE * fileC;
int A[2][4];
int B[4][2];
int C[2][2];
fileA= fopen("matrixA.txt", "r"); //read the files
fileB= fopen("matrixB.txt", "r");
if (fileA == NULL) //check file A
{
printf("Error: Can't open file A \n");
exit(1);
}
if (fileB == NULL) //check file B
{
printf("Error: Can't opening file B \n");
exit(1);
}
for (int i=0, i<2, i++)
{
for (int j=0, j<4, j++)
{
fscanf(fileA, "%d", &A[i][j]); //reads contents of the fileA
}
}
fclose (fileA);
for (int i=0, i<4, i++)
{
for (int j=0, j<2, j++)
{
fscanf(fileB, "%d", &B[i][j]); //fileB
}
}
fclose(fileB);
for(int i=0, i<2, i++) //algorithm for multiplying the two matrices
{
for (int j=0, j<2, j++)
{
C[i][j]= 0;
for(int k=0, k<4, k++)
{
C[i][j]= C[i][j]+ (A[i][k]* B[k][j]);
}
}
}
fileC= fopen("matrixC.txt", "w"); //I am totally unsure about this
fprintf(fileC, "%d", &C[i][j]); //Supposed to open an empty text file I created and dump the results of matrixC in it??
fclose (fileC); //You guys will prob get mad at this point...
return 0;
}
And these are the compile errors...
C:\Users\LeDerp\HW1.c: In function `main':
HW1.c:27: parse error before `int' //Line 28
C:\Users\LeDerp\HW1.c: At top level:
HW1.c:34: warning: parameter names (without types) in function declaration //35
HW1.c:34: warning: data definition has no type or storage class //35
HW1.c:35: parse error before `for' //37
HW1.c:42: warning: parameter names (without types) in function declaration //44
HW1.c:42: warning: data definition has no type or storage class //44
HW1.c:43: parse error before `for' //46
HW1.c:55: warning: initialization makes integer from pointer without a cast //58
HW1.c:55: initializer element is not constant //58
HW1.c:55: warning: data definition has no type or storage class //58
HW1.c:56: parse error before string constant //59
HW1.c:56: warning: data definition has no type or storage class //59
HW1.c:57: warning: parameter names (without types) in function declaration //60
HW1.c:57: warning: data definition has no type or storage class //60
HW1.c:59: parse error before `return' //62
I am just tired out at this point but if you have any suggestions I will give you my full attention. Thank you.