平均分大了一倍那樣,希望大佬解答
#include <stdio.h>
int sum = 0.00;
int max;
int min;
double maen;
int i;
int j;
int summation(int score[])
{
? ? for(i = 0; i < 10; i++)
? ? {
? ? ? ? sum += score[i];
? ? }
? ? return sum;
}
int maximal(int score[])
{
? ? max = score[0];
? ? for(i = 0; i < 10; i++)
? ? {
? ? ? ? if(score[i] > max)
? ? ? ? {
? ? ? ? ? ? max = score[i];
? ? ? ? }
? ? }
? ? return max; ??
}
int minimum(int score[])
{
? ? min = score[0];
? ? for(i = 0; i<10; i++)
? ? {
? ? ? ? if(score[i] < min)
? ? ? ? {
? ? ? ? ? ? min = score[i];
? ? ? ? }
? ? }
? ? return min;
}
double average(int score[])
{
? ? double a = summation(score);
? ? maen = a / 10;
? ? return maen;
}
void descending_order(int score[])
{
? ? for(i = 0; i < 9; i++)
? ? {
? ? ? ? for(j = 0; j <= i; j++)
? ? ? ? {
? ? ? ? ? ? int n = 0;
? ? ? ? ? ? if(score[j] > score[j + 1])
? ? ? ? ? ? {
? ? ? ? ? ? ? ? n = score[j];
? ? ? ? ? ? ? ? score[j] = score[j + 1];
? ? ? ? ? ? ? ? score[j + 1] = n;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for(i = 0; i < 10; i++)
? ? {
? ? ? ? if(i < 9)
? ? ? ? {
? ? ? ? ? ? printf("%d, ", score[i]);
? ? ? ? }else{
? ? ? ? ? ? printf("%d", score[i]);
? ? ? ? }
? ? }
}
int main()
{
? ? int score[10]={67,98,75,63,82,79,81,91,66,84};
? ? printf("班級總分為:%d\n", summation(score));
? ? printf("班級的最高分為:%d\n", maximal(score));
? ? printf("班級的最低分為:%d\n", minimum(score));
? ? printf("班級的平均分為:%.2f\n", average(score));
? ? descending_order(score);
}
2018-10-26
第一點我需要吐槽的是你的代碼沒有可讀性,很雜亂。第二我已經(jīng)看出來的你的問題了,全局變量和局部變量名重復(fù)了,導(dǎo)致改變了變量中的值。我建議你不要全局變量,只在函數(shù)中定義局部變量,當你需要哪些變量的時,在定義。這樣即使兩個局部變量名重復(fù)了也不影響變量中的值。