Apr 21, 2010 at 8:28pm UTC
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;
}
Apr 21, 2010 at 8:33pm UTC
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 Apr 21, 2010 at 8:33pm UTC
Apr 21, 2010 at 8:34pm UTC
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?
Apr 21, 2010 at 8:50pm UTC
I meant, why is there a comma after the second row is filled with 0's?
Apr 21, 2010 at 8:55pm UTC
Ah that's right it's useless a mistake i made ,but the compiler didn't pointed as an error. heh