【发布时间】:2020-01-29 20:35:43
【问题描述】:
您好,我有一个代码显示二维矩阵的主对角线下的元素,我还需要显示第二个对角线下的元素。任何想法在循环中操作什么。
// loop to show the elements under the main diagonal
void element_under_diag(int number, int arr[number][number])
{
int i, j;
printf("\nUnder the main diagonal:\n");
for(i=0;i<number;i++){
for(j=0;j<number;j++){
if(i>j)
printf("%d ",arr[i][j]);
}
}
printf("\n");
}
number在主函数中取自用户,是矩阵的行数和列数。
这个循环的结果是这样的:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the main diagonal:
4 7 8
现在我需要这样的输出:
The entered matrix is:
1 2 3
4 5 6
7 8 9
Under the secondary diagonal:
6 8 9
【问题讨论】:
-
非对角线的索引是多少?你能看到那里的图案吗?将模式转化为不等式。
标签: c loops matrix multidimensional-array