Hi, that error is because in the 3th for (for(int r=0; r<=2; r++) sum=0;), you miss to open the {.
So that for is only executing "sum=0;" r=0..2 times..
Probably you want to do:
for(int r=0; r<=2; r++) {
sum=0;
for(int c=0; c<=2; c++)
{
sum+=table[r][c]; // here it was the problem befor adding the {} (this r it is not in the scope of the for)
row[r]=sum;
}
}
I need help with this its not outputing what i want, i cant get it in a 3 X 3 table. heres the output i get
Enter 9 entries of a 3 x 3 table: 10
Enter 9 entries of a 3 x 3 table: 14
Enter 9 entries of a 3 x 3 table: 18
15Enter 9 entries of a 3 x 3 table: 15
Enter 9 entries of a 3 x 3 table: 16
Enter 9 entries of a 3 x 3 table: 11
Enter 9 entries of a 3 x 3 table: 17
Enter 9 entries of a 3 x 3 table: 12
Enter 9 entries of a 3 x 3 table: 13
42 0
1542 1
42 2
1542 0
42 1
42 2
10
14
18
1515
16
11
17
12
13
NOT MAGIC
#include <iostream>
usingnamespace std;
int main()
{
int table[3][3];
for(int r=0; r<=2; r++)
{ for(int c=0; c<=2; c++)
{
cout<<"Enter 9 entries of a 3 x 3 table: ";
cin>>table[r][c];
}
}
for(int r=0; r<=2; r++)
{
for(int c=0; c<=2; c++)
{
cout<<table[r][c]<<" ";
}
cout<<endl;
}
int row[3];
int col[3];
for( int r=0; r<=2; r++)
{ int sum=0;
for(int c=0; c<=2; c++)
{ sum+=sum+row[r];
sum+=sum+col[c];
if((row[0]==row[1] && row[1]==row[2]) && (col[0]==col[1] &&
col[1]==col[2]))
cout<<"MAGIC";
else cout <<" NOT MAGIC "<<endl;}
}
return 0;
}
my output is as follows
10 14 18
15 16 11
17 12 13
NOT MAGIC
NOT MAGIC
NOT MAGIC
NOT MAGIC
NOT MAGIC
NOT MAGIC
NOT MAGIC
NOT MAGIC
NOT MAGIC
The answer is suppose to be MAGIC but i dont know how to put the sum in it so basically the sum up there i believe wasnt even used up, almost there but need some help for you guys please :)
That's odd. I ran your earlier code http://www.cplusplus.com/forum/general/40765/#msg219888
and it works perfectly. Your earlier code is correct.
Did you make some kind of data entry error? Those extra 15's in the output are weird but I didn't get them when I ran your program.
Your newest version is messed up. These lines are wrong:
1 2
sum+=sum+row[r];
sum+=sum+col[c];
Go back to the earlier version and add your table printing code to it.