c中的多维数组不接受我的输入

时间:2014-01-10 15:55:01

标签: c arrays multidimensional-array

/ *在下面的程序中,我想接受5个科目中任意数量学生的分数,将它们存储在一个名为“标记”的数组中,然后计算每个学生对所有5个科目的平均分数。     但它似乎只接受你输入的最后一个学生的标记。

我想知道程序中的错误吗?


   {
    char subject[5][10]=
    {
        "Maths",   
        "Physics",
        "English",
        "Chemistry",
        "Biology",
    };


    char student[30][20],ans;
    int marks [30][5],i,j,nos, total;
    float studavg[30], subavg[5];
    main()
    {
        for (ans='y',i=0;i<30 && ans=='y';i++)
        {
            printf("\nEnter student number: %d",i+1);
            gets(student[i]);
            fflush(stdin);
            printf("more? (y/n)");
            ans=getchar();
            fflush(stdin);    
        }
        nos=i;
/*that part initializes my student array and saves the no of students*/

        for (i=0;i<nos;i++)
        {
             for (j=0;j<5;j++)
             {
                 marks[i][j]=0;
                 printf("Enter the marks for %s in %s",student[i],subject[j]);
                 scanf("%d",&marks[i][j]);
                 fflush(stdin);    
             }                  
         }      
    /*this part is supposed to create an array of student marks*/


        for (i=0;i<nos;i++)
        {
            for (j=0;j<5;j++)
            {
               total+=marks[i][j];           
            }
            studavg[i]=total/5;
         }
     }

/*the last part was to find the student average*/}

1 个答案:

答案 0 :(得分:1)

您必须为每位新学生清除total

for (i = 0; i < nos; i++)
{
    for (j = total = 0; j < 5; j++)
    {
       total += marks[i][j];
    }
    studavg[i] = total / 5;
}