卡布奇诺qi
2018-10-26 21:23
#include <stdio.h>
int score[]={67,98,75,63,82,79,81,91,66,84};
int main()
{ void sort(int a[],int n); //以下5行为函数声明
int mark_sum(int a[],int n);
int average(int n);
int Max(int a[],int n);
int Min(int a[],int n);
int i; //局部变量,只在main函数中生效
sort(score,10); //调用sort函数
printf("mark=%d\t",mark_sum(score,10)); //调用mark_sum函数并输出
printf("average=%d\n",average(10)); //同上
printf("max=%d \t",Max(score,10));
printf("min=%d\n",Min(score,10));
printf("成绩降序:"); //输出已排好序的数组元素
for(i=0;i<10;i++)
printf("%d ",score[i]);
printf("\n");
return 0;
}
void sort(int a[],int n) //定义sort函数
{ int i,j,t; //起泡法排序算法
for(j=0;j<n-1;j++)
for(i=0;i<n-1-j;i++)
if(a[i]<a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
}
int mark_sum(int a[],int n) //同上
{ int i,sum=0;
for(i=0;i<10;i++)
sum+=a[i];
return sum;
}
int average(int n)
{ int aver;
aver=mark_sum(score,10)/10;
return aver;
}
int Max(int a[],int n)
{ int i,max=a[0];
for(i=1;i<10;i++)
if(max<a[i]) max=a[i];
return max;
}
int Min(int a[],int n)
{ int i,min=a[0];
for(i=1;i<10;i++)
if(min>a[i]) min=a[i];
return min;
}
这太麻烦了吧
#include <stdio.h>
#include <stdlib.h>
int score_sum(int arr[], int n);
int score_max(int arr[], int n);
int score_min(int arr[], int n);
int score_ave(int arr[], int n);
int score_bubble(int arr[], int n);
int main()
{
int sum, max, min, ave;
int score[] = { 67, 98, 75, 63, 82, 79, 81, 91, 66, 84 };
int N = sizeof(score) / sizeof(int);
sum = score_sum(score, N);
printf("sum of score is %d\n", sum);
max = score_max(score, N);
printf("max of score is %d\n", max);
min = score_min(score, N);
printf("max of score is %d\n", min);
ave = score_ave(score, N);
printf("max of score is %d\n", ave);
score_bubble(score, N);
system("pause");
return 0;
}
int score_sum(int arr[], int n)
{
int sum = 0;
int i;
for (i = 0; i < n; i++)
{
sum += arr[i];
}
return sum;
}
int score_max(int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
{
if(arr[i] > max)
max = arr[i];
}
return max;
}
int score_min(int arr[], int n)
{
int i;
int min = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] < min)
min = arr[i];
}
return min;
}
int score_ave(int arr[], int n)
{
int sum = 0;
int i;
for (i = 0; i < n; i++)
{
sum += arr[i];
}
return sum / n;
}
int score_bubble(int arr[], int n)
{
int i, j, temp;
for (i = n; i >= 0; i--)
for (j = 0; j < i; j++)
if (arr[j] < arr[j+1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
C语言入门
926207 学习 · 20797 问题
相似问题