Like Tree1Likes
  • 1 Post By suryak

matrix addition program using arrays

This is a discussion on matrix addition program using arrays within the C Programming forums, part of the General Programming Boards category; PHP Code: /* program to perform martix addition */ # include <stdio.h> void main  () { int i , j ; int a [ 2 ][ 2 ], b [ 2 ...

  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)
    mpconline likes this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,308
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    22,308
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11: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, 03: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, 10: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


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