-1

我在下面制作了这个程序来计算学生的平均成绩。在我使用 -1 之前一切正常,它应该停止程序,因为它显示所有已输入学生的平均值,说再见!然后终止。

我认为我的计算可能是错误的,因为它打印了学生平均分数的错误结果。提前致谢。

#include <stdio.h>

int main(void)
{
float final_mark(int a_mark1, int a_mark2, int lab_mark, int quiz_mark, int exam_mark);
int i, a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark;
float average_mark = 0.0;
do
{
    for (i = 0; i < 2; i++)
    {
        printf("Enter assignment 1 mark (-1 to quit): ");
        scanf("%d", &a_mark1);

        if(a_mark1 == -1)
        {
            average_mark += final_mark(a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark);
            if ((average_mark > 1 ) && (average_mark < 100 ))
            {
                printf("The average student mark is %.2f%% \n", average_mark);
            }
            printf("Goodbye! \n");
            return 0;
        }

        printf("Enter assignment 2 mark: ");
        scanf("%d", &a_mark2);

        printf("Enter laboratory mark: ");
        scanf("%d", &lab_mark);

        printf("Enter quiz mark: ");
        scanf("%d", &quiz_mark);

        printf("Enter exam mark: ");
        scanf("%d", &exam_mark);

        printf("Student %d final mark: %.2f \n", i + 1, final_mark(a_mark1, a_mark2, lab_mark, quiz_mark, exam_mark));
    }
}
while(a_mark1 != -1);
return 0;
}
float final_mark(int a_mark1, int a_mark2, int lab_mark, int quiz_mark, int     exam_mark)
{
float final_mark = a_mark1 * 0.1 + a_mark2 * 0.15 + lab_mark * 0.15 +     quiz_mark * 0.1 + exam_mark * 0.5;
return final_mark;
}
4

3 回答 3

1

我认为您需要重新考虑一下您的逻辑。为什么不使用while循环来控制流量。然后,如果用户立即输入 -1,您可以立即退出程序。您应该使用一个数组来存储每个学生的平均值,然后您可以循环并找到班级平均值。

-float final_mark没有任何括号,您的功能似乎有点粗略。

- 你也应该把你的函数原型放在外面main。请参阅下面的更改。

#include <stdio.h>

#define MAX_STUDENTS 10 //define what the max number of students is

float final_mark(int mark1, int mark2, int lab, int quiz, int exam);

int main()
{
   int i = 0, mark1 = 0, mark2 = 0,
       lab = 0, quiz = 0, exam = 0;
   int num_students;
   float students_avg[MAX_STUDENTS] = {0}; //array to hold averages for students
   float average = 0;

   while (i < MAX_STUDENTS) {

      printf("Enter assignment 1 mark (enter -1 to quit):\n");
      scanf("%d", &mark1);

      if (mark1 == -1)
         break; //no more students, break out of while loop

      printf("Enter assignment 2 mark: ");
      scanf("%d", &mark2);

      printf("Enter laboratory mark: ");
      scanf("%d", &lab);

      printf("Enter quiz mark: ");
      scanf("%d", &quiz);

      printf("Enter exam mark: ");
      scanf("%d", &exam);

      average = final_mark(mark1, mark2, lab, quiz, exam);

      students_avg[i] = average; //add this average to array

      printf("Student # %d average was %.2f\n", i, students_avg[i]);//debug info

      i++;
   }
   num_students = i; //how many students grades did we read?

   average = 0; //reset to 0 so we can use below

   for (i = 0; i < num_students; i++)
      average += students_avg[i];

   if (num_students > 0)
      printf("Class average is %.2f\n", average/num_students);
   else
      printf("Goodbye!\n");

   return 0;
}

float final_mark(int mark1, int mark2, int lab, int quiz, int exam)
{
   //we can just return the calculation
   return ((mark1 * 0.1) + (mark2 * 0.15) + (lab * 0.15) + (quiz * 0.1) + (exam * 0.5));
}
于 2018-05-28T15:32:33.640 回答
0

只需擦除上面的两行 printf("GOODBYE!\n"); 你会得到你想要的。

于 2018-05-28T15:00:15.683 回答
-1

如果用户输入 -1,也许您必须将 mark s 变量初始化为 0。并且 a_mark1 也初始化为 0

于 2018-05-28T14:50:10.147 回答