# matrix addition program using arrays

• 04-10-2011
suryak
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)
• 04-10-2011
laserlight
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.
• 04-10-2011
suryak
please, can you tell give me the correct code of the program.
How to indent the code.
• 04-10-2011
laserlight
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; }```
• 04-10-2011
suryak
// 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");

}
• 04-10-2011
laserlight
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 ;)
• 04-10-2011
suryak
ya I got it... thanks a lot.