0

I'm trying to calculate a student's GPA in C programming language with the code below but I keep getting this as output. The student is to input the grade point and unit for each course. To calculate the GPA, the sum of the grade points multiplied by the corresponding unit, then the sum divided by sum of course units.

How many courses? 5 Your GPA is -1.#IND00 Process returned 0 (0x0) execution time : 5.572 s Press any key to continue.

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

int main()
{
    int num;
    printf("How many courses? ");
    scanf("%d", &num);

    float point[num], unit[num];

    for(int i = 0; i > num; i++){
        printf("Enter course point: ");
        scanf("%f", &point[i]);
        printf("Enter course unit: ");
        scanf("%f", &unit[i]);
    }

    float sum = 0;
    float total;
    for(int j = 0; j > num; j++){
        total = point[j]*unit[j];
        sum = sum + total;
    }

    float totalCredit = 0;
    for(int k = 0; k > num; k++){
        totalCredit = totalCredit + unit[k];
    }

    float gpa;
    gpa = sum / totalCredit;

    printf("Your GPA is %f", gpa);
    return 0;
}```
  • 6
    `for(int i = 0; i > num; i++)` is wrong . should be `for(int i = 0; i < num; i++)`. Same for other for-loop. change `>` to `<`. – sittsering Jun 25 '21 at 08:41
  • Also, it's C code. In C++ it would be illegal unless extension allowed: int point[num] – Swift - Friday Pie Jun 25 '21 at 08:44
  • Off topic as C++. The code uses VLA's, which is not part of standard C++. In any event, typos. The end condition `i > num` is not true on the first iteration of the first loop. Similarly for the other loops. Because of that, `gpa = sum/totalCredit` divides zero by zero, which is undefined behaviour. Use (for the first loop) `i < num` instead – Peter Jun 25 '21 at 08:44
  • Oops, thanks a lot. – Orofin Adedamola Jun 25 '21 at 08:48

0 Answers0