gpt4 book ai didi

c - 用于定义和打印矩阵的所有元素的函数

转载 作者:行者123 更新时间:2023-11-30 19:16:38 26 4
gpt4 key购买 nike

我正在研究 C 语言中的函数,我想创建一个函数来将矩阵的所有元素设置为相同的值,然后使用另一个函数打印矩阵。是否可以?这是我写的代码[编辑]:

#include <stdio.h>
#include <stdlib.h>
#define x 79
#define y 24

int def_matrice(int matrice[y][x], int valore);
int print_matrice(int matrice[y][x]);

int main()
{
int i = 0, j = 0, matrice[y][x];

for(i = 0; i < y; i++)
{
for(j = 0; j < x; j++)
{
matrice[i][j] = 32;
}
}

for(i = 0; i < y; i++)
{
for(j = 0; j < x; j++)
{
printf("%c", matrice[i][j]);
}
printf("\n");
}

def_matrice(matrice[y][x], 89);
print_matrice(matrice[y][x]);

return 0;
}

int def_matrice(int matrice[y][x], int valore)
{
int i = 0, j = 0;
for(i = 0; i < y; i++)
{
for(j = 0; j < x; j++)
{
matrice[i][j] = valore;
}
}
return 0;
}

int print_matrice(int matrice[y][x])
{
int i = 0, j = 0;
for(i = 0; i < y; i++)
{
for(j = 0; j < x; j++)
{
printf("%c", matrice[i][j]);
}
printf("\n");
}
return 0;
}

但是当我尝试编译时,出现以下错误[已编辑]:

|30|error: passing argument 1 of 'def_matrice' makes pointer from integer without a cast|
|6|note: expected 'int (*)[79]' but argument is of type 'int'|
|31|error: passing argument 1 of 'print_matrice' makes pointer from integer without a cast|
|7|note: expected 'int (*)[79]' but argument is of type 'int'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

最佳答案

修复原型(prototype)(在函数声明和函数定义处):

int def_matrice(int matrice[y][x], int valore)
int print_matrice(int matrice[y][x])

然后修复您的函数调用:

def_matrice(griglia, 89);
print_matrice(griglia);

然后,您需要在 def_matriceprint_matrice 中声明 ij

然后 def_matriceprint_matrice 的参数名称为 matrice,但在函数中使用名称 griglia

关于c - 用于定义和打印矩阵的所有元素的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29782708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com