c语言 求五个人平均身高,输入五个人身高后无法求平均值 哪里出错了,求大佬点拨 求C语言大佬,这个程序循环不了,而且输入也有问题,哪里出错了...

作者&投稿:氐燕 (若有异议请与网页底部的电邮联系)

两个错误:

1、sum没有赋初始值0。

2、sum是整型,sum/5是整型运算,结果为整型,并不是真实想求得的结果。解决方法:将5改为5.0,即sum/5.0,使运算变成实型运算。

另外,这个程序实在是太繁琐了。修改一下:

#include <stdio.h>
void main(){
int i,h,sum=0;
for(i=0;i<5;i++){
printf("第%d个人的身高(cm): ",i+1);
scanf("%d",&h);
sum+=h;
}
printf("五个人的平均身高: %f",sum/5.0);
}

//运行示例:



C语言 求三个数的最大数的最大公约数? 哪里出错了? 请大佬帮忙解决下~

好像是属于比较基础的c语言编程吧,你去找一本c语言编程基础学习应该就可以了。

对程序作了几处修改,现在可以运行了:
#include#includestruct link *appendnode(struct link *head);void displink(struct link *head);void deletememory(struct link *head);struct message{char name[10];int score[5];};struct link{struct message data;struct link *next;};int main(){int i=0;char c;struct link *head=NULL;printf("do you want to append a new node(Y/N)?
");scanf(" %c",&c);while (c=='Y'){head=appendnode(head);displink(head);printf("do you want to append a new node(Y/N)?
");scanf("%c",&c);i++;}printf("%d new nodes have been appended!
",i);//deletememory(head);return 0;}struct link*appendnode(struct link *head){struct link *p=NULL;struct link *pr=head;struct message data;p=(struct link *)malloc(sizeof (struct link));if (p==NULL){printf("no enough memory to alloc");exit(0);}if (head==NULL){head=p;}else{while (pr->next!=NULL){pr=pr->next;}pr->next=p;}pr=p;printf("请输入姓名和五科成绩:
");scanf("%s%d%d%d%d%d%*c",p->data.name,&p->data.score[0],&p->data.score[1],&p->data.score[2],&p->data.score[3],&p->data.score[4]);pr->next=NULL;return head;}void displink(struct link *head){struct link *p=head;int j=1;struct message data;while (p!=NULL){printf("%5d%s%5d%5d%5d%5d%5d
",j,p->data.name,p->data.score[0],p->data.score[1],p->data.score[2],p->data.score[3],p->data.score[4]);p=p->next;j++;}}