gpt4 book ai didi

c - 程序在代码块上运行并且停止后

转载 作者:行者123 更新时间:2023-11-30 21:03:22 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>
#define N 10

float func ( int *arr, int n, int *count )
{
int *p,i;
float sum=0;
p=arr;

for (i=0;i<n;i++)
{
sum += *p;
p++;
}

sum /= n;
count=0;
p=arr;

for(i=0;i<n;i++) //לולאה שעוברת על כל המערך ומוסיפה כל תוכן של איברבמערך שיותר גדול מהממוצע
{
*count+= (*p>sum);
p++;
}
return sum;
}

void main()
{
int i, count=0, arr[N]={0}, n=N;

for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);

printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",func(arr,n,count),count);


}

我需要有关此程序的一些帮助。代码在上面,程序需要从用户那里获取10个成绩,然后打印平均值并计算有多少个成绩比平均值高

最佳答案

您的main函数声明不正确。您应该使用所有警告和调试信息进行编译(例如 gcc -Wall -g 如果使用 GCC ,这可能由您的 Codeblocks IDE 使用...)然后您应该使用调试器(例如 gdb )。你应该测试 scanf(3) 的结果

顺便说一句,

count = 0; // better written as count = NULL;

错误:它正在清除指针。您可能想要

*count = 0;

还有,你的最后一个printf假设某种评估顺序(因为您希望在打印 func 之前调用 count 更改 count ), undefined behavior 也是如此。您需要:

    float avg = func(arr,n,&count);
// from http://stackoverflow.com/a/25382154/841108
printf("The average in class is: %f and the number"
" of students that had the best grades are: %d \n",
avg,count);

请向您的老师展示或告诉您的老师您在 SO 上获得了帮助(无论如何他都会找到)

顺便说一句,我不明白为什么学生要在网上问作业。他们这样做不会学到任何东西,而且他们的老师无论如何都会注意到。

关于c - 程序在代码块上运行并且停止后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25381973/

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