为了账号安全,请及时绑定邮箱和手机立即绑定

这个代码有什么问题?

#include <stdio.h>
int total(int score[])
{
    int sum;
    int size = sizeof(score);
    for(int i=0; i<size; i++)
    {    
        sum += score[i];
    }
    return sum;
}
int max(int score[])
{
    int max, item;
    int size = sizeof(score); 
    for(int i=0; i<size; i++)
    {
        item = score[i];
        if(item > max)
        {
            max = item;
        }
    }
}
int min(int score[])
{
    int min, item;
    int size = sizeof(score);
    for(int i=0; i<size; i++)
    {
        item = score[i];
        if(item < min)
        {
            min = item;
        }
    }
}
int average(int score[])
{
    return (float)total(score) / sizeof(score);
}
void sort(int score[])
{
    int i, temp, flag;
    int size = sizeof(score); 
    while(flag)
    {
        flag = 0;
        for(i=0; i<size-1; i++)
        {
            if(score[i] > score[i+1])
            {
                // 交换两元素
                temp = score[i];
                score[i] = score[i+1];
                score[i+1] = temp;
                flag = 1;
                }
            }
        }
}
void printScore(int score[])
{
    int size = sizeof(score);
    for(int i=0; i<size; i++)
    {
        printf(" %d", score[i]);
    }
    printf("\n")
}
int main()
{
    int score[N]={67,98,75,63,82,79,81,91,66,84};
    printf("Test function total(): total(score)=%d\n", total(score));
    printf("Test function max(): max(score)=%d\n", max(score));
    printf("Test function min(): min(score)=%d\n", min(score));
    printf("Test function average(): average(score)=%d\n", average(score));
    printf("Test function sort(): sort(score)=");
    sort(score);
    printScore(score);
    return 0;
}


运行失败

hello.c: In function 'total':
hello.c:5:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
     int size = sizeof(score);
                      ^
hello.c:2:15: note: declared here
 int total(int score[])
               ^~~~~
hello.c: In function 'max':
hello.c:15:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
     int size = sizeof(score);
                      ^
hello.c:12:13: note: declared here
 int max(int score[])
             ^~~~~
hello.c: In function 'min':
hello.c:28:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
     int size = sizeof(score);
                      ^
hello.c:25:13: note: declared here
 int min(int score[])
             ^~~~~
hello.c: In function 'average':
hello.c:40:40: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
     return (float)total(score) / sizeof(score);
                                        ^
hello.c:38:17: note: declared here
 int average(int score[])
                 ^~~~~
hello.c: In function 'sort':
hello.c:45:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
     int size = sizeof(score);
                      ^
hello.c:42:15: note: declared here
 void sort(int score[])
               ^~~~~
hello.c: In function 'printScore':
hello.c:64:22: warning: 'sizeof' on array function parameter 'score' will return size of 'int *' [-Wsizeof-array-argument]
     int size = sizeof(score);
                      ^
hello.c:62:21: note: declared here
 void printScore(int score[])
                     ^~~~~
hello.c:70:1: error: expected ';' before '}' token
 }
 ^
hello.c: In function 'main':
hello.c:73:15: error: 'N' undeclared (first use in this function)
     int score[N]={67,98,75,63,82,79,81,91,66,84};
               ^
hello.c:73:15: note: each undeclared identifier is reported only once for each function it appears in


正在回答

1 回答

#include <stdio.h>
#include <stdlib.h>
#define N 10int cmp(const void *a,const void *b){    return *(int*)b - *(int*)a;}int Sum(int score[]){    int s = 0,i;    for(i = 0 ; i < N ; i++)        s += score[i];    return s;}int Max(int score[]){    return score[0];}int Min(int score[]){    return score[N-1];}double Avg(int s){    return (s + 0.0) / N;}int main(){    int score[N]={67,98,75,63,82,79,81,91,66,84};    int i,sum;    sum = Sum(score);    qsort(score,N,sizeof(int),cmp);//sort it    printf("Total score : %d\n",sum);    printf("Max score : %d\n",Max(score));    printf("Min Score : %d\n",Min(score));    printf("Average score : %f\n",Avg(sum));    printf("\n成绩排序:\n");    for(i = 0 ; i < N ; i++)    {        printf("Rank %d's score : %d\n",i+1,score[i]);    }    return 0;}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C语言入门
  • 参与学习       924400    人
  • 解答问题       20753    个

C语言入门视频教程,带你进入编程世界的必修课-C语言

进入课程

这个代码有什么问题?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信