# Matrix multiplication

• 02-11-2011
prico
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]);       }   } }```
• 02-11-2011
anduril462
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.
• 02-11-2011
rags_to_riches
Another cross-poster.
• 02-11-2011
prico
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.
• 02-11-2011
prico
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]);       }   } }```
• 02-11-2011
anduril462
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.
• 02-11-2011
prico
I got no replies that's why I posted here. The more minds, the better.
• 02-11-2011
anduril462
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.
• 02-11-2011
prico
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.
• 02-11-2011
CommonTater
Quote:

Originally Posted by anduril462
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.
• 02-11-2011
whiteflags
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.
• 02-11-2011
prico
Quote:

Originally Posted by whiteflags
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.
• 02-11-2011
whiteflags
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.