Array problem need help please, thank you

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.
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
#include<iostream.h>
#include<conio.h>
void main(void)
{
clrscr();
 int a[3][3],num;
 for(int i=0;i<3;i++)
	for(int j =0;j<3;j++)
	       {	cin>>num;
		a[i][j]=num;}
int row[3];
int sum;
	  for(i=0;i<3;i++)       //----for row addition....
	  {     sum=0;
		for(j=0;j<3;j++)
		sum= sum + a[i][j];
	       row[i] = sum;
	  }
	for(i=0;i<3;i++)
	cout<<row[i]<<"  "<<i<<"\n";
int column[3];
	  for(j=0;j<3;j++)         //----for column addition....
	   {    sum=0;
		for(i=0;i<3;i++)
		sum = sum + a[i][j];
		column[j]= sum;
	   }
	   for(i=0;i<3;i++)
	   cout<<column[i]<<" "<<i<<"\n";
	   clrscr();
	   cout<<"\n\nFollowing mattrix ..\n\n";
for( i=0;i<3;i++)
       {	for(j =0;j<3;j++)
		cout<<a[i][j]<<" ";
		cout<<"\n";
       }
 if ((row[0]==row[1] && row[1]==row[2]) && (column[0]==column[1] && column[1]==column[2]))
	{
	cout<<" is magic......\n\nas sum of each row and column is = "<<row[0];
	}
   else
	{   cout<<"not magic...";         }
       getch();
}


hope its ok for you...
iostream.h is ancient and has been replaced with iostream, conio.h is non standard and not necessary at all here, clearing the screen is not what a console program is expected to do, and main has to return an int; it's not legal for it to be of type void.
its working for me...
u can use int main(void)
and return 0....nd this is also an ancient thing.....i using it when i was 20..nw i am 22...i just make it simple for learner.... :p
I'm not saying it won't "work", I'm saying it's wrong to tell beginners to use them. The iostream.h is outdated, void main() is illegal (and although some compilers implement it as an extension, there's nothing "complicated" about returning an int), and also notice that foo() and foo(void) mean the exact same thing in C++. The foo(void) syntax is only there because of C compatibility (in C, those have different meanings).
Last edited on
I also don't see using namespace std; anywhere in that code which could make your "cin" "cout" commands cause an error.

EDIT: Does your code even run arun thakur? Declaring an integer "i" in the for loop on line 7 DOES NOT mean it exists in scope for the following loops. You need to redeclare the variable or else put it in the right scope.

Stuff like this is one of the main reasons we ask people NOT to use Dev-C++.
Last edited on
AFAIK, there were no namespaces in C++ when iostream.h was in use.
@filipe: ??? Then what declares\sets up the input\output streams? I'm not trying to troll you it's annoying me a little that I can't find this header file anywhere to take apart. I'll probably grab it off of Bloodshed's site later.
Last edited on
They're in the global namespace, as they would be in C.
this looks very confusing lol
and that code didnt compile at all
hmmm.....long discussion huh....
guys i use turbo c 3.0 Ver.... nd i write dat code at my own...yesterday in 15 minutes....nd also execute it successfully....
i don't use....any other compiler....bcoz i am not comfortable with them....basically i em java programmer....
and @ Mr. Computer geek ....in c we does't need to redeclare the things....its not java...


i made dis simple program to define the logic....for stated problem...so just stop noising on syntaxes :p.............

closed account (z05DSL3A)
arun thakur,

It does not matter if the code works for you, it is pre standard C++and will not work for most of the people learning C++ with a modern (standard compliant) compiler. Showing 'the logic' only works if the reader can understand the code, you would have been better of using pseudo code to express the logic than clouding the issue with 'old' C++.
hmm.....thanx Grey...i ll' keep dat in my mind...,,!!!
Topic archived. No new replies allowed.