New To arrays, Addition

I had to add two arrays with each other so this the initale program thati wrote
it work fine but is there any other solution more optimized where i would use multiple (For) loops.

#include<iostream>
#include<string>

using namespace std;

int main()
{
int A[2][3]=
{
{0,0,0},
{0,0,0},
};

int B[2][3]={{0,0,0},{0,0,0},};
int C[2][3]={{0,0,0},{0,0,0},};

//reading A
cout<<" A = "<<endl;

for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cin>>A[i][j];
}

}
//reading B
cout<<" B = "<<endl;

for( i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cin>>B[i][j];
}

}

for( i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
C[i][j]=A[i][j]+B[i][j];
}
}

// A+B=
cout<<"A+B = "<<endl;
for( i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
Please use [code] blocks.

This code doesn't work on g++ 4.2.1.

So you're adding two 2d arrays' values to each other? Why would you want to use more for loops than you already have? Just curious, but if anything...

-Albatross
Last edited on
int B[2][3]={{0,0,0},{0,0,0},};
int C[2][3]={{0,0,0},{0,0,0},};

What are the second commas doing in these declarations?
1
2
3
4
5


int B[2][3]={{0,0,0},{0,0,0},};
int C[2][3]={{0,0,0},{0,0,0},};


this is intialzing the 2D arrays to 0 ;

and i meant by (for )loop ,is it possible to combine them so i wouldn't have that much
I meant, why is there a comma after the second row is filled with 0's?
Ah that's right it's useless a mistake i made ,but the compiler didn't pointed as an error. heh
Topic archived. No new replies allowed.