Thread: Help with a matrix program.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    7

    Help with a matrix program.

    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.
    Last edited by usernameC; 01-29-2013 at 10:40 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Right off the bat you need to replace this:
    Code:
    for (int i=0, i<2, i++)
    with
    Code:
    for (int i=0; i<2; i++)
    Note the semi-colons in the second example. Change all your for loops to use semi-colons in this manner, and see what happens when you compile it.

    And welcome to the forum!

    For your output - actually, I don't understand what you're trying to do there - but one step at a time.
    Last edited by Adak; 01-29-2013 at 10:55 PM.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Hi, thank you! Hope to contribute to this forum whenever I can.

    So this is what I have.

    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");
        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]);
                }
            }
         fclose (fileA); 
         for (int i=0; i<4; i++)
             {
            for (int j=0; j<2; j++)
                {
                fscanf(fileB,"%d", &B[i][j]);
                }
            }
         fclose(fileB); 
         for(int i=0; i<2; i++)
             {
            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"); 
         fprintf(fileC; "%d"; &C[i][j]); 
         fclose (fileC);
         
        return 0;
    }
    And this is what I get:

    C:\Users\LeDerp\HW1.c: In function `main':
    HW1.c:27: parse error before `int'
    HW1.c:27: `i' undeclared (first use in this function)
    HW1.c:27: (Each undeclared identifier is reported only once
    HW1.c:27: for each function it appears in.)
    HW1.c:27: parse error before `)'
    HW1.c:29: `j' undeclared (first use in this function)
    HW1.c:29: parse error before `)'
    C:\Users\LeDerp\HW1.c: At top level:
    HW1.c:34: warning: parameter names (without types) in function declaration
    HW1.c:34: warning: data definition has no type or storage class
    HW1.c:35: parse error before `for'
    HW1.c:42: warning: parameter names (without types) in function declaration
    HW1.c:42: warning: data definition has no type or storage class
    HW1.c:43: parse error before `for'
    HW1.c:55: warning: initialization makes integer from pointer without a cast
    HW1.c:55: initializer element is not constant
    HW1.c:55: warning: data definition has no type or storage class
    HW1.c:56: parse error before string constant
    HW1.c:56: warning: data definition has no type or storage class
    HW1.c:57: warning: parameter names (without types) in function declaration
    HW1.c:57: warning: data definition has no type or storage class
    HW1.c:59: parse error before `return'
    Last edited by usernameC; 01-29-2013 at 11:07 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (int i=0; i<2; i++)
    I would suggest you try

    int i, j;
    at the start of main, and write

    for ( i=0; i<2; i++)

    For loop declarations are a feature of C99 (a newer version of C). If you're using an old compiler, it just won't understand it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Dongargarh, India
    Posts
    16
    you need to declare your variables i,j,k before you use them
    & you also need to replace semicolons(;) with commas(,) in fscanf & fprintf.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Hey guys,

    I'm currently updating my compiler. I will take all suggestions so far and make changes. I will post the results in a second.

    Thanks!

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE * fileA;
         FILE * fileB;
         FILE * fileC;
         
        int A[2][4];
        int B[4][2];
        int C[2][2];   
         int i, j, k;
         
        fileA= fopen("matrixA.txt", "r");
        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]);
                }
            }
         fclose (fileA); 
         for (int i=0; i<4; i++)
             {
            for (int j=0; j<2; j++)
                {
                fscanf(fileB, "%d", &B[i][j]);
                }
            }
         fclose(fileB); 
         for(int i=0; i<2; i++)
             {
            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"); 
         fprintf(fileC, "%d", &C[i][j]); 
         fclose (fileC);
         
        return 0;
    }
    I'm not sure why I keep getting parse errors, I'm sure I got the right formats..
    Errors:

    C:\Users\LeDerp\HW1.c: In function `main':
    HW1.c:28: parse error before `int'
    HW1.c:28: parse error before `)'
    HW1.c:30: parse error before `)'
    C:\Users\LeDerp\HW1.c: At top level:
    HW1.c:35: warning: parameter names (without types) in function declaration
    HW1.c:35: warning: data definition has no type or storage class
    HW1.c:36: parse error before `for'
    HW1.c:43: warning: parameter names (without types) in function declaration
    HW1.c:43: warning: data definition has no type or storage class
    HW1.c:44: parse error before `for'
    HW1.c:56: warning: initialization makes integer from pointer without a cast
    HW1.c:56: initializer element is not constant
    HW1.c:56: warning: data definition has no type or storage class
    HW1.c:57: parse error before string constant
    HW1.c:57: warning: data definition has no type or storage class
    HW1.c:58: warning: parameter names (without types) in function declaration
    HW1.c:58: warning: data definition has no type or storage class
    HW1.c:60: parse error before `return'

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    The last 3 lines before return 0 is my horrible attempt at getting array C to be saved as a .txt file..

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    For the compiling errors:


    The "exit" funciton needs stdlib.h


    the "int" needs to be removed from for loop (to declare "i" in the way Salem suggested)


    In the fprintf, the variable C does not require '&'
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    Thank you all for your help. From your suggestions I have finally got this program to work. I used Dev C++ compiler instead of jGrasp and it ran.

    Here's what the program code is:

    Code:
    #include <stdio.h>
    
    int main()
    {
        FILE * fileA;
         FILE * fileB;
         FILE * fileC;
         
        int A[2][4];
        int B[4][2];
        int C[2][2];   
        int i, j, k;
         
        fileA= fopen("matrixA.txt", "r");
        fileB= fopen("matrixB.txt", "r");   
         
           if (fileA == NULL) //check file A
              {
              printf("Error: Can't open file A \n");
              return 0;
              }
              
           if (fileB == NULL) //check file B
              {
              printf("Error: Can't opening file B \n");
              return 0;
                 } 
            
        for ( int i=0; i<2; i++ )
             {
            for (int j=0; j<4; j++)
                {
                fscanf(fileA,"%d", &A[i][j]);
                }
            }
         fclose (fileA); 
         for (int i=0; i<4; i++)
             {
            for (int j=0; j<2; j++)
                {
                fscanf(fileB, "%d", &B[i][j]);
                }
            }
         fclose(fileB); 
         for(int i=0; i<2; i++)
             {
            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]); // algorithmn
                    }
                }
            } 
         fileC= fopen("matrixC.txt", "w"); 
         fprintf(fileC, "%d", C[i][j]); 
         fclose (fileC);
             
        return 0;
    }
    So my only problem right now is my output. Either something is wrong with the algorithm or it's not printing right.

    Matrix A is:

    1 2 3 4
    8 7 6 5

    Matrix B is:

    1 1
    1 1
    1 1
    1 2

    A Matrix C file is written but in it is just:

    10

    While it should be:

    10 14
    26 31

    So what went wrong?

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Your fprintf is called once -> It takes the value of i and j and spits out what ever it comes up with.

    What you will need to do is create a "for" loop that prints out each column, printing a \n at the end of the column.
    Fact - Beethoven wrote his first symphony in C

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    ^ Note that the for loop should just go around the fprintf, not the fopen and fclose.

    Also, you should probably check to see if it's open before you try and write to it...
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    7
    So I have it like this:

    Code:
     for (int i=0; i<2; i++)
                {
                    for (int j=0; j<2;j++)
                    {
                     fprintf(fileC, "%d \n", C[i][j]); 
                    }
                }
    And it gets spit out like:

    10
    14
    26
    31

    How do I get it to appear like:

    10 14
    26 31

    Sorry I'm still not sure how the line breaks or space works. But thank you for your help!

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Delete the \n from line #5.

    Move line 7 to line 8.
    make printf("\n"); the new line 7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with a program for a LED matrix.
    By Kakefarmer in forum C Programming
    Replies: 15
    Last Post: 01-13-2012, 04:34 PM
  2. Matrix c program help!!!
    By kclew in forum C Programming
    Replies: 21
    Last Post: 02-25-2009, 04:46 AM
  3. Help! matrix program
    By jadman in forum C Programming
    Replies: 3
    Last Post: 06-21-2005, 11:20 AM
  4. 2x2 matrix program
    By screamer903 in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 08:02 AM
  5. matrix program
    By J in forum C Programming
    Replies: 4
    Last Post: 04-21-2002, 08:07 PM