Thread: matrix addition program using arrays

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    Question matrix addition program using arrays

    PHP Code:
    /* program to perform martix addition */

    # include <stdio.h>
    void main ()
    {
    int i,j;
    int a[2][2],b[2][2],c[2][2];

    printf("\nAddition of A & B martices");
    printf("\n\n Enter elements of martix A");
       for (
    i=0;i<=1;i++)
       { 
         for (
    j=0;j<=1;j++)
         {

         
    scanf("%d",&a[i][j]);
         }
       }
    Printf("\nEnter elements of matrix B"); 
       for (
    i=0;i<=1;i++)
       {
         for (
    j=0;j<=1;j++)
         {

         
    scanf("%d",&b[i][j]);
         }
       }

    for (
    i=0;i<=1;i++)
    {
     for (
    j=0;j<=1;j++)
     
    c[i][j]=a[i][j]+b[i][j];
    }
    printf(" Addition matrix of %d and %d is %d",a[2][2],b[2][2],c[2][2]);
    printf("\n");


    I'm getting errors. can you help me out.
    Its 2 x 2 matrix addition, where A and B 2D arrays are taken as inputs and C arrays is output ( addition matrix of A and B)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Same problem as in your other thread: you need to print the elements of a, b and c, not print an element that does not exist.

    By the way, instead of comparing with i <= 1, compare with i < 2. You also need to indent your code better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    please, can you tell give me the correct code of the program.
    How to indent the code.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by suryak
    please, can you tell give me the correct code of the program.
    Think: you used nested for loops to read each matrix. You used nested for loops to perform the addition. Why aren't you using nest for loops to print each matrix?

    Quote Originally Posted by suryak
    How to indent the code.
    This is how I might format the code:
    Code:
    /* program to perform matrix addition */
    
    # include <stdio.h>
    
    int main(void)
    {
        int i, j;
        int a[2][2], b[2][2], c[2][2];
    
        printf("\nAddition of A & B martices");
        printf("\n\n Enter elements of martix A");
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                scanf("%d", &a[i][j]);
            }
        }
    
        printf("\nEnter elements of matrix B");
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                scanf("%d", &b[i][j]);
            }
        }
    
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                c[i][j] = a[i][j] + b[i][j];
            }
        }
    
        /* Print matrices here */
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    // this is what I have changed it to, still I'm getting errors. can you give me the code of it please....

    /* program to perform martix addition */

    # include <stdio.h>
    void main ()
    {
    int i,j;
    int a[2][2],b[2][2],c[2][2];

    printf("\nAddition of A & B martices");
    printf("\n\n Enter elements of martix A");
    for (i=0;i<2;i++)
    {
    for (j=0;j<2;j++)
    {

    scanf("%d",&a[i][j]);
    }
    }
    Printf("\nEnter elements of matrix B");
    for (i=0;i<2;i++)
    {
    for (j=0;j<2;j++)
    {

    scanf("%d",&b[i][j]);
    }
    }

    for (i=0;i<2;i++)
    {
    for (j=0;j<2;j++)
    c[i][j]=a[i][j]+b[i][j];

    printf(" Addition matrix of %d and %d is %d",a[i][j],b[i][j],c[i][j]);
    }

    printf("\n");

    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now the problem is that your inner for loop lacks braces. You have those braces everywhere else, but managed to miss them in the one place where they were actually needed
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    ya I got it... thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bug in the addition drill program
    By Sharifhs in forum C Programming
    Replies: 1
    Last Post: 08-13-2010, 10:50 PM
  2. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  3. howto build this addition matrix
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 12-24-2008, 12:19 AM
  4. Addition Program - Help
    By ggraz in forum C Programming
    Replies: 6
    Last Post: 10-15-2008, 09:45 PM
  5. help with matrix addition
    By rich999 in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2005, 02:30 PM

Tags for this Thread