what is wrong with my program? And how can i fix it? I am trying to write a program to test whether the given matrix (i hard code it) is a magic square or not. It does successfully compile so there isn't any syntax error. My guess is my last IF loop.
which is
if ((RT == CT)&&(RT == DT)&&(RT!=CT)&&(CT!=DT))
cout<<"this is a magic sqaure";
else
cout<<"this is not a magic sqaure";
but if this if loop is wrong what am i suppose to do?
You have more problems than that last if statement. None of your totals will come out right.
For finding the row and column totals you have just:
1 2
RT[size];
CT[size];
These lines don't do anything. No assignment is made.
Your code for finding the diagonal totals looks good except you will get garbage values because DT[] is not initialized (the elements should all be initially 0).
You could initialize your totals arrays as you did for the matrix array like so:
1 2 3
int RT[3] = {0,0,0};
int CT[3] = {0,0,0};
int DT[2] = {0,0};
Next, use this line for finding the row totals: RT[row] += matrix[row][column];
Use similar code to find the column totals.
I would tell you where these lines go but you aren't using code tags so I can't cite line #'s in your code.
Lastly, your if statement at the end will need major work. For example RT[size] is not a valid element - the index is beyond the array bounds. Also, RT == DT is comparing memory addresses, not the values stored in the array. Good luck, and please start using code tags.
first of all, how to use code tag?
I change my code a bit as you stated.
for the "RT[row] += matrix[row][column];" 1) i don't understand it 2)i don't know where to put it.
And for the rows total you mentioned, what do you mean by row total? you mean the total of rows? or the sum of each rows?
e.g
1 2 3
4 5 6
7 8 9
sum of each rows for matrix is sumRT={1 +2 +3}, {4+5+6},{7 + 8 +9}
Here's what i think i did.
I stored the rows into a 1 dimensional array where RT = [{1 , 2 , 3}, {4 , 5,6},{7 , 8 ,9}]
and CT = [{1 , 4 , 7}, {2 , 5, 8},{3 , 6 ,9}]
and DT= [{1, 5, 9} , {7, 5, 3}]
What i want is two things,
1) is the sum of the sumRT sumCT, sumDT, which i mentioned {1 +2 +3}, {4+5+6},{7 + 8 +9}
2) then a if loop that will test whether for each element of array for sumRT sumCT,sumDT will be the same
3) then another if loop that test each element of RT, CT, DT != to the same.
4) lastly another if loop to test each element of cout<<matrix[row][column] is within 1 to 9.
About code tags. When writing a post look to the right under "Format:". The one that looks like <> is for code tags. Click it to get the tags and then place your code between them.
I had thought that RT stood for Row Totals, same as sumRT in your last post. Same for CT and DT.
I thought you wanted RT[0] = 1+2+3, RT[1] = 4+5+6 and RT[2] = 7+8+9.
This line RT[row] += matrix[row][column]; would produce those totals. I meant for it to be placed on line 5 below:
1 2 3 4 5
for(int row = 0; row<3; row++); //row
{//start of loop to sum of all the rows using columns
for (int column = 0; column<3;column++)
RT[row] += matrix[row][column];// replacing existing line RT[3];
The other one for column totals would go on line 5 below:
1 2 3 4 5 6 7
for (int column = 0; column<3;column++);//column
{//start of loop for columns
for(int row = 0; row<3; row++)
CT[column] += matrix[row][column];// replacing CT[3];
}//end of loop of columns.
The above nested for loops would produce the numbers you gave for sumRT and sumCT and store them in the 3 elements of RT and CT.
Here's what i think i did.
I stored the rows into a 1 dimensional array where RT = [{1 , 2 , 3}, {4 , 5,6},{7 , 8 ,9}]
and CT = [{1 , 4 , 7}, {2 , 5, 8},{3 , 6 ,9}]
and DT= [{1, 5, 9} , {7, 5, 3}]
That didn't occur to me because it's not possible to store 9 values in an array with only 3 elements (or 6 values in 2 elements). Your code for DT actually should give:
DT[0] = 1+5+9 and DT[1] = 7+5+3
So I thought that was what you wanted to do with CT and RT as well. Sorry if I got that wrong.
I'll work with you on more of this if we can get these totals straightened out.
for(int row = 0; row<3; row++); //row
{//start of loop to sum of all the rows using columns
for (int column = 0; column<3;column++)
RT[row] += matrix[row][column];// replacing existing line RT[3];
it gave me warning C4258: 'row' : definition from the for loop is ignored: the definition from the enclosing scope is used. same goes for the column. Note: i did declare rows and column.
For now let's ignore that problem because it successfully compile even though it state there's a problem.
So prevoiusly you said my "if" statement isn't doing what i want it to do
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for(int row=0;row<3;row++)
{ //start of display the matrix for the user
for(int column=0;column<3;column++)
{
if ((RT[rows] == CT[column])&&(DT == RT))
cout<<"this is a magic sqaure";
else
cout<<"this is not a magic sqaure";
break;
}
I delete the "(RT == DT)&&(RT!=CT)&&(CT!=DT))" part because i don't want the SUM of each column and row is not equal, i want each element. But what you taught me is to declare the SUM. So how could i compare each element of rows and columns to check it's uniqueness of a magic square?
I read up on magic squares, since I wasn't sure what they are! I think you have 2 tasks left ( since we have found the sums now).
These are:
1) Verify that all row sums, column sums and the two diagonal sums are equal to the same value (15 for a 3x3 square).
2) Verify that the matrix elements contain the values 1 through size*size (1-9 for a 3x3 square).
Are you still having trouble with both of these?
I have written a program for this so I know the following approach will work to check if all sums are equal. Since this is probably a homework assignment, I can't just give it to you though (sorry!).
in steps:
task #1) choose one of the sums as the common value to compare all of the others to. For example: int sum = DT[0];
use a bool variable to detect failure of any equality check: bool allEqual = true;
check the other diagonal sum first
1 2
if( DT[1] != sum )
allEqual = false;
Then, loop through the 3 cases for RT[0] through RT[2] and make the same test for each. Do the same for the 3 CT values. If allEqual is still true after all this then they are "all equal".
Task #2 is more involved though. I'll outline my solution method for you if you can get task #1 down.