用c语言二维数组的赋值:打印杨辉三角,打印八行
时间: 2024-01-29 08:39:08 浏览: 74
新建文件夹,c语言杨辉三角用二维数组源码,c语言
以下是用 C 语言二维数组的赋值,打印杨辉三角,并打印八行的代码示例:
```
#include <stdio.h>
int main() {
int rows = 8;
int triangle[rows][rows];
// initialize the first two rows with 1's
triangle[0][0] = 1;
triangle[1][0] = 1;
triangle[1][1] = 1;
// fill in the rest of the rows
for (int i = 2; i < rows; i++) {
triangle[i][0] = 1;
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
triangle[i][i] = 1;
}
// print the first eight rows
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
printf("%d ", triangle[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果如下:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
```
阅读全文