SOMEBODY??!! ANYBODY??!! :/

i have this task to read two 2D arrays and put the sum in third array. i dont get it why this codes not working.please help.

#include<iostream>
using namespace std;
void main ()
{
int arr1[3][3],arr2[3][3],ans[3][3],i,j,k,m;
cout<<"enter the enteries of first 3x3 array"<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>arr1[i][j];
cout<<"enter the enteries of second 3x3 array"<<endl;
for(k=0;k<3;k++)
for(m=0;m<3;m++)
cin>>arr2[i][j];
for(i=0,k=0;i<3,k<3;i++,k++)
for(j=0,m=0;j<3,m<3;j++,m++)
if((i==k)&&(j==m))
{
ans[i][j]=arr1[i][j]+arr2[k][m];

}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<ans[i][j]<<endl;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
void main ()
{
int arr1[3][3],arr2[3][3],ans[3][3],i,j,k,m;
cout<<"enter the enteries of first 3x3 array"<<endl;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cin>>arr1[i][j];
cout<<"enter the enteries of second 3x3 array"<<endl;
for(k=0;k<3;k++)
for(m=0;m<3;m++)
cin>>arr2[i][j];
for(i=0,k=0;i<3,k<3;i++,k++)
for(j=0,m=0;j<3,m<3;j++,m++)
if((i==k)&&(j==m))
{
ans[i][j]=arr1[i][j]+arr2[k][m];

}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<ans[i][j]<<endl;

}


What is the error you get?
Last edited on
1
2
3
for(k=0;k<3;k++)
for(m=0;m<3;m++)
cin>>arr2[i][j];

Remove the k and m from entire program, because you do not need them.
First, code tags.

Second. I've no idea what your doing here? Why do you need k or m?

1
2
3
4
5
6
7
8
9
10
for(i=0,k=0;i<3,k<3;i++,k++)
{
    for(j=0,m=0;j<3,m<3;j++,m++)
    {
        if((i==k)&&(j==m))
        {
            ans[i][j]=arr1[i][j]+arr2[k][m];
        }
    }
}


If i ==k then why don't you just use i?
these are the two 2D arrays. first is [i][j] and second is [k][m]. my problem is i am not geeting the sum in the end.
if i input 1 for all the entries for both 2D arrays i am not getting 2 as the sum.
No. You have three 3*3 arrays: arr1, arr2, ans. The i and j can and should be used as indices for all three here.

Read my previous post. I show there the code that you have wrong.
Topic archived. No new replies allowed.