sum the rows and columns of a magic square

I wrote this code(with a little help) that makes a magic square. Now I need to sum the rows and columns and diagonals. I can't figure out what i'm doing wrong.

Lines 57 thru 63 are my attempt at doing so. Can someone help me figure out what i'm doing wrong? I've tried adjusting everything I can think of with no luck.



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
#include <iostream>
#include <iomanip>

using namespace std;

int size(bool flag)
	{
	int test;
	if(flag)
	{
		do
		{
			test=(int)(double(rand())/double(RAND_MAX)*14.0+3.0);
		}while(test%2==0);
		return test;
	}
		else
			return 7;
}

int main()
{
	int n = 7;
	int row, col, size;
	
	int magic[19][19];

	

	for(int j=0 ; j<n ; j++)
	{
		for(int k=0 ; k<n ; k++)
		{
			magic[j][k] = 0;
		}
	}

	row=1; col=(n+1)/2;
	magic[row-1][col-1]=1;
	
	for(int i=2 ; i<=(n*n) ; i++)
	{
		row-=1; col-=1;
		if(row==0 && col==0)
		{
			col++; row+=2;
		}
		else if(row==0) row=n;
		else if(col==0) col=n;
		else if(magic[row-1][col-1]!=0)
		{
			col++; row+=2;
		}
		magic[row-1][col-1]=i;
	}
		
			for (row=1; row<=n; row++)
				for(col=1; col<=n; col++)
					magic[row][n+1]+=magic[row][col];

			for(col=1; col<=n; col++)
				for(row=1; row<=n; row++)
					magic[n+1][col] += magic[row][col];
	
			
	for(int r=0; r<(n+1) ; r++)
	{
		cout<<endl;
		for(int c=0; c<(n+1) ; c++)
		{
		cout << setw (5) << magic[r][c];
		}
		cout<<endl;
	}
	
	return 0;
}
here is my concept for my last time project
your looping not wrong. but can create a new variable instead you use magic[n+1]

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
bool magicSquare(int **matrix,int dimension)  // add rows, columns, and diagonals
   { 
		int firstSum = 0, sum;
		bool magic = true;

		for (int r = 0; r < dimension; r++) // add 1st column for a comparison
          firstSum += matrix[r][1]; 
		for (int r = 1; r < dimension; r++) // row loop first when adding rows
        { 
			sum = 0;
            for ( int c = 0; c < dimension; c++)
               sum += matrix[r][c];  // add row
             if ( sum != firstSum)  // check for magic failure
                return (false);  // not magic
           }
                 
         for ( int c = 0; c < dimension; c++)   // column loop first when adding columns
           { 
				sum = 0;
				for (int r = 0; r < dimension; r++)
                  sum += matrix[r][c];   // add columns
                  if ( sum != firstSum)  // check for magic failure
                   return (false);  // not magic
            }
         sum = 0;
         for (int r = 0; r < dimension; r++)  
              sum += matrix[r][r];   // add front diagonal
            if ( sum != firstSum)  // check for magic failure
               return (false);  // not magic
          sum = 0;
          for (int r = 0; r < dimension; r++)  
              sum += matrix[r][dimension - r - 1];   // add back diagonal
             if ( sum != firstSum)  // check for magic failure
              return (false);  // not magic
          else
              return (true);
}  // end magicSquare function 
Topic archived. No new replies allowed.