连续输入若干个正整数,求出其和及平均值,直到输入为0结束(c语言)_百度...
发布网友
发布时间:2024-09-09 15:42
我来回答
共4个回答
热心网友
时间:2024-10-11 19:39
#include <stdio.h>
#include <stdlib.h>
#define N 10
int main(void)
{
int i,aver=0,index=0,sum=0;
int * a;
a=(int * )malloc(sizeof(int)*N);
printf("连续输入若干个正整数(输入0结束):\n");
while(1)
{
scanf("%d",&a[index]);
if(0==a[index])
{
break;
}
sum+=a[index];
index++;
if(index>=N)
{
a=(int * )realloc(a,sizeof(int)*N);
}
}
if(index>0)
aver=sum/index;
else
aver=0;
printf("总和为%d,平均值为%d\n",sum,aver);
return 0;
}
热心网友
时间:2024-10-11 19:35
我用C++写了一个
#include
<iostream.h>
void
main()
{
int
sum
=
0,
num,
counter
=
0;
double
ave;
cin
>>num;
while(num)
{
counter++;
sum
+=
num;
ave
=
(double)sum
/
counter;
}
cout
<<"the
summation
is
"<<sum<<endl;
cout
<<"the
average
is
"<<ave<<endl;
}
热心网友
时间:2024-10-11 19:40
main()
{
int i,j;
int Sum;
Float avg;
Sum=i=j=0;
avg=0;
do
{
Scanf("%d",&i);
j++;
Sum=Sum+i;
}while (i<>0)
j--;
avg=Sum/j;
printf("Sum=%d,Avg=%f",Sum,avg);
}
热心网友
时间:2024-10-11 19:37
#include <stdio.h>
void main()
{
int n,i=0;
double ver, sum =0;
scanf("%d",&n);
while(n!=0)
{
++i;
sum += n;
scanf("%d", &n);
}
ver = sum/i;
printf("sum = %f,ver = %f",sum,ver);
}
热心网友
时间:2024-10-11 19:38
#include <stdio.h>
#include <stdlib.h>
#define N 10
int main(void)
{
int i,aver=0,index=0,sum=0;
int * a;
a=(int * )malloc(sizeof(int)*N);
printf("连续输入若干个正整数(输入0结束):\n");
while(1)
{
scanf("%d",&a[index]);
if(0==a[index])
{
break;
}
sum+=a[index];
index++;
if(index>=N)
{
a=(int * )realloc(a,sizeof(int)*N);
}
}
if(index>0)
aver=sum/index;
else
aver=0;
printf("总和为%d,平均值为%d\n",sum,aver);
return 0;
}
热心网友
时间:2024-10-11 19:35
main()
{
int i,j;
int Sum;
Float avg;
Sum=i=j=0;
avg=0;
do
{
Scanf("%d",&i);
j++;
Sum=Sum+i;
}while (i<>0)
j--;
avg=Sum/j;
printf("Sum=%d,Avg=%f",Sum,avg);
}
热心网友
时间:2024-10-11 19:35
#include <stdio.h>
void main()
{
int n,i=0;
double ver, sum =0;
scanf("%d",&n);
while(n!=0)
{
++i;
sum += n;
scanf("%d", &n);
}
ver = sum/i;
printf("sum = %f,ver = %f",sum,ver);
}
热心网友
时间:2024-10-11 19:32
我用C++写了一个
#include
<iostream.h>
void
main()
{
int
sum
=
0,
num,
counter
=
0;
double
ave;
cin
>>num;
while(num)
{
counter++;
sum
+=
num;
ave
=
(double)sum
/
counter;
}
cout
<<"the
summation
is
"<<sum<<endl;
cout
<<"the
average
is
"<<ave<<endl;
}