问答详情
源自:6-12 综合练习

有的时候能成功运行,有的时候会出现错误

有的时候运行会出现 segmentation fault的字样 然后就运行失败了

#include <stdio.h>
int sum(int score[])
{
    int sum=0;
    int i;
    for (i=0;i<10;i++)
    {
        sum = sum + score[i];
    }
    return sum;
}

int max (int score[])
{
    int max = 0;
    int i;
    for (i=0;i<9;i++)
    {
        if (score[i]>score[i+1])
        {
            max= score[i];
        }
    }
    return max;
}

int low (int score[])
{
    int low = 0;
    int i;
    for (i=0;i<9;i++)
    {
        if (score[i]<score[i+1])
        {
            low = score[i];
        }
    }
    return low;
}

int average(int score[])
{
   
    int sum=0;
    for (int i=0;i<10;i++)
    {
        sum= sum+score[i];
    }
    return sum/10;
}

void order( int score[])
{
    int i,j;
    for (i=9;i>=0;i--)
    {
        for (j=0;j<=i;j++)
        {
            int temp = 0;
            if (score[j]>score[j+1])
            {
                temp = score[j];
                score[j] = score[j+1];
                score[j+1] = temp;
            }
            
        }
    }
}
int main()
{
    int score[10]={67,98,75,63,82,79,81,91,66,84};
    printf("总分为%d\n",sum(score));
    printf("最高分为%d\n",max(score));
    printf("最低分为%d\n",low(score));
    printf("平均分为%d\n",average(score));
    order(score);
    int i;
    for (i=0;i<10;i++)
    {
        printf("%d\t",score[i]);
    }
    printf("\n");
    return 0;
}


提问者:慕仙7415739 2020-02-29 08:49

个回答

  • qq_莫失莫忘_70
    2020-03-22 02:14:08

    #include <stdio.h>
    #define N 10
    int sum(int score[N])
    {
        int i,s=0;
        for(i=0;i<N;i++)
        {
            s+=score[i];
        }
        printf("总和是%d\n",s);
        return 0;
    }
    int max(int score[N])
    {
        int i,M=0;
        for(i=0;i<N;i++)
        {
            if(score[i]>M)
            {
                M=score[i];
            }
        }
          printf("最大值是%d\n",M);
        return 0;
    }
    int min(int score[N])
    {
        int i,m=100;
        for(i=0;i<N;i++)
        {
            if(score[i]<m)
            {
                m=score[i];
            }
        }
          printf("最小值是%d\n",m);
        return 0;
    }
    double avg(int score[N])
    {
        int i;double sum=0;
        for(i=0;i<N;i++)
        {
            sum+=score[i];

        }
        sum/=N;
        printf("平均值是%f\n",sum);
        return 0;
    }
    int sort(int score[N])
    {
        int i,j;
        for(j=N;j>=0;j--)
        for(i=j;i>=0;i--)
        {
           int temp;
           if(score[i]>score[i-1])
           {
               temp=score[i];
               score[i]=score[i-1];
               score[i-1]=temp;
           }
            }
            printf("排完序列以后:");
        for(i=0;i<N;i++)
        {

            printf("%d ",score[i]);
        }
        return 0;
    }
    int main()
    {
        int score[N]={67,98,75,63,82,79,81,91,66,84};
        sum(score);
        max(score);
        min(score);
        avg(score);
        sort(score);
        return 0;
    }