Thread: Matrix Multiplication in C

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    12

    Matrix Multiplication in C

    Hi,

    I am trying to create a program in C that will multiply a matrix with a vector. Both the size of the vector and dimensions of the matrix are given by the user. I am having the most trouble trying to declare and use the 2D array, which is my matrix. I just don't get it. Here is what I have so far. there are probably a lot of errors because I don't quite understand how to use pointers correctly.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
    	
    	int s, z;	 /*s is number of columns, z number of rows and length of vector*/
    	int i, x, y, ns, nz;
    	double *X, *E, *a;		/*X is input vector, E is result vektor, a is matrix*/
    	FILE *fd;
    	
    	
    	/*Vektor*/
    	fd = fopen("vektor3_2.dat", "r");
    	fscanf(fd, "%d", &z);
    	nz=z;
    	
    	if (z < 2) return 2;          
    	
    	X = malloc(z * sizeof(*X));    
    	E = malloc(z * sizeof(*E));
    	a = malloc(z * s * sizeof(*a));
    	
    	for (i = 0; i < z; i++)
    		fscanf(fd, "%lf", &X[z]);  
    	
    	
    	
    	/*Matrix*/
    	fscanf(fd, "%d", &s);
    	ns=s;
    	nz=z;
    	for (z = 0; z<nz; z++)
    		for (s=0; s<ns; s++)
    			fscanf(fd, "%lf", &a[z][s]);
    		
    	
    	fclose(fd);
    	
    	/*Multiplication*/
    	
    	for (z=0; z<nz; z++)
    		for (s=0; s<ns; s++)
    			E[z] = E[z] + a[z][s] * X[z];
    	
    	/*result out*/
    	fd = fopen("Ergebnis3_2.dat", "w");
    		for(i=0; i<z; i++)
    			printf("%lf\n", E[z]);
    	
    	
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    a = malloc(z * s * sizeof(*a));
    Shouldn't you have some size for s at this point?
    Code:
    for (i = 0; i < z; i++)
    		fscanf(fd, "%lf", &X[z]);
    That should be i not z.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    12
    Ya, that was a dumb error. But as far as the matrix a goes, the compiler doesn't recognize it. This is the error:
    pr3_2.c: In function ‘main’:
    pr3_2.c:33: error: subscripted value is neither array nor pointer
    pr3_2.c:42: error: subscripted value is neither array nor pointer

    Code:
    fscanf(fd, "%lf", &a[z][s]);
    and
    Code:
    E[z] = E[z] + a[z][s] * X[z];
    how declare the matrix and then use it?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > a = malloc(z * s * sizeof(*a));
    Well you've allocated a flattened array (1D), not something you can subscript with [][]

    If you want to sub-script by hand, then can do if you want.
    fscanf(fd, "%lf", &a[z][s]);
    would be
    fscanf(fd, "%lf", &a[z*ns+s]);



    Alternatively, you need
    Code:
    double **a;
    a = malloc( z * sizeof(*a) );
    for ( i = 0 ; i < z ; i++ ) a[i] = malloc( s * sizeof (*a[i]) );
    This results in something you can use [][] notation on.
    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
    May 2010
    Posts
    12
    Ok, so in this new code, the first line creates a pointer to a. The second reserves locations for z (aka rows) and the third line reserves locations for columns within each row
    Is that correct?

    And so now I should be able to use [][] for each location in the matrix?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    12
    That fixed the compiling errors, but now I just get an empty matrix out (i.e 0 0 0 0). I can't tell if my error lies in my multiplication part, or the entry of data.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Dunno without seeing the latest code.
    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.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    12
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
    	
    	int s, z;			/*nummer von Spalten*/
    	int i, x, y, ns, nz;
    	double *X, *E, **a;		/*nummer von Zeilen*/
    	FILE *fd;
    	
    	
    	/*Vektor*/
    	fd = fopen("vektor3_2.dat", "r");
    	fscanf(fd, "%d", &z);
    	nz=z;
    	
    	if (z < 2) return 2;          /* Programm beenden, wenn n < 2 */
    	
    	X = malloc(z * sizeof(*X));           /*Speicherplaetze fuer Vektorcomponenten reservieren*/
    	E = malloc(z * sizeof(*E));
    	a = malloc(z * sizeof(*a));
    	for (i=0; i<z; i++)
    		a[i] = malloc(s*sizeof(*a[i]));
    	
    	for (i = 0; i < z; i++)
    		fscanf(fd, "%lf", &X[i]);  /*Vektor Inhalt bekommen*/
    	
    	
    	
    	/*Matrix*/
    	fscanf(fd, "%d", &s);
    	ns=s;
    	nz=z;
    	for (z = 0; z<nz; z++)
    		for (s=0; s<ns; s++)
    			fscanf(fd, "%lf", &a[z][s]);
    		
    	
    	fclose(fd);
    	
    	/*Multiplication*/
    	
    	for (z=0; z<nz; z++)
    		for (s=0; s<ns; s++)
    			E[z] = E[z] + a[z][s] * X[z];
    	
    	
    	fd = fopen("Ergebnis3_2.dat", "w");
    		for(i=0; i<z; i++)
    			printf("%lf\n", E[z]);
    	
    	
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    12
    Sorry about the comments in German.
    s is number of columns
    z is number of rows
    E is result vector
    X is input vector
    a is input matrix

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You still can't be using s in your malloc before you know what s is.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    12
    Ok, I moved my input of s above malloc.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(){
    	
    	int s, z;			/*nummer von Spalten*/
    	int i, x, y, ns, nz;
    	double *X, *E, **a;		/*nummer von Zeilen*/
    	FILE *fd;
    	
    	
    	/*Vektor*/
    	fd = fopen("vektor3_2.dat", "r");
    	fscanf(fd, "%d", &z);
    	fscanf(fd, "%d", &s);
    	ns=s;
    	nz=z;
    	
    	if (z < 2) return 2;          /* Programm beenden, wenn n < 2 */
    	
    	X = malloc(z * sizeof(*X));           /*Speicherplaetze fuer Vektorcomponenten reservieren*/
    	E = malloc(z * sizeof(*E));
    	a = malloc(z * sizeof(*a));
    	for (i=0; i<z; i++)
    		a[i] = malloc(s*sizeof(*a[i]));
    	
    	for (i = 0; i < z; i++)
    		fscanf(fd, "%lf", &X[i]);  /*Vektor Inhalt bekommen*/
    	
    	
    	
    	/*Matrix*/
    	
    	for (z = 0; z<nz; z++)
    		for (s=0; s<ns; s++)
    			fscanf(fd, "%lf", &a[z][s]);
    		
    	
    	fclose(fd);
    	
    	/*Multiplication*/
    	
    	for (z=0; z<nz; z++)
    		for (s=0; s<ns; s++)
    			E[z] = E[z] + a[z][s] * X[z];
    	
    	
    	fd = fopen("Ergebnis3_2.dat", "w");
    		for(i=0; i<z; i++)
    			printf("%lf\n", E[z]);
    	
    	
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose if you want the right answer it might not hurt to set E[z] to 0 before starting to add things up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  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 Multiplication ( Accessing data from a file ) HELP
    By six_degreez in forum C Programming
    Replies: 2
    Last Post: 07-24-2008, 05:21 PM
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM

Tags for this Thread