I am tasked in a school assignment to create a magic square. I was given a peice of code that is was supposed to use. So I'm having trouble figuring out how to place the numbers in the magic square. Something doesn't seem right with the code I was given. I think maybe it was incomplete or maybe wrong. I've been working on this all day and it's time to move on and start studying for the rest of my finals. Can anyone point me in the direction of my mistake. I'm not asking for a direct answer but maybe something along the lines of a good push in the right direction would greatly be appreciated. My code outputs the correct number of numbers they just aren't continuing to add numbers to the square.
I beleive my mistake is somewhere in between lines 22-42.
Your code is almost perfect. There is a few issues with lines 24-41. Some times when you assign a value to the matrix, you're jumping out of bounds. The values of row/col are from 1-7. Arrays allow access from 0-6.
To rectify your problem, wherever you assign a value to matrix, you need to assign it to row - 1 and col - 1.
ok, I had to make one more change to the second for statement where i<(n*n)
to read i<=(n*n)
and now it's perfect.
I can't tell you how much I appreciate the help. Now all I have left to do is sum the rows, columns and diagonals, set up the randon size generator, and output it to a file. Shouldn't take more than a couple hours, if I'm lucky. Thanks so much for the gentle shove and putting me back on track.