Thread: additon of 2 matrices error in code

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1
    the code is as follows plz tell me where i went wrong and if possible plz explain
    Code:
    #include<stdio.h>
     main()
     {
     int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
     printf("input row and column of a matrix:\n");
     scanf("%d%d",&m,&n);
     printf("input row and column of the matrix:\n");
     scanf("%d%d",&p,&q);
     if((m==p)&&(n==q))
     {
     printf("matrices can be added\n");
     printf("input A matrix:\n");
     for(i=0;i<m;++i)
     for(j=0;j<n;++j)
     scanf("%d",&a[i][j]);
     printf("input b matrix:\n");
     for(i=0;i<m;++i)
     for(j=0;j<n;++j)
     scanf("%d",&a[i][j]);
     for(i=0;i<m;++i)
     {
      for(j=0;j< n;++j)
      c[i][j]=0;
      c[i][j]=a[i][j]+b[i][j];
        printf("%5d",c[i][j]);
      printf("\n");
    }
    }
    else
    printf("matrices can not be added");
    getch();
    }
    this code is getting executed but end results ie the sum is not as desired
    Last edited by sekharsomu; 08-09-2008 at 11:14 AM. Reason: for giving more inf

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    You do two times scanf() into a[][]...

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Code:
      for(j=0;j< n;++j)
      {
      c[i][j]=0;
      c[i][j]=a[i][j]+b[i][j];
        printf("%5d",c[i][j]);
      }
      printf("\n");

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please learn how to indent code, and fix main to explicitly return an int.

    The second one should read into b not a as rasta_freak says.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM