Logical Problem with Arrays, please need some assistance

Write a few lines of code (not a separate function) to do the following:



1. Ask the user for 9 integers as the entries of a 3 x 3 table.

2. Fill the array with the 9 integers.

3. Print the array in the shape of a 3 x 3.

4. If every row and column of the table has the same sum, print the message: MAGIC.



For example:

Enter 9 entries of a 3 x 3 table: 10 14 18 15 16 11 17 12 13

10 14 18

15 16 11

17 12 13

MAGIC



This is magic because each row and each column has a sum of 42.

Thats the question above and i tried coding it and this is what i have so far, but i keep getting error saying obsolete r not sure what that means.


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;
        
int main()
{

int table[3][3],num;   
        for(int r=0; r<=2; r++)
            for(int c=0; c<=2; c++) 
            {
              cout<<"Enter 9 integers"<<r;
              cin>>num;   
              table[r][c]=num;
            }
int row[3];
int sum;   
for(int r=0; r<=2; r++)   
               sum=0;
           for(int c=0; c<=2; c++)
           {
               sum+=table[r][c];
               row[r]=sum;
           }
           for(int r=0; r<=2; r++){
               cout<<row[r]<<" "<<r<<endl;}
int col[3];
for(int c=0; c<=2; c++)
           {    sum=0;
                for(int r=0; r<=2; r++){
                   sum+=table[r][c];
                   col[c]=sum;
                }
           }
           for(int r=0; r<=2; r++)
               
                for(int c=0; c<=2; c++)
 {
                   cout<<table[r][c]<<" ";
                }
                    cout<<endl;
                   
return 0;
}


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;
}
}



S,
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
            
int main()
{

        int table[3][3],num;   
        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>>num;
              table[r][c]=num;
            }  
        }   
int row[3];
int sum;
 for(int r=0; r<=2; r++)
        {      sum=0;
           for(int c=0; c<=2; c++)
           {
               sum+=table[r][c];
               row[r]=sum;
           }   
               cout<<row[r]<<" "<<r<<endl;
         } 
int col[3];
for(int c=0; c<=2; c++)
           {    sum=0;
                for(int r=0; r<=2; r++)
                {
                   sum+=table[r][c];
                   col[c]=sum;
                }
                 cout<<col[c]<<" "<<c<<endl;
           }
            for(int r=0; r<=2; r++)
{
                for(int c=0; c<=2; c++)
                {
                   cout<<table[r][c]<<" "<<endl;
                }
            }
if((row[0]==row[1] && row[1]==row[2]) && (col[0]==col[1] && col[1]==col[2]))
       {
       cout<<"MAGIC";
       }
 else { cout <<" not magic ";}
                
return 0;
}
                 

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
try deleting line 12 and replacing line 13 with: cin>>table[r][c];
oh yea bill i did that but heres an updated code ive been working on almost there but not quite

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace 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 :)
and that repetitive NOT MAGIC, i just want it only once
closed account (D80DSL3A)
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.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;
          
int main()   
{
           
        int table[3][3],num;
        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<<endl;
         }
int row[3];  
int sum;   
 for(int r=0; r<=2; r++)
        {      sum=0;
           for(int c=0; c<=2; c++)   
           {   
               sum+=table[r][c];
               row[r]=sum;
           }
               cout<<row[r]<<" "<<r<<endl;
         }   
int col[3];
for(int c=0; c<=2; c++) 
           {    sum=0;
                for(int r=0; r<=2; r++)
                {
                  sum+=table[r][c];
                   col[c]=sum;
                }
                 cout<<col[c]<<" "<<c<<endl;
           } 

if((row[0]==row[1] && row[1]==row[2]) && (col[0]==col[1] &&
col[1]==col[2]))
                
       cout<<"MAGIC"<<endl;
 else  cout <<" not magic "<<endl;
                 
return 0;
}


my output is
10 14 18 

15 16 11 

17 12 13 

42 0
42 1
42 2
42 0
42 1
42 2
MAGIC




i dont understand why it prints out the 42, i dont want that in there
nvm i just got it basically what i did was removed the cout part so it doesnt show the 42, thanks again
Topic archived. No new replies allowed.