输入不超过20个非负整数(输入每个整数后按回车,以-1结束且不计入整数个数),用一维数组作函数参数编程实现如下功能:
(1)录入每个整数,函数原型:unsigned short ReadNumber(int num[]),num存储输入的整数,返回值为输入整数的个数;
(2)按由高到低的顺序排序,函数原型:void SortNumberDescending(int num[], unsigned short n),n为整数个数;
(3)对这些整数求和及平均值,函数原型:void SummingAveraging(int num[], unsigned short n, int *pSum, float *pAver),pSum指向和变量,pAver指向平均值变量;
(4)输出这些整数,函数原型:void PrintNumber(int num[], unsigned short n) 。
主函数中按以下示例形式提示输入、调用函数(1)、调用函数(2)、提示输出、调用函数(4)、调用函数(3)、输出整数和及平均值。
Input Numbers:
87
100
...
23
-1
Sort in descending order:
100
...
23
Sum=474,Aver=59.25
输入格式:
"Input Numbers:\n"
"%d"
输出格式:
"\nSort in descending order:\n"
"\nSum=%d,Aver=%.2f\n"
"%d\n"
#include <stdio.h>
#include <stdlib.h>
unsigned short ReadNumber(int num[]);
void SortNumberDescending(int num[], unsigned short n);
void SummingAveraging(int num[], unsigned short n, int *pSum, float *pAver);
void PrintNumber(int num[], unsigned short n);
int main()
{
int num[20];
int sum=0;
float aver=0;
int *pSum=&sum,*pAver=&aver;
unsigned short n;
printf("Input Numbers:\n");
n=ReadNumber(num);
SortNumberDescending(num,n);
printf("\nSort in descending order:\n");
PrintNumber(num,n);
SummingAveraging(num,n,&sum,&aver);
printf("\nSum=%d,Aver=%.2f\n",sum,aver);
}
unsigned short ReadNumber(int num[])
{
int i=0;
unsigned short n;
do
{
scanf("%d",&num[i]);
i++;
}while(num[i-1]>=0);
n=i-1;
return n;
}
void SortNumberDescending(int num[], unsigned short n)
{
int i,j,k,temp;
for(i=0;i<n;i++)
{
k=i;
for(j=i+1;j<n;j++)
{
if(num[j]>num[k]) k=j;
}
if(k!=i)
{
temp=num[i];
num[i]=num[k];
num[k]=temp;
}
}
}
void SummingAveraging(int num[], unsigned short n, int *pSum, float *pAver)
{
int i;
for(i=0;i<n;i++)
{
*pSum+=num[i];
}
*pAver=(float)*pSum/n;
}
void PrintNumber(int num[], unsigned short n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\n",num[i]);
}
}