I've done this and it works fine.. |
Actually, it doesn't work fine. Your looping is a little bit messed up.
While you are printing out the elements of your matrix, for EACH ELEMENT you are calculating the total for each row and the total for each column. However, only the last row and last column are saved, and you print it out after each row, stating "The total for each row is:". It's actually only 1 row and 1 column printed multiple times.
For testing purposes, why don't you use a different matrix that is not a magic square so you can verify that you are actually calculating the values of each row and column correctly.
What I think you really want to do is the following:
Your main loop is printing out each row separately. While you are printing out this row, also calculate the total, and then print it at the end. Maybe print some special character like "|" to signify that you are printing a sum rather than a matrix element.
Then when you are done printing out the rows, calculate the sums of the columns. Print each one separately (maybe lining them up below the columns they represent. You can use a row of "_" to signify that you are printing the sums rather than another row.
To calculate the diagonals, you will loop through i OR j--the index can be used for both row and column. Figure out how to use the index by listing the indices that make up the diagonals.