如何从矩阵打印行/列/对角线

时间:2019-09-19 16:31:13

标签: c

我试图让我的程序打印出不符合魔方规则的行,列或对角线,例如, 如果矩阵是

1 9 5

2 4 3

6 8 7

第1行[2,4,3]无效

第2行[6、8、7]不起作用

第0列[1、2、6]不起作用

对角1 [1、4、7]不起作用

我尝试过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;
}

3 个答案:

答案 0 :(得分:1)

您必须使用循环分别打印每个字符。 printf(3)无法打印整数数组。

答案 1 :(得分:1)

在列计算中将sum1尾化似乎很奇怪

//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;
    }
  } 

如果行计算使用的是如下循环

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

列计算循环将被修改

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

加上,您可以简单地编写对角线计算

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

答案 2 :(得分:0)

我认为您明白了:

    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]);
    }