编写一个C语言程序,计算5×5的矩阵的主对角线之和与次对角线之和并
答案:2 悬赏:80 手机版
解决时间 2021-03-18 18:12
- 提问者网友:火车头
- 2021-03-18 03:21
编写一个C语言程序,计算5×5的矩阵的主对角线之和与次对角线之和并
最佳答案
- 五星知识达人网友:渊鱼
- 2021-03-18 04:49
#include
#define LEN 50
void inputMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
printf ("请输入%d行%d列的矩阵:
", row, col);
for (i=0; i
printf ("第%d行:", i+1);
for (j=0; j scanf ("%d", &matrix[i][j]);
}
}
putchar ('
');
return 1;
}
int printMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
for (i=0; i
for (j=0; j printf ("%d ", matrix[i][j]);
}
putchar ('
');
}
putchar ('
');
return 1;
}
int calcMatrix (int matrix[LEN][LEN], int row, int col) {
int mainDiagonalSum, diagonalSum, max, min;
int i,j;
if (row!=col)
return 0;
mainDiagonalSum = diagonalSum = 0;
max = min = matrix[0][0];
for (i=0; i
for (j=0; j if (i==j)
mainDiagonalSum += matrix[i][j];
if (i+j==row-1)
diagonalSum += matrix[i][j];
if (matrix[i][j]>max)
max = matrix[i][j];
if (matrix[i][j] min = matrix[i][j];
}
}
printf ("主对角线之和: %d
", mainDiagonalSum);
printf ("次对角线之和: %d
", diagonalSum);
printf ("最大值: %d
", max);
printf ("最小值: %d
", min);
putchar ('
');
return 1;
}
int main (void) {
int matrix[LEN][LEN], row, col;
row = col = 5;
inputMatrix (matrix, row, col);
printMatrix (matrix, row, col);
calcMatrix (matrix, row, col);
return 0;
}运行结果
为了简便,以下运行示意将矩阵设为3行3列,上方源代码为题主要求的5行5列
#define LEN 50
void inputMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
printf ("请输入%d行%d列的矩阵:
", row, col);
for (i=0; i
for (j=0; j
}
}
putchar ('
');
return 1;
}
int printMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
for (i=0; i
}
putchar ('
');
}
putchar ('
');
return 1;
}
int calcMatrix (int matrix[LEN][LEN], int row, int col) {
int mainDiagonalSum, diagonalSum, max, min;
int i,j;
if (row!=col)
return 0;
mainDiagonalSum = diagonalSum = 0;
max = min = matrix[0][0];
for (i=0; i
mainDiagonalSum += matrix[i][j];
if (i+j==row-1)
diagonalSum += matrix[i][j];
if (matrix[i][j]>max)
max = matrix[i][j];
if (matrix[i][j]
}
}
printf ("主对角线之和: %d
", mainDiagonalSum);
printf ("次对角线之和: %d
", diagonalSum);
printf ("最大值: %d
", max);
printf ("最小值: %d
", min);
putchar ('
');
return 1;
}
int main (void) {
int matrix[LEN][LEN], row, col;
row = col = 5;
inputMatrix (matrix, row, col);
printMatrix (matrix, row, col);
calcMatrix (matrix, row, col);
return 0;
}运行结果
为了简便,以下运行示意将矩阵设为3行3列,上方源代码为题主要求的5行5列
全部回答
- 1楼网友:怙棘
- 2021-03-18 05:48
Harry pointed out the
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯