0

I am trying to get my program print out the rows, columns or diagonals if they don't equal the magic square rule, for example, if the matrix is

1 9 5

2 4 3

6 8 7

Row 1 [2, 4, 3] doesn't work

Row 2 [6, 8, 7] doesn't work

Column 0 [1, 2, 6] doesn't work

Diagonal 1 [1, 4, 7] doesn't work

I've tried print("%d", matrix[row])

#include <stdio.h> #include <stdbool.h> #include <unistd.h> int main() { //declared variables int size = 3; int matrix[3][3]; int row, column = 0; int sum0, sum1, sum2; int flag = 0; //ask user to input 1-9 and scans it printf("Enter in the values: \n"); for (row = 0; row < size; row++){ for (column = 0; column < size; column++) scanf("%d", &matrix[row][column]); } //enters number into magic square format printf("You entered: \n"); for (row = 0; row < size; row++) { printf("\n"); for (column = 0; column < size; column++) { printf("%d ", matrix[row][column]); } } //diagonal calculations sum0 = 0; for (row = 0; row < size; row++) { for (column = 0; column < size; column++) { if (row == column) sum0 = sum0 + matrix[row][column]; } } //row calculations for (row = 0; row < size; row++) { sum1 = 0; for (column = 0; column < size; column++) { sum1 = sum1 + matrix[row][column]; } if (sum0 == sum1) flag = 1; else { flag = 0; break; } } //column calculations for (row = 0; row < size; row++) { sum1 = 0; for (column = 0; column < size; column++) { sum2 = sum2 + matrix[row][column]; } if (sum1 == sum2) flag = 1; else { flag = 0; break; } } printf("\nAnalyzing...\n"); if (flag == 1) { sleep(2); printf("This is a magic square!\n"); } else { sleep(2); printf("This is not a magic square!\n"); } return 0; } 
2
  • Does your code not work as you expected? If so, then I suggest you debug it. Do you know how to debug your code? Commented Sep 19, 2019 at 18:19
  • What is causing the difficulty here? Commented Sep 19, 2019 at 21:55

3 Answers 3

1

You have to use loops to print each character separately. printf(3) offers no way to print an array of integers.

Sign up to request clarification or add additional context in comments.

Comments

1

it seems weired that initailzied sum1 in colum calculations

//column calculations for (row = 0; row < size; row++) { sum1 = 0; // <= sum2 = 0; maybe for (column = 0; column < size; column++) { sum2 = sum2 + matrix[row][column]; } if (sum1 == sum2) flag = 1; else { flag = 0; break; } } 

and if row calcuations used loop as below

for(row = 0; row < size; row++){ for(column = 0; column < size; column++){ // access to matrix[row][column] } } 

column calculations loop will be revised

for(column = 0; column < size; column++){ for(row = 0; row < size; row++){ // access to matrix[row][column] } } 

plus, you can write the diagonal calculations simply

//diagonal calculations sum0 = 0; for (int diag = 0; diag < size; diag++) { sum0 += matrix[diag][diag]; } 

Comments

0

I think you get the logic:

 const int x = 5; const int y = 7; int matrix[x][y]; for (int j = 0; j < x; ++j) { for (int i = 0; i < y; ++i) { matrix[j][i] = j + i; printf("%3d ", matrix[j][i]); } putchar('\n'); } puts("\nPrint a column: "); for(int i = 0; i < x; i++) { printf("%3d ", matrix[i][0 /*constant*/]); } puts("\nPrint a row: "); for (int k = 0; k < 7; ++k) { printf("%3d ", matrix[0/*constant*/][k]); } puts("\nPrint a diagonal"); for (int l = 0, m = 0; l < x && m < y; ++l, ++m) { printf("%3d ", matrix[l][m]); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.