请问下为何输入了数据无法运行出来

super_stupid 2019-05-24 08:39:41
#include<stdio.h>
#define ARR_SIZE 30
int ReadScore(long num[], float score[]);
int GetFail(long num[], float score[]);
int GetAboveAver(long num[], float score[]);
void GetDetail(float score[]);
int x,y,n;
void main ()
{
long num[ARR_SIZE];
float score[ARR_SIZE];
n=ReadScore( num, score);
printf("不及格学生:");
x=GetFail( num, score);
printf("不及格人数为: %d \n",x);
printf("及格学生:");
y=GetAboveAver( num, score);
printf("及格人数为: %d \n",y);
GetDetail( score);
}

int ReadScore(long num[], float score[])
{
int i;
n=0;
do
{
i=0;
scanf("%d",num[i]);
scanf("%f",score[i]);
n++;
i++;
}while(score[i-1]>0);
return(n);
}

int GetFail(long num[], float score[])
{
int i;
x=0;
for(i=0;i<n;i++)
{
if(score[i]<60)
x++;
printf(" %d ", num[i]);
}
return(x);
}
int GetAboveAver(long num[], float score[])
{
int i;
float t;
y=0;
for(i=0;i<n;i++)
{
t+=score[i]/n;
if(score[i]>=60)
y++;
printf(" %d ", num[i]);
}
printf("全班平均分: %f ",t);
return(y);
}
void GetDetail(float score[])
{
int i,a, b;
float u,v,w;
u=(float)x/n;
for(i=0;i<n;i++)
{
if(score[i]>=60||score[i]<80)
a++;
if(score[i]>=80||score[i]<=100)
b++;
}
v=(float)a/n;
w=(float)b/n;
printf("不及格学生占百分比: %f \n",u);
printf("60分到80占百分比: %f \n",v);
printf("80分以上占百分比: %f \n",w);
}
...全文
54 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
super_admi 2019-05-24
  • 打赏
  • 举报
回复
我是看到你的名字才进来看看的,结果看到你贴的代码,瞬间就不想看了……
CaptainXue 2019-05-24
  • 打赏
  • 举报
回复
你的程序太多问题了,不知道你写完后有没有认真读过你的程序。有语法问题、逻辑问题。可以看看这几篇文章,对你会有帮助;
程序找错的方法:https://blog.csdn.net/weixin_43956598/article/details/90062550
逻辑思维的重要性!https://blog.csdn.net/weixin_43956598/article/details/90049508
修改后的代码和标注如下,希望你好好自己改改程序:


#include<stdio.h>
#define ARR_SIZE 30
int ReadScore(long num[], float score[]);
int GetFail(long num[], float score[]);
int GetAboveAver(long num[], float score[]);
void GetDetail(float score[]);
int x,y,n;
int main () { //新的C++标准
long num[ARR_SIZE];
float score[ARR_SIZE];
n=ReadScore( num, score);
printf("不及格学生:");
x=GetFail( num, score);
printf("不及格人数为: %d \n",x);
printf("及格学生:");
y=GetAboveAver( num, score);
printf("及格人数为: %d \n",y);
GetDetail(score);
return 0;
}
int ReadScore(long num[], float score[]) {
int i;
n=0;
i=0;//应该在循环体外吧!不然每次都初始化为0
do {
scanf("%ld",&num[i]);//long型的格式控制符为%ld
scanf("%f",&score[i]);//输入应该有&
n++;
i++;
} while(score[i-1]>0);
return(n-1);//这里应该是n-1,因为最后一个分数小于0表示结束输入,
//而此时的n已经加1了,所以需要减1
}

int GetFail(long num[], float score[]) {
int i;
x=0;
for(i=0; i<n; i++) {
if(score[i]<60) { //这里有个大括号,因为是当成绩小于60的学生才输出
x++; //没有大括号就代表只统计不及格的人数
printf(" %d ", num[i]);
}
}
printf("\n");
return(x);
}
int GetAboveAver(long num[], float score[]) {
int i;
float t=0;//最好初始化一下
y=0;
//这里应该是先把所有学生的总分。
for(i=0; i<n; i++) {
t+=score[i];
}
//求平均分
float ave=t*1.0/n;//注意:程序中的/和数学中的除法不一样,想要得到小数,可以通过乘以1.0的方式
//依次查找及格的学生,统计人数
for(i=0; i<n; i++) {
if(score[i]>=60) {
y++;
printf(" %d ", num[i]);
}
}
printf("\n");
printf("全班平均分: %f ",ave);
return(y);
}
void GetDetail(float score[]) {
int i,a, b;
float u,v,w;
u=(float)x/n;
a=b=0;//初始化一下
for(i=0; i<n; i++) {
if(score[i]>=60&&score[i]<80)//这里是&&,表示与,而不是||
a++;
if(score[i]>=80&&score[i]<=100)
b++;
}
v=(float)a/n;
w=(float)b/n;
printf("不及格学生占百分比: %f \n",u);
printf("60分到80占百分比: %f \n",v);
printf("80分以上占百分比: %f \n",w);
}


拥抱Linux 2019-05-24
  • 打赏
  • 举报
回复
小错误不少,有一些是scanf函数的参数使用不正确,有一些是代码逻辑问题,还有一些是编程风格方面的问题,比如全局变量和局部变量的名称一样,等等。
就不一一指出来了,你自己一行一行地看吧。

在你的代码基础上修改之后的结果如下,供参考:

#include <stdio.h>

#define ARR_SIZE 30

int ReadScore(long num[], float score[]);
int GetFail(long num[], float score[]);
int GetAboveAver(long num[], float score[]);
void GetDetail(float score[]);

int x, y, n;

int main(void)
{
long num[ARR_SIZE];
float score[ARR_SIZE];
n = ReadScore(num, score);
printf("学生总人数为: %d\n", n);
printf("不及格学生:");
x = GetFail(num, score);
printf("不及格人数为: %d \n", x);
printf("及格学生:");
y = GetAboveAver(num, score);
printf("及格人数为: %d \n", y);
GetDetail(score);
return 0;
}

int ReadScore(long num[], float score[])
{
int i = 0;
n = 0;
do {
scanf("%ld", &num[i]);
scanf("%f", &score[i]);
n++;
i++;
} while (score[i - 1] > 0);
return (n - 1);
}

int GetFail(long num[], float score[])
{
int i;
x = 0;
for (i = 0; i < n; i++) {
if (score[i] < 60) {
x++;
printf(" %ld ", num[i]);
}
}
printf("\n");
return (x);
}
int GetAboveAver(long num[], float score[])
{
int i;
float t = 0;
y = 0;
for (i = 0; i < n; i++) {
t += score[i] / n;
if (score[i] >= 60) {
y++;
printf(" %ld ", num[i]);
}
}
printf("\n");
printf("全班平均分: %.1f\n", t);
return (y);
}
void GetDetail(float score[])
{
int i, a = 0, b = 0;
float u, v, w;
u = (float)x / n;
for (i = 0; i < n; i++) {
if (score[i] >= 60 && score[i] < 80)
a++;
if (score[i] >= 80 && score[i] <= 100)
b++;
}
v = (float)a / n;
w = (float)b / n;
printf("不及格学生占百分比: %.0f %% \n", u * 100);
printf("60分到80占百分比: %.0f %% \n", v * 100);
printf("80分以上占百分比: %.0f %% \n", w * 100);
}

如有帮助,欢迎 及时 结帖、给分! :-)

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧