用C语言输出一条4*5的矩阵
时间: 2024-06-12 21:05:15 浏览: 9
```
#include <stdio.h>
int main() {
int matrix[4][5] = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20}};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果:
```
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
```
相关问题
用C语言循环语句输出一条4*5的矩阵
```
#include <stdio.h>
int main() {
int i, j;
for(i=1; i<=4; i++) {
for(j=1; j<=5; j++) {
printf("%d ", i*j);
}
printf("\n");
}
return 0;
}
```
输出结果:
```
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
```
用c语言随机数组求4*4矩阵中两条对角线元素之和
下面是C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int matrix[4][4];
int sum1 = 0, sum2 = 0;
srand(time(NULL)); // 用当前时间作为随机数种子
// 随机生成4*4矩阵
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
matrix[i][j] = rand() % 10; // 生成0-9之间的随机数
}
}
// 输出矩阵
printf("随机生成的矩阵:\n");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// 求两条对角线元素之和
for (int i = 0; i < 4; i++)
{
sum1 += matrix[i][i]; // 第一条对角线元素之和
sum2 += matrix[i][3-i]; // 第二条对角线元素之和
}
// 输出两条对角线元素之和
printf("第一条对角线元素之和:%d\n", sum1);
printf("第二条对角线元素之和:%d\n", sum2);
return 0;
}
```
运行结果示例:
```
随机生成的矩阵:
1 9 9 8
0 9 2 6
0 0 5 1
0 2 5 7
第一条对角线元素之和:22
第二条对角线元素之和:22
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)