设有4*4的方阵 其中的元素由键盘输入 分别求出主对角线上元素之和,辅助对角线元素之积,方阵中最大的元素的代码
时间: 2023-03-29 21:03:20 浏览: 169
主对角线上元素之和的代码:
```
#include <stdio.h>
int main() {
int matrix[4][4];
int sum = ;
for (int i = ; i < 4; i++) {
for (int j = ; j < 4; j++) {
scanf("%d", &matrix[i][j]);
if (i == j) {
sum += matrix[i][j];
}
}
}
printf("主对角线上元素之和为:%d\n", sum);
return ;
}
```
辅助对角线元素之积的代码:
```
#include <stdio.h>
int main() {
int matrix[4][4];
int product = 1;
for (int i = ; i < 4; i++) {
for (int j = ; j < 4; j++) {
scanf("%d", &matrix[i][j]);
if (i + j == 3) {
product *= matrix[i][j];
}
}
}
printf("辅助对角线元素之积为:%d\n", product);
return ;
}
```
方阵中最大的元素的代码:
```
#include <stdio.h>
int main() {
int matrix[4][4];
int max = -100000;
for (int i = ; i < 4; i++) {
for (int j = ; j < 4; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] > max) {
max = matrix[i][j];
}
}
}
printf("方阵中最大的元素为:%d\n", max);
return ;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)