gpt4 book ai didi

c - 为什么我不能让这个简单的 C 程序运行?

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

我正在制作一个简单的控制台程序,该程序应该使用户能够输入一个数组,并且该程序应该显示所有数字的算术平均值,但无论我尝试什么,我都无法让它工作。这里有人能指出为什么这不起作用吗?

(这是塞尔维亚语,但您可能可以看到什么是什么。)

编辑:运行此代码时出现这些错误:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
F:\rastko\Documents\Code\C\Test1\main.c||In function 'main':|
F:\rastko\Documents\Code\C\Test1\main.c|49|warning: passing argument 1 of 'UnesiElementeNiza' makes pointer from integer without a cast [-Wint-conversion]|
F:\rastko\Documents\Code\C\Test1\main.c|19|note: expected 'int *' but argument is of type 'int'|
F:\rastko\Documents\Code\C\Test1\main.c|51|warning: passing argument 1 of 'AritmetickaSredinaNiza' makes pointer from integer without a cast [-Wint-conversion]|
F:\rastko\Documents\Code\C\Test1\main.c|29|note: expected 'unsigned int *' but argument is of type 'int'|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

然后程序崩溃了。我不知道这意味着什么,而且在技术演讲方面我的英语不是最好的,因为我正在学习如何用塞尔维亚语编码。

#include <stdio.h>
#include <stdlib.h>
#define MAX 41

int BrojElemenataNiza(unsigned int brElem)
{

if (brElem <= MAX){
return brElem;
}
else {
return 69;
}
}

void UnesiElementeNiza(int niz[0], unsigned int brElem)
{
for (unsigned int i = 0; i < brElem; ++i)
{
printf("niz[%u] = ", i);
scanf("%d", &niz[i]);
}

}

double AritmetickaSredinaNiza(int niz[0], unsigned int brElem)
{
int j = 0;
for (unsigned int i = 0; i < brElem; ++i)
{
j = j + niz[i];
}
return j/brElem;
}

int main()
{
int n, m, j, niz[0];

printf("unesite broj elemenata niza\n");
scanf("%d", &n); //unos broja elemenata niza
int brElem = BrojElemenataNiza(n);

printf("unesite elemente niza\n"); //unos elemenata niza
UnesiElementeNiza(niz[brElem], brElem);

double arSredina = AritmetickaSredinaNiza(niz[0], brElem);
printf("aritmeticka sredina niza je ");
printf("%lf", arSredina);


return 0;
}

最佳答案

这个怎么样,抱歉,是英语,我不会说塞尔维亚语。

#include "stdlib.h"
#include "string.h"
#include "stdio.h"

#define MAX 41

int check(unsigned int brElem)
{

if (brElem <= MAX){
return brElem;
}
else {
return 69;
}
}

void num_to_array(int *niz, unsigned int brElem)
{
for (unsigned int i = 0; i < brElem; ++i)
{
printf("niz[%u] = ", i);
scanf("%d", &niz[i]);
}

}

double average(int niz[0], unsigned int brElem)
{
int j = 0;
for (unsigned int i = 0; i < brElem; ++i)
{
j = j + niz[i];
}
return j/brElem;
}

int main()
{
int n, m, j, niz[MAX];

printf("how many numbers ?\n");
scanf("%d", &n); //unos broja elemenata niza
int brElem = check(n);

printf("enter numbers\n"); //unos elemenata niza
num_to_array(niz, brElem);

double arSredina = average(niz, brElem);
printf("aritmeticka sredina niza je ");
printf("%lf", arSredina);


return 0;
}

关于c - 为什么我不能让这个简单的 C 程序运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59166357/

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