c - 在 C 中的 2d 矩阵中的第二个对角线下打印元素
问题描述
您好我有一个代码显示二维矩阵的主对角线下的元素,我还需要显示第二个对角线下的元素。任何想法在循环中操作什么。
// 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
解决方案
如果一个数组是用 N * N 个元素定义的,那么 if 语句中的条件可能看起来像
if ( N - i - 1 < j ) printf( "%d ", a[i][j] );
推荐阅读
- r - 将某些图与子图组合后,“填充”标签无法正常工作
- spring - Netflix-Zuul 挥之不去的 ThreadLocal 与 RequestContext 过滤器
- r - 使用 tuber 包创建包含 YouTube 视频详细信息的数据框
- html - 如何从另一个 div 获得悬停效果?
- ruby-on-rails - --webpacker=stimulus 是否会以某种方式禁用 form_with remote:true?
- postgresql - Postgres-将时间格式从 24 小时更改为 12 小时?
- jquery - 我尝试在 jquery ajax 中使用它
- apache-spark - 从 `String` 对生成 `VertexId`
- sql - MS Access:显示客户的单个记录,其中列出了最近的联系人
- windows - 是否有可能(截至 2018 年 6 月)在 Windows 上成功运行 Google Assistant SDK 和相关功能?