使用C

时间:2018-05-28 14:35:53

标签: c if-statement while-loop

我在下面编写了这个程序来计算学生的平均分数。 一切顺利,直到我使用-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;
}

3 个答案:

答案 0 :(得分:0)

只需擦除printf上方的两行(“GOODBYE!\ n”);你会得到你想要的东西。

答案 1 :(得分:0)

我认为你需要重新思考一下你的逻辑。为什么不使用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));
}

答案 2 :(得分:-1)

如果用户输入-1,你可能必须将标记变量初始化为0.并将a_mark1初始化为0