Matrix multiplication

This is a discussion on Matrix multiplication within the C Programming forums, part of the General Programming Boards category; I am trying to perfrom multiplication on matrices entered in a text file. First line in text file should be ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Matrix multiplication

    I am trying to perfrom multiplication on matrices entered in a text file. First line in text file should be the size (matrices are going to be use size for row and column size). The remaining lines will list the values in matrices. For example, in data.txt I might have:
    2
    1.2 2.3
    -3.0 1.0
    7.1 0.4
    6.3 2.6

    My multiplying doesn't work however, that's what I need help fixing. Here's the code.

    Code:
    #include <stdio.h>
    
    typedef struct {
       int n;
       double *entry; }
    matrix;
    
    FILE *file;
    char filename[]="";
    int n,i,j,k;
    matrix *a;
    matrix *b;
    matrix *c;
    
    
    int main (void) {
       
       printf("Please enter the input file: ");
       scanf("%s", &filename);
    
       file = fopen(filename, "r");
       if (file != NULL) {
           }
       else
        {
           printf("Error: File Not Found");
        }
    
    fscanf(file,"%d", &n);
    
    a=(matrix *)malloc(sizeof(matrix));
    b=(matrix *)malloc(sizeof(matrix));
    c=(matrix *)malloc(sizeof(matrix));
    
    a->entry=(double *)malloc(sizeof(double)*n*n);
    b->entry=(double *)malloc(sizeof(double)*n*n);
    c->entry=(double *)malloc(sizeof(double)*n*n);
    
    buildMatrix(n);
    }
    
    void buildMatrix(n) {
    printf("\nMatrix A includes: \n");
    for (i=0;i<n;i++) {
       for (j=0;j<n;j++) {
           fscanf(file,"%lf\t",&a->entry[i*n+j]);
           if (j<n-1)
                 printf("%.2lf\t", a->entry[i*n+j]);
           else
                 printf("%.2lf\n", a->entry[i*n+j]);
         }
       }
    printf("\nMatrix B includes: \n");
    for (i=0;i<n;i++) {
        for (j=0;j<n;j++) {
             fscanf(file,"%lf\t", &b->entry[i*n+j]);
             if (j<n-1)
                printf("%.2lf\t", b->entry[i*n+j]);
             else
                printf("%.2lf\n", b->entry[i*n+j]);
         }
       }
     }
    
    void matrixMultiply(n) {
    for(i=0;i<n;i++) {
       for(j=0;j<n;j++) {
            float sum=0.0;
            for(k=0;k<n;k++)
             {
               sum=sum+(a[i][k]*b[k][j]);
               c[i][j]=sum;
             }
        printf ("%.2lf\t",c[i][j]);
          }
       }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    This question, and some of your specific problems have been addressed many times before. You should always search a form before posting to see if your question has already been answered. Here is the result of searching the C forum for "matrix multiplication": C Board - Search Results.

    Read those posts in light of the fact that you have problems with the way you allocate space for and iterate through 2-D arrays. If you still have problems, come on back and give us as much specific info as possible. What doesn't work about your multiplication? Wrong values, seg fault, etc.

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,639
    Another cross-poster.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    I read them, but they don't help. I know I needed to stop casting, but I don't know how to fix my multiplyMatrix (n) so that it will multiply and print the resulting matrix.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    By the way the error says that "subscripted value is neither array nor pointer". Here is how I changed the code

    Code:
    #include <stdio.h>
    
    /* Creates structure */
    typedef struct {
       int n;
       double *entry; }
    matrix;
    
    /*Initializes matrix pointers, size, and the loop variables (i,j,k). Also initilizes character array. */
    FILE *file;
    char filename[]="";
    int n,i,j,k;
    matrix *a;
    matrix *b;
    matrix *c;
    
    
    int main (void) {
       
       printf("Please enter the input file: "); /* Asks user for the name of the file that holds their matrix info */
       scanf("%s", &filename); /*Scans user's string and saves it in filename */
    
       file = fopen(filename, "r"); /* Opens the file the user input */
       if (file != NULL) { /*Checks if the file can be opened. */
           }
       else
        {
           printf("Error: File Not Found"); /* If file is not found, error message shows. */
        }
    
    fscanf(file,"%d", &n); /* Scans file for integer and stores this integer in n */
    
    /* Allocates space for matrices (a, b, and result in c) */
    a=(matrix *)malloc(sizeof(matrix));
    b=(matrix *)malloc(sizeof(matrix));
    c=(matrix *)malloc(sizeof(matrix));
    
    a =(double *)malloc(sizeof(double)*n*n);
    b =(double *)malloc(sizeof(double)*n*n);
    c =(double *)malloc(sizeof(double)*n*n);
    
    buildMatrix(n);
    }
    
    /*Places values into matrix a and b from text file */
    void buildMatrix(n) {
    printf("\nMatrix A includes: \n");
    for (i=0;i<n;i++) {
        fscanf(file,"%lf\t", a[i]+j);
       }
    
    printf("\nMatrix B includes: \n");
    for (j=0;j<n;j++) {
        fscanf(file,"%lf\t", b+j);
       }
     }
    void matrixMultiply(n) {
    for(i=0;i<n;i++) {
       for(j=0;j<n;j++) {
            float sum=0.0;
            for(k=0;k<n;k++)
             {
               sum=sum+(a[i][k]*b[k][j]);
               c[i][j]=sum;
             }
        printf ("%.2lf\t",c[i][j]);
          }
       }
    }

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Are you not getting enough help on dream.in.code (hence rags_to_riches comment)? Cross posting is considered rude and wastes peoples time. I don't really want to spend my time explaining something somebody may have already stated on dream.in.code. The smart folks over there will help you. Read up on proper forum etiquette too, to avoid such blunders in the future.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    I got no replies that's why I posted here. The more minds, the better.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,288
    Liar! You posted there at 1:29pm, and the first reply was at 1:47pm, a full 2 hours and 20 minutes before you posted here. You didn't even post your followup at dream.in.code before spamming us. Be patient instead of selfish and greedy with other people's time. You're getting help there, so just stick with it.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    I didn't post on there at 1. I didn't post until after my class and my class is at 3. Come on cut me some slack. I'm not asking anyone to do it for me. I just want to fix it so that it will multiply the matrices and then print it out. I'm only asking for help.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    Liar! You posted there at 1:29pm, and the first reply was at 1:47pm, a full 2 hours and 20 minutes before you posted here. You didn't even post your followup at dream.in.code before spamming us. Be patient instead of selfish and greedy with other people's time. You're getting help there, so just stick with it.
    Another 200 or so man hours down the drain.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    The general rule of matrix multiplication is that for entry i,j you have to compute row i in table one and column j in table two.

    1.2 2.3
    -3.0 1.0

    7.1 0.4
    6.3 2.6


    (0,0) 1.2*7.1 + 2.3*6.3 = 23.01
    (0,1) 1.2*0.4 + 2.3*2.6 = 6.46
    (1,0) (-3.0)*7.1 + 1.0*6.3 = -15.0
    (1,1) (-3.0)*0.4 + 1.0*2.6 = 1.4

    So there is your matrix. You just have to do the same thing in your program.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Quote Originally Posted by whiteflags View Post
    The general rule of matrix multiplication is that for entry i,j you have to compute row i in table one and column j in table two.

    1.2 2.3
    -3.0 1.0

    7.1 0.4
    6.3 2.6


    (0,0) 1.2*7.1 + 2.3*6.3 = 23.01
    (0,1) 1.2*0.4 + 2.3*2.6 = 6.46
    (1,0) (-3.0)*7.1 + 1.0*6.3 = -15.0
    (1,1) (-3.0)*0.4 + 1.0*2.6 = 1.4

    So there is your matrix. You just have to do the same thing in your program.
    I understand how matrix multiplication works for me to do it on paper. I just don't know how to fix the function I have in my coding to display the appropriate results.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    Well then break down what should happen, and compare what actually happens in the function. For example, if the dimensions are 2x2, then you know that you will have two rows to compute and two columns. This culminates as a pair of for loops, one to control the rows, enclosed inside one to control the columns: where you add up the entry i,j and save it to the answer. Then, you have to print the answer with another similar loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21