dw网页设计作业成品:一个c结构程序问题

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/27 22:29:29
#include <stdio.h>
#define N 5

struct student
{
int sno;
char name[10];
float score_c;
float score_java;
}stu[N]={
{1,"lineage",88,87},
{2,"billy",79,99},
{3,"jimmy",56,89},
{4,"bob",66,98},
{5,"pears",80,55}};

void ave(float m,float n);

void main()
{
int i;
float ave[5];
for(i=0;i<N;i++)
{
ave[i]=ave(struct student stu[i].score_c,struct student stu[i].score_java);
}

}

void ave(float m,float n)
{
float sum,ave;
sum=m+n;
ave=sum/2;
printf("%.2f",ave);
}

请高手看看它题目要求定义一个结构,用来保存学员的信息,上面我已经定义好了。要求用函数来计算平均成绩。我老是提示有错误,请帮忙!~~急!

#include <stdio.h>
struct student
{
int sno;
char name[10];
float score_c;
float score_java;
}
void av(float,float,char);
main()
{
int i;
float a,b;
char c;
struct student ave[5]={
{1,"lineage",88,87},
{2,"billy",79,99},
{3,"jimmy",56,89},
{4,"bob",66,98},
{5,"pears",80,55}};
for(i=0;i<5;i++)
{
a=ave[i].score_java;
b=ave[i].score_c;
c=ave[i].name;
av(a,b,c);
}
}

void av(float m,float n,char z)
{
float sum,ave;
sum=m+n;
ave=sum/2;
printf("%s的总分是:%.2f\t平均分是:%.2f\n",z,sum,ave);
}

正确答案~
1.结构体相当于一个数据类型,和int的用法一样,如上:struct student你可以看做是一个数据类型 ave[5]就是数组名。和定义int ave[5]是一样的道理。然后直接给ave[5]赋值
2.函数要在主函数里调用,另外要声明原函数。
3.调用函数时传的参是实参,需要有声明。
4.尽量不要过多的重名,你的结构体变量(数组)和函数名相同,有的编译器会报错

void ave(float m,float n)
{
float sum,ave;
sum=m+n;
ave=sum/2;
printf(\"%.2f\",ave);
}
说明无返回值
请改成
float ave(float m,float n)
{
float sum,ave;
sum=m+n;
ave=sum/2;
printf(\"%.2f\",ave);
return ave;
}

#include <stdio.h>
#define N 5

struct student
{

int sno;
char name[10];
float score_c;
float score_java;
}stu[N]={
{1,"lineage",88,87},
{2,"billy",79,99},
{3,"jimmy",56,89},
{4,"bob",66,98},
{5,"pears",80,55}};

float ave(float m,float n);

void main()
{
int i;
float av[5];
for(i=0;i<N;i++)
{

av[i]=ave(stu[i].score_c,stu[i].score_java);
}
for(i=0;i<N;i++)
{

printf("%f\n",av[i]);

}

}

float ave(float m,float n)
{
float sum,ave;
sum=m+n;
ave=sum/2;
return ave;
}

有三个错误:
1) ave函数不返回值,但却在main函数里赋值给ave
2) float ave[5]和ave函数重名,要改
3) ave[i]=ave(struct student stu[i].score_c,struct student stu[i].score_java); 里的 struct student 不需要。

正确的程序如下:

#include <stdio.h>
#define N 5

struct student
{
int sno;
char name[10];
float score_c;
float score_java;
}stu[N]={
{1,"lineage",88,87},
{2,"billy",79,99},
{3,"jimmy",56,89},
{4,"bob",66,98},
{5,"pears",80,55}};

float ave(float m,float n);

int main(void)
{
int i;
float ave2[N];

for(i=0;i<N;i++)
{
ave2[i]=ave(stu[i].score_c,stu[i].score_java);

}
return 0;
}

float ave(float m,float n)
{
float sum,ave;
sum=m+n;
ave=sum/2;
printf("%f\n", ave);
return ave;
}

阅过,便我未定义过结构