Thread: matrix summation

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

    matrix summation

    anyone help me to correct the code ? where's the error ?


    Code:
    // MATRIX SUMMATION 
    
            
    
           #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;
    		 int a[MAXROWS][MAXCOLS], int B[MAXROWS][MAXCOLS] , int C[MAXROWS][MAXCOLS] ;
    
    		 printf("How many rows?");
    		 scanf("%d", &nrows);
    		 printf("How many columns?");
    		 scanf("%d", &ncols);
    
    		 printf("\n\nFirst matrix:\n");
    		 readinput(a,nrows, ncols);
    
    		 printf("\n\nSecond matrix:\n");
    		 readinput(b,nrows, ncols);
    
    		 computesums (a,b,c, nrows, ncols);
    		 printf("\nSums 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 a[][MAXCOLS], int c[][MAXCOLS] , int m, int n)
    
    	   {
    	    
    		   int row, col;
    		   for(row=0; row<m;++row)
    
    			for(col=0; col<n;++col)
    
    				c[row][col]=a[row][col]+b[row][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", a[row][col]);
    		  printf("\n");
    
    
    		}
    
    		return;
    
    	   }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The FIRST error is the part where you waste our time - since obviously you know the error in output - and decide not to state what that error is.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    73
    since obviously you know the error in output - and decide not to state what that error is.
    Adak @ Interesting reply! its not running at all.

  4. #4
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Umm, I'm no expert, but I don't think there should be a space between # and define…

    Oh, and Adak: OI, SHUT UP! What if (s)he ACTUALLY doesn't know? Ever thought of that?

    EDIT: Also, what compiler are you using? Usually it tells you what the problem is.
    Last edited by mszegedy; 05-30-2010 at 12:13 AM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Indeed I have thought about that. I also thought about advising you to refrain from telling others to shut up, mszegdy.

    Of course chaklader knows more about the problem, since he/she must have tried to compile or link it, at least.

    If you want help with a program, you need to share the relevant information you have.

  6. #6
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    I'm sorry. I just happen to become rudely hostile when one person treats another person's words that are, for all purposes, innocent and sincere, with extreme rudeness and hostility.

    As such, I believe that if chaklader had enough information to know how to solve the problem, (s)he would not post a topic about it in the first place.

    I have examined this code (granted, not extremely carefully) and have found nothing amiss. I will continue looking for errors. I think that #define thing might be the reason.

    What does your compiler say? I would run it through mine, but I'm typing this on an iPhone.

    Also, I would add two "if" flow controls (or possibly two "while" loops) to the main procedure that checks if the number of rows and columns given are below MAXCOLS and MAXROWS.

    Oh, and in the very first part of main when you are declaring those variables, make "B" and "C" lowercase. They are referred to everywhere else as "b" and "c", and it's case-sensitive I believe.
    Last edited by mszegedy; 05-30-2010 at 12:58 AM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    73
    Adak @ actually i thought its not necessary to share the compiler information .... however as you need that -


    error C2065: 'b' : undeclared identifier
    error C2065: 'c' : undeclared identifier
    error C2086: 'a' : redefinition
    error C2109: subscript requires array or pointer type
    error C2109: subscript requires array or pointer type
    error C2065: 'a' : undeclared identifier
    error C2109: subscript requires array or pointer type
    error C2109: subscript requires array or pointer type

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    73
    mszegedy @ thanks for your care .

  9. #9
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    You're welcome.

    Huh, "a" redefinition? Waitasec…

    Uh, replace "a" in computesums with some other name. It's confusing the compiler and messing things up.

    You might want to read the edit I made to my previous post. It's also messing your compiler up.


    It should work now.

    EDIT: Whoops, change "b" and "c" too. In computesums.
    Last edited by mszegedy; 05-30-2010 at 01:13 AM.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by mszegedy View Post
    I'm sorry. I just happen to become rudely hostile when one person treats another person's words that are, for all purposes, innocent and sincere, with extreme rudeness and hostility.

    As such, I believe that if chaklader had enough information to know how to solve the problem, (s)he would not post a topic about it in the first place.
    Chaklader knows more about this problem, and you see it posted, right above this one.

    I'm not being "rude and hostile", I'm trying to help Chaklader, solve a problem - quickly and efficiently.

    Would you prefer that I lie to Chaklader? I can't change the reality of the internet, this forum, or people in general. Truth is, they NEED to know the compiler or linker errors, if present, and they won't help posters who waste their time, nearly as much.

    Shall I gift wrap that for you, or is plain truth, in plain text, sufficient?

    In this function, the second parameter should be b, to match the function prototype.
    Code:
    void computesums( int a[][MAXCOLS] , int a[][MAXCOLS],
    In writeoutput(), you are referring to the a[] array, and you didn't bring in the a[] array, as a parameter to the function.

    In main(), B[] and C[], should be b[] and c[]. Everything is case sensitive in C.

    You don't want the word "int" before each variable in a list:

    int a[], b[][], c[][][], FILL IN THE SIZES, and then just one semi-colon on the end. Only one "int", though.
    Last edited by Adak; 05-30-2010 at 01:23 AM.

  11. #11
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Yes, telling someone (s)he is "wasting our time" is a very quick and efficient way of solving a problem.

    Perhaps (s)he doesn't know what his/her compiler is trying to tell him/her. It is very hard to understand compiler text if you don't think like a computer, or understand what "unsigned" means or what a "segmentation fault" is. Not that those appeared in the text. Worse, if your compiler isn't particularly verbose, you can end up with meaningless errors like SEGFAULT and such. And what the heck is that 2 supposed to mean before each line number? "2109" doesn't sound very much like a line number.
    Last edited by mszegedy; 05-30-2010 at 01:25 AM.

  12. #12
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Wait, he's offline. I'd better go to sleep (it's 3:30 AM where I am).

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by mszegedy View Post
    Yes, telling someone (s)he is "wasting our time" is a very quick and efficient way of solving a problem.

    Perhaps (s)he doesn't know what his/her compiler is trying to tell him/her. It is very hard to understand compiler text if you don't think like a computer, or understand what "unsigned" means or what a "segmentation fault" is. Not that those appeared in the text. Worse, if your compiler isn't particularly verbose, you can end up with meaningless errors like SEGFAULT and such.
    Three short words == quick!

    I don't CARE what the poster understands - we'll help with that. I need to know WHAT the compiler or linker error content IS.

    Those errors may be meaningless to you - but they are very meaningful to me (and others on this forum).

    Why don't you kick back for a bit, and see how well this forum works?

    Would you trust a doctor who didn't insist on knowing your symptoms when you get sick? I sure wouldn't.

    We're not here to hold the poster's hands. We solve problems from posters who are willing to make a real effort, and are forthright with the info we need. And we do it for free.

    Pretty neat, if you think about it.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    73
    mszegedy @ I really respect your care . Anyhow , please don't leave the forum .
    Adak @ The program is working now and I understood you as well . thanks for your help.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    @mszegedy My oh my aren't you the incarnation of forum ethics and dedication to helping others with your 8 posts, 5 of which are on this thread and make no attempt at solving the OP's problem.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

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. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM
  4. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM