Magic Square

My knowledge about C++ is very limited,
just keep modifying the source code from internet to fix the problem.

The expected output is this(just one example):
1
2
3
4
5
6
Please input the size of the square:
Please input the square row by row:
Sum of rows: 23 15 7
Sum of columns: 15 15 15
Sum of diagonals: 15 15
| This is NOT a magic square. |
\-----------------------------/


But my output is this:
1
2
3
4
5
6
Please input the size of the square:
Please input the square row by row:
Sum of rows:
Sum of columns:
Sum of diagonals:
| This is NOT a magic square. |
\-----------------------------/


how can i print out the sum of rows, columns and diagonals?
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<conio.h>
#include<stdio.h>
#define max 25
int square[max][max];
int check(int);
 
int check(int n)
{
    int column,row,k,sum,tsum=0;
    sum=(int)(( n * ( n * n + 1)))/2;
 
    /* checking sum of each row is same */
    printf("\nSum of rows: ");
    for(row=0;row<n;row++)
   {
          tsum=0;
          for(column=0;column<n;column++)
         {
             tsum=tsum+square[column][row];
         }
         if(tsum!=sum)
               return 0;
    }
     /* checking sum of each coloumn is same */
    printf("\nSum of columns: ");
    printf("\nSum of diagonals: ");
    for(column=0;column<n;column++)
   {
        tsum=0;
        for(row=0;row<n;row++)
       {
             tsum=tsum+square[column][row];
       }  
  
        if(tsum!=sum)
          return 0;
   }
 /* checking sum of diagonal is same */
     for(column=0;column<n;column++) 
     {
           tsum=0;
           for(row=n-1;row>=0;row--)
          {
                tsum=tsum+square[column][row];
         }
         if(tsum!=sum)
          return 0;
     }
      return 1;   
/* checking sum of diagonal is same */
     tsum=0; 
     for(column=0;column<n;column++)
     {
           tsum=tsum+square[column][column];
     }
      if(tsum!=sum) 
            return 0;
}
 
void main()
{
    int column,row,k,size,flag;
     //clrscr();
     printf("\nPlease input the size of the square: ");
     scanf("%d",&size);
     printf("\nPlease input the square row by row: \n",size * size);

      for(column=0;column<size;column++)
     {
           for(row=0;row<size;row++)
          {
             scanf("%d",&square[column][row]);
         }
    } 
     flag=check(size);
    if(flag)
    {
 
          printf("\n /-------------------------\\ ");
          printf("\n | This is a magic square. |");
          printf("\n \\-------------------------/ ");
    }
    else
    {
         printf("\n /-----------------------------\\ \n");
         printf("\n | This is NOT a magic square. | \n");
         printf("\n \\-----------------------------/ \n ");
    }
 }   
Topic archived. No new replies allowed.