I've made this program below to calculate the average mark of a student. Everything works well until I exit the program using -1, it is supposed to display the average of all students that have been entered and say Goodbye. If no students have been entered and you hit -1 at the start of the program it's supposed to just say goodbye but it still prints the average mark and some crazy number that seemingly comes out of no where.
Can anyone tell me where I'm going wrong with my code, how can I get it to just say goodbye when -1 is entered and how come the final average mark of all students entered is wrong? Thanks in Advance.
#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);
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;
}
Aucun commentaire:
Enregistrer un commentaire