一个计算平均值C程序

来源:百度知道 编辑:UC知道 时间:2024/06/07 19:57:37
本人是C语言的初学者,请教一个C语言程序的编写。要求程序读入一系列数据直到读入标志值-1为止,然后显示出之前所输入数据的平均值,下面是程序的运行示例:(括号中是从输入的数)
This program averages a list of numbers.
Enter -1 to signal the end of the list.
? (95)
? (100)
? (89)
? (91)
? (97)
? (-1)
The average is 94.4
谢谢,帮帮忙。

//给你一个完整的程序,已经测试过了,没有问题
#include <stdio.h>

void main()
{
int data,sum = 0;
int count = 0;

printf("This program averages a list of numbers.\nEnter -1 to signal the end of the list.\n");

do
{
printf("? ");scanf("%d",&data);
sum += data;
++count;

}while(data!=-1);

++sum;
--count;

printf("The average is %.2f\n",float(sum)/count);
}

#include <stdio.h>
#include <stdlib.h>

void main()
{
float a=0,b=0;
int c=0;
printf("This program averages a list of numbers.\nEnter -1 to signal the end of the list.\n");
for (c=0; a!=-1; c++)
{
b+=a;
printf("? ");
scanf("%f",&a);
}
printf("%s %f\n","The average is",b/(c-1));
system("pause");