0

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?

Liyanna
  • 119
  • 10
  • Please reduce it to a [mcve]. – Eugene Sh. Jun 29 '22 at 17:05
  • 2
    To begin with `gets(&studentNum)` is wrong for *two* reasons: The first and most important is that `gets` is so [dangerous](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) that it has even been removed from the C standard, it's no longer a valid part of C. Use e.g. [`fgets`](https://en.cppreference.com/w/c/io/fgets) instead. The second problem is the pointer-to operator `&`, it makes you pass a value of the wrong type to the function. – Some programmer dude Jun 29 '22 at 17:07
  • As for your problem, this might be a good time to learn how to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your programs. More specifically how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Jun 29 '22 at 17:09
  • 1
    On another note, when running your program from a terminal or command-line environment, output to `stdout` (which is where `printf` is writing its output) is *line* buffered, meaning output is actually written on newline. By using leading newline you write the *previous* line, not the current output in the current `printf` call. Please make it a habit to print *trailing* newlines. – Some programmer dude Jun 29 '22 at 17:10
  • 2
    In `float assignment(float assignAverage)` you have `float sum` and then `sum += assign[i];` with a compiler warning *uninitialized local variable 'sum' used* and this isn't the only one - all your functions neglect this. Enable compiler warnings, save yourself a lot of time. Local variables must be **explicitly initialised**. – Weather Vane Jun 29 '22 at 17:14
  • 1
    Aside: in those functions you are using arrays unnecessarily. You input to the array element, add that to the sum, and have no further use for the array element. Also, you pass an argument to those functions, which is only overwritten and used as a local variable. – Weather Vane Jun 29 '22 at 17:19

1 Answers1

0
#include <stdio.h>
#include <stdlib.h>

#define MAX_INPUT 20
#define TOTAL_ASSIGNMENTS 3
#define TOTAL_SEATWORKS 2
#define TOTAL_LAB_ACT 3
#define TOTAL_QUIZZES 2

void assignment(float *assignAverage);
void seatwork(float *swAverage);
void labActivity(float *labActivityAverage);
void quiz(float *qAverage);
void longQuiz(float *lq);
void midterms(float *mt);
void classStanding(float *computedClassStanding);

int main()
{
    char studentNum[MAX_INPUT], studentName[MAX_INPUT], studentCourse[MAX_INPUT];
    int studentYear;
    float ass = 0, sw = 0, labAct = 0, q = 0, longq = 0, midt = 0, cs = 0, midtermGrade = 0;

    printf("\n STUDENT NUMBER: ");
    fgets(studentNum, MAX_INPUT, stdin);
    printf(" STUDENT NAME: ");
    fgets(studentName, MAX_INPUT, stdin);
    printf(" COURSE: ");
    fgets(studentCourse, MAX_INPUT, stdin);
    printf(" YEAR: ");
    scanf("%i", &studentYear);

    printf("\n");

    assignment(&ass);
    printf("\n");
    seatwork(&sw);
    printf("\n");
    labActivity(&labAct);
    printf("\n");
    quiz(&q);
    printf("\n");
    longQuiz(&longq);
    printf("\n");
    midterms(&midt);

    cs = (float) ass + (float) sw + (float) labAct + (float) q + (float)longq;
    cs = cs * 0.60;

    midtermGrade = (float) cs +  (float) midt;


    printf("\n ======================================== \n");

    printf(" STUDENT NUMBER: %s", studentNum);
    printf(" STUDENT NAME: %s", studentName);
    printf(" COURSE: %s", studentName);
    printf(" YEAR: %i", studentYear);

    printf("\n \t ASSIGNMENT AVERAGE: %.2f ", ass);
    printf("\n \t SEATWORK AVERAGE: %.2f ", sw);
    printf("\n \t LABORATORY ACTIVITY AVERAGE: %.2f ", labAct);
    printf("\n \t QUIZZES AVERAGE: %.2f", q);
    printf("\n \t LONG QUIZ: %.2f", longq);

    printf("\n \n \t CLASS STANDING: %.2f", cs);
    printf("\n \t MIDTERM EXAM GRADE: %.2f", midt);

    printf("\n \n \t MIDTERM GRADE: %.2f", midtermGrade);

    printf("\n ======================================== \n");

    return 0;
}

void assignment(float *assignAverage)
{
    float assign[TOTAL_ASSIGNMENTS], sum = 0;

    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;
}

void seatwork(float *swAverage)
{
    float sum = 0, 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;
}

void labActivity(float *labActivityAverage)
{
    float sum = 0, 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;
}

void quiz(float *qAverage)
{
    float sum = 0, q[TOTAL_QUIZZES];

    for (int 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;
}

void longQuiz(float *lq)
{
    float longQuizScore = 0;
    printf("\t LONG QUIZ SCORE: ");
    scanf("%f", &longQuizScore);

    *lq = longQuizScore * 0.30;

    return;
}

void midterms(float *mt)
{
    float mtExamScore = 0;
    printf(" \t MIDTERMS EXAMS SCORE: ");
    scanf("%f", &mtExamScore);

    *mt = mtExamScore * 0.40;

    return;
}
Liyanna
  • 119
  • 10