第1个回答 2010-05-11
#include <stdio.h>
#define N 10
main()
{
int a[N],i,max=0,min=0,sum=0;
float average;
printf("Please input the number :\n");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
max=max>a[i]?max:a[i];
min=min<a[i]?min:a[i];
sum=sum+a[i];
}
average=(float)sum/N; /*(float)将整型量sum强制转换为实型量,以保证average的精度*/
printf("the max=%d\nthe min=%d\nthe average=%f\nthe sum=%d",max,min,average,sum);
}
/*以上程序我设的一维数组长度为10,并已调试成功。*/
第2个回答 2010-05-11
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
#define ARRAY_SIZE 10
int max(int * ary , int len);
int min(int * ary , int len);
float ave(int * ary , int len);
int sum(int * ary , int len);
int main(int argc , char ** argv){
int num[ARRAY_SIZE] ;
/***********初始化数组*************/
int i;
time_t _tmp;
srand((unsigned int)time(&_tmp)); //播随机数种子
for(i = 0 ; i < ARRAY_SIZE ; i++){
num[i] = rand() % (INT_MAX / ARRAY_SIZE);/**防止加法整型溢出**/
}
/********************************/
/**********显示数组的内容********/
for(i = 0 ; i < ARRAY_SIZE ; i++){
printf("num[%-3d] = %d \n" , i , num[i]);
}
/********************************/
/**********输出计算结果**********/
printf("------------ out put --------------\n");
printf("max : %d\n" , max(num , ARRAY_SIZE));
printf("min : %d\n" , min(num , ARRAY_SIZE));
printf("ave : %.8f\n" , ave(num , ARRAY_SIZE));
printf("sum : %d\n" , sum(num , ARRAY_SIZE));
printf("\nPress Any Key Continue");
/********************************/
getch();
return EXIT_SUCCESS;
}
//求最大值
int max(int * ary , int len){
int i , tmp = *ary ;
for(i = 1 ; i < len ; i++){
tmp = *(ary+i) > tmp ? *(ary+i) : tmp;
}
return tmp;
}
//求最小值
int min(int * ary , int len){
int i , tmp = *ary;
for(i = 1 ; i < len ; i++){
tmp = *(ary+i) < tmp ? *(ary+i) : tmp;
}
return tmp;
}
//求最平均值
float ave(int * ary , int len){
return sum(ary , len) * 1.0 / len;
}
//求和
int sum(int * ary , int len){
int i , sum = 0;
for(i = 0 ; i < len ; i++){
sum += *(ary+i);
}
return sum;
}本回答被网友采纳