MATRIX multiplication

This is a discussion on MATRIX multiplication within the C Programming forums, part of the General Programming Boards category; whats the error in the code ? Code: // MATRIX MULTIPLICATION #include<stdio.h> # define MAXROWS 20 # define MAXCOLS 30 ...

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

    MATRIX multiplication

    whats the error in the code ?


    Code:
    // MATRIX MULTIPLICATION 
    
           #include<stdio.h>
           # define MAXROWS 20
           # define MAXCOLS 30
           
           void readinput (int a[][MAXCOLS], int nrows, int ncols );
           void computesums (int a[][MAXCOLS], int b[][MAXCOLS], int c[][MAXCOLS], int nrows, int ncols);
           void writeoutput ( int c[][MAXCOLS] , int nrows, int ncols);
           void main()
    	   {
    		   
    	     int nrows, ncols,t;
    		 int a[MAXROWS][MAXCOLS],  b[MAXROWS][MAXCOLS] ,  c[MAXROWS][MAXCOLS] ;
    
    		 printf("Enter the no. of rows and columns of the first matrix");
    		 printf("How many rows?");
    		 scanf("%d", &nrows);
    		 printf("How many columns?");
    		 scanf("%d", &t);
             printf("\n\nFirst matrix:\n");
    		 readinput(a,nrows,t);
    
    		 printf("Enter the no. of rows and columns of the Second matrix");
    		 printf("How many rows?");
    		 scanf("%d", &t );
    		 printf("How many columns?");
    		 scanf("%d", & ncols);
             printf("\n\nSecond matrix:\n");
    		 readinput(b,t,ncols);
    
    		 computesums (a,b,c,t);
    
    		 printf("\nMultiplication of the elements:\n\n");
    
    		 writeoutput(c, nrows, ncols);
    
    
    	   }
    
    
    	   void readinput( int a[][MAXCOLS], int m , int n)
    
    	   {
    	    
    		int row,col ;
    		for(row=0;row<m; ++ row)
    		{
    			printf("\nEnter the data for row no. %d\n", row+1);
                for(col=0;col<n;++col)
                scanf("%d", &a[row][col]);
    		}
               return;
    
    	   }
    
           void computesums( int a[][MAXCOLS] , int b[][MAXCOLS], int c[][MAXCOLS], int f)
    
    	   {
    	       int row, col;
    		   for(row=0; row<f;++row)
               for(col=0; col<f;++col)
    
    				for(int i=0;i<f;++i)
    
    				{   c[row][col]=0;
    					c[row][col]+=a[row][i]*b[i][col] ;
    				}
    			   return;
    	   }
              void writeoutput( int c[][MAXCOLS], int m, int n)
    
    	   {
    	    int row, col;
    		for(row=0;row<m;++ row)
    
    		{ for(col=0; col<n;++col)
              printf("%d ", c[row][col]);
    		  printf("\n");
    		}
            return;
    
    		  }


    error C2660: 'computesums' : function does not take 4 parameters
    Error executing cl.exe.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
           void computesums (int a[][MAXCOLS], int b[][MAXCOLS], int c[][MAXCOLS], int nrows, int ncols);
    Your function prototype is different from definition.

  3. #3
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Yep. The first time you define computesums has to match up with the second time you define computesums. Change the argument of the first definition to match the second definition. Alternatively, you can change the argument of the second one to match the first one, and put int f in both.

    It's also lacking in general polish; it almost never makes any sense to put two printfs right after each other. If you run it like this, it'll say "Enter the no. of rows and columns of the First matrixHow many rows?", which I trust you do not want at all. Add a line break somewhere in between. As my old information literacy teacher would say, "Computers are stupid! They only do what you tell them to do!".
    Last edited by mszegedy; 05-31-2010 at 12:03 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    73
    i have edited but the result is still shows error. However, at least the program is running .


    Code:
    
           // MATRIX MULTIPLICATION 
    
           #include<stdio.h>
           # define MAXROWS 20
           # define MAXCOLS 30
           
           void readinput (int a[][MAXCOLS], int nrows, int ncols );
           void computesums (int a[][MAXCOLS], int b[][MAXCOLS], int c[][MAXCOLS], int f, int ncols);
           void writeoutput ( int c[][MAXCOLS] , int nrows, int ncols);
           void main()
    	   {
    		   
    	     int nrows, ncols,t;
    		 int a[MAXROWS][MAXCOLS],  b[MAXROWS][MAXCOLS] ,  c[MAXROWS][MAXCOLS] ;
    
    		 printf("Enter the no. of rows and columns of the first matrix\n");
    		 printf("How many rows?");
    		 scanf("%d", &nrows);
    		 printf("How many columns?");
    		 scanf("%d", &t);
             printf("\n\nFirst matrix:\n");
    		 readinput(a,nrows,t);
    
    		 printf("Enter the no. of rows and columns of the Second matrix\n");
    		 printf("How many rows?");
    		 scanf("%d", &t );
    		 printf("How many columns?");
    		 scanf("%d", & ncols);
             printf("\n\nSecond matrix:\n");
    		 readinput(b,t,ncols);
    
    		 computesums (a,b,c,nrows, ncols);
    
    		 printf("\nMultiplication of the elements:\n\n");
    
    		 writeoutput(c, nrows, ncols);
    
    
    	   }
    
    
    	   void readinput( int a[][MAXCOLS], int m , int n)
    
    	   {
    	    
    		int row,col ;
    
    		for(row=0;row<m; ++ row)
    		{
    			printf("\nEnter the data for row no. %d\n", row+1);
                for(col=0;col<n;++col)
                scanf("%d", &a[row][col]);
    		}
               return;
    
    	   }
    
           void computesums( int a[][MAXCOLS] , int b[][MAXCOLS], int c[][MAXCOLS], int f, int ncols)
    
    	   {
    	       int row, col;
    		   for(row=0; row<f;++row)
               for(col=0; col<ncols;++col)
    
    				for(int i=0;i<f;++i)
    
    				{   
    					
    					int j;
    
    			       	c[row][col]=0;
    
    					j=a[row][i]*b[i][col] ;
    
    					c[row][col]+=j;
    
    
    
    				}
    
    			   return;
    	   }
             
    	   
    	   void writeoutput( int c[][MAXCOLS], int m, int n)
    
    	   {
    	    int row, col;
    		for(row=0;row<m;++ row)
    
    		{ 
    			for(col=0; col<n;++col)
    
              printf("%d ", c[row][col]);
    		  printf("\n");
    		
    		}
            
    		return;
    
    
    
    		  }

    And , lastly , mszegedy @ what you are doing in that extreme cold or its just a joke ?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    What kind of error?
    Give your input, expected output, actual output.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
       for(row=0; row<f;++row) {
                   for(col=0; col<ncols;++col) {
    			   c[row][col]=0;            /*  move to here */
    		           for(int i=0;i<f;++i) {   
    				int j;
                               /* c[row][col] = 0;         */
    					j=a[row][i]*b[i][col] ;
    
    					c[row][col]+=j;
    
                              }
                   }
    	}

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    73
    Bayint Naung @ THANKS YOU VERY MUCH . ITS WORKING NOW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-18-2010, 10:17 AM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. *operator overloading: scalar matrix multiplication
    By gemini_shooter in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:14 PM
  4. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM

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