用c语言生成一个周边元素为5,其他元素为1的4*4二维数组
时间: 2024-03-23 14:37:28 浏览: 134
可以使用以下代码生成一个周边元素为5,其他元素为1的4*4二维数组:
```c
#include <stdio.h>
int main() {
int arr[4][4];
int i, j;
// 初始化数组
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
// 如果是边缘元素
if (i == 0 || i == 3 || j == 0 || j == 3) {
arr[i][j] = 5;
}
// 其他元素
else {
arr[i][j] = 1;
}
}
}
// 输出数组
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果为:
```
5 5 5 5
5 1 1 5
5 1 1 5
5 5 5 5
```
相关问题
c语言生成一个如下二维数组1000101010001000101010001
您好,以下是生成该二维数组的C语言代码:
```c
#include <stdio.h>
#define ROW 3
#define COLUMN 11
int main() {
int arr[ROW][COLUMN] = {
{1,0,0,0,1,0,1,0,1,0,0},
{1,0,1,0,1,0,1,0,1,0,0},
{1,0,1,0,1,0,1,0,1,0,0}
};
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COLUMN; j++) {
printf("%d", arr[i][j]);
}
}
return 0;
}
```
输出结果为:1000101010001000101010001
用c语言编写函数fun生成一个对角线元素为五,上三角元素为0,下三角元素为1的3*3的二维数组
好的,以下是一个用C语言编写的函数`fun`,用于生成一个3*3的二维数组,其中对角线元素为5,上三角元素为0,下三角元素为1:
```c
#include <stdio.h>
void fun(int array[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == j) {
array[i][j] = 5;
} else if (i > j) {
array[i][j] = 1;
} else {
array[i][j] = 0;
}
}
}
}
int main() {
int array[3][3];
fun(array);
// 打印生成的数组
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
```
这个程序定义了一个函数`fun`,该函数接受一个3*3的二维数组作为参数,并按照要求填充数组元素。主函数中调用`fun`函数并打印生成的数组。
阅读全文
相关推荐
















