I have this program that basically calculates the student's midterm grade. It lets the user to input the grades for corresponding activities. Then, with the inputs from the user, it computes the average for seat works, assignments, lab activities, quizzes, and exams. All of these computed averages are used to compute the midterm grade.
So I used arrays and function in this.
#include <stdio.h>
#include <stdlib.h>
#define TOTAL_ASSIGNMENTS 3
#define TOTAL_SEATWORKS 2
#define TOTAL_LAB_ACT 3
#define TOTAL_QUIZZES 2
float assignment(float);
float seatwork(float);
float labActivity(float);
float quiz(float);
float longQuiz(float);
float midterms(float);
float classStanding(float);
int main()
{
int studentYear;
float assignAverage, swAverage, labActivityAverage, qAverage, lq, mt;
char studentNum[20], studentName[20], studentCourse[20];
printf("\n STUDENT NUMBER: ");
gets(&studentNum);
printf(" STUDENT NAME: ");
gets(&studentName);
printf(" COURSE: ");
gets(&studentCourse);
printf(" YEAR: ");
scanf("%i", &studentYear);
printf("\n");
assignAverage = assignment(assignAverage);
printf("\n");
swAverage = seatwork(swAverage);
printf("\n");
labActivityAverage = labActivity(labActivityAverage);
printf("\n");
qAverage = quiz(qAverage);
printf("\n");
lq = longQuiz(lq);
printf("\n");
mt = midterms(mt);
printf("\n");
printf("\n ========================================");
printf("\n STUDENT NUMBER: %s", studentNum);
printf("\n STUDENT NAME: %s", studentName);
printf("\n COURSE: %s", studentName);
printf("\n YEAR: %i", studentYear);
printf("\n \n \t ASSIGNMENT AVERAGE: %.2f", assignAverage);
printf("\n \t SEATWORK AVERAGE: %.2f", swAverage);
printf("\n \t LAB ACTIVITIES AVERAGE: %.2f", labActivityAverage);
printf("\n \t QUIZZES AVERAGE: %.2f", qAverage);
printf("\n \t LONG QUIZ: %.2f", lq);
float cs = (float) assignAverage + (float) swAverage + (float) labActivityAverage + (float) qAverage + (float)lq;
cs = cs * 0.60;
printf("\n \n \t CLASS STANDING: %.2f", cs);
printf("\n \t MIDTERM EXAM GRADE: %.2f", mt);
float midtermGrade = (float) cs + (float) mt;
printf("\n \n \t MIDTERM GRADE: %.2f", midtermGrade);
printf("\n ======================================== \n");
}
float assignment(float assignAverage)
{
float sum, assign[TOTAL_ASSIGNMENTS];
for (int i = 0; i < TOTAL_ASSIGNMENTS; i++)
{
printf("\t ASSIGNMENT #%i GRADE: ", i + 1);
scanf("%f", &assign[i]);
sum += assign[i];
}
assignAverage = sum / TOTAL_ASSIGNMENTS;
assignAverage = assignAverage * 0.10;
return assignAverage;
}
float seatwork(float swAverage)
{
float sum, sw[TOTAL_SEATWORKS];
for (int i = 0; i < TOTAL_SEATWORKS; i++)
{
printf(" \t SEATWORK #%i SCORE: ", i + 1);
scanf("%f", &sw[i]);
sum += sw[i];
}
swAverage = sum / TOTAL_SEATWORKS;
swAverage = swAverage * 0.15;
return swAverage;
}
float labActivity(float labActivityAverage)
{
float sum, labAct[TOTAL_LAB_ACT];
for (int i = 0; i < TOTAL_LAB_ACT; i++)
{
printf("\t LABORATORY ACTIVITY #%i GRADE: ", i + 1);
scanf("%f", &labAct[i]);
sum += labAct[i];
}
labActivityAverage = sum / TOTAL_LAB_ACT;
labActivityAverage = labActivityAverage * 0.25;
return labActivityAverage;
}
float quiz(float qAverage)
{
int i;
float sum, q[TOTAL_QUIZZES];
for (i = 0; i < TOTAL_QUIZZES; i++)
{
printf("\t QUIZ #%i SCORE: ", i + 1);
scanf("%f", &q[i]);
sum += q[i];
}
qAverage = sum / TOTAL_QUIZZES;
qAverage = qAverage * 0.20;
return qAverage;
}
float longQuiz(float lq)
{
printf("\t LONG QUIZ SCORE: ");
scanf("%f", &lq);
lq = lq * 0.30;
return lq;
}
float midterms(float mt)
{
printf(" \t MIDTERMS EXAMS SCORE: ");
scanf("%f", &mt);
mt = mt * 0.40;
return mt;
}
Some of the values printed are correct. But there are some that don't, specifically the SEATWORK AVERAGE, LAB ACTIVITIES AVERAGE, CLASS STANDING, and the MIDTERM GRADE.
The expected output should be:
STUDENT NUMBER: 1234
STUDENT NAME: HARRY
COURSE: BETCPET
YEAR: 1
ASSIGNMENT AVERAGE: 9.90
SEATWORK AVERAGE: 13.80
LAB ACTIVITIES AVERAGE: 24.00
QUIZZES AVERAGE: 18.40
LONG QUIZ: 29.40
CLASS STANDING: 57.30
MIDTERM EXAM GRADE: 39.60
MIDTERM GRADE: 96.30
But it instead prints this out. The SEATWORK AVERAGE, LAB ACTIVITIES AVERAGE, CLASS STANDING, and MIDTERM GRADE are far from their supposed to be output.
STUDENT NUMBER: 1234
STUDENT NAME: HARRY
COURSE: BETCPET
YEAR: 1
ASSIGNMENT AVERAGE: 9.90
SEATWORK AVERAGE: 36.15
LAB ACTIVITIES AVERAGE: 64.17
QUIZZES AVERAGE: 18.50
LONG QUIZ: 29.40
CLASS STANDING: 94.87
MIDTERM EXAM GRADE: 39.60
MIDTERM GRADE: 134.47
As you can see in my source code, I used the same way of calculating the averages for the seatwork, assignment, lab activities, and quizzes. I don't get how their average is different from what it's supposed to be.
Is the problem in the functions? Or the way how I call the functions?