C 中的 GPA 计算器不起作用。可能是类型转换错误

GPA Calculator in C, doesn't work. probably type conversion error

问题是我得不到正确的 GPA。我怀疑部门和类型转换。以下是我正在尝试做的确切问题,但我想了解我的代码缺少什么。

https://imgur.com/a/HFrIO - 他们没有提到学分,所以我只是向程序中的用户询问。

编辑:从上面的问题可以看出,它应该计算 GPA 但是当我分别尝试使用标记 50、60、70 和每门课程 3 个学分时,我没有得到像 0 这样的输出。

(最初的作业需要 30 名学生和 5 门课程,但我定义了它们并将它们更改为 2 门课程 1 名学生,以便在运行时对其进行测试。)

#include <stdio.h>

#define NUMBER_OF_COURSES 2 // Homework asks for 5, change at the end
#define NUMBER_OF_STUDENTS 1 // Homework asks for 30, change at the end

void calculateCourse(int *letterGradePoints, int *credit); // Func. prototype
float calculateStudentGpa(); // Func. prototype

int main()
{
    // Store gpa s of students in an array
    float studentGpas[NUMBER_OF_STUDENTS];
    int i;
    for(i = 0; i < NUMBER_OF_STUDENTS; ++i)
    {
        /*DEBUG*/printf("----\nPROGRAM IS IN MAIN FOR LOOP\n----\n");
        studentGpas[i] = calculateStudentGpa();
    }

    // Print all gpas
    for(i = 0; i < NUMBER_OF_STUDENTS; ++i)
    {
        printf("\nGPA of student %d is : %d        ", i + 1, studentGpas[i]);
    }
    // Find min gpa
    int min = studentGpas[0];
    for(i = 1; i < NUMBER_OF_STUDENTS; ++i)
        if(min > studentGpas[i])
            min = studentGpas[i];
    // Find max gpa
    int max = studentGpas[0];
    for(i = 1; i < NUMBER_OF_STUDENTS; ++i)
        if(max < studentGpas[i])
            max = studentGpas[i];

    // Print min and max
    printf("Min gpa is : %d        Max gpa is : %d", min, max);

    return 0;
}

float calculateStudentGpa()
{
    /*DEBUG*/printf("\nPROGRAM IS IN calculateStudentGpa FUNCTION");
    /* Dealing with a single students gpa */

    int credit[NUMBER_OF_COURSES];
    int letterGradePoints[NUMBER_OF_COURSES];

    int i; int debug = 0;
    for(i = 0; i < NUMBER_OF_COURSES; ++i)
    {
        /*DEBUG*/ if(debug == 0) { /*DEBUG*/printf("\nPROGRAM IS IN calculateStudentGpa FUNCTION for LOOP\n"); debug++; } // Print this one once

        calculateCourse(&letterGradePoints[i], &credit[i]);

        /*DEBUG*/printf("\nPROGRAM IS IN calculateStudentGpa FUNCTION for LOOP\n");
        /*DEBUG*/printf("\n[DEBUG] i in calculateStudentGpa : %d", i);
        /*DEBUG*/printf("\n[DEBUG] letterGradePoints[i] in calculateStudentGpa : %d", letterGradePoints[i]);
        /*DEBUG*/printf("\n[DEBUG] credit[i] in calculateStudentGpa : %d\n", credit[i]);
    }
    /*DEBUG*/printf("\nPROGRAM HAS ..PASSED.. calculateStudentGpa FOR LOOP");

    float gpa; float up = 0; float down = 0;

    /* Either we need to have
     * (i = 0; i < NUMBER_OF_COURSES; ++i) AND indexes of arrays as i
     * or
     * (i = 1; i <= NUMBER_OF_COURSES; ++i) AND indexes of arrays as i - 1
     * below in 2 for loops!!!!
     */
    for(i = 0; i < NUMBER_OF_COURSES; ++i)
    {
        up += (letterGradePoints[i] * credit[i]); // Might need (float)
    }

    for(i = 1; i <= NUMBER_OF_COURSES; ++i)
    {
        down += credit[i - 1];
    }

    gpa = up / down;

    /* We are done with a single student, we need all 30 students */

    return gpa;
}

void calculateCourse(int *letterGradePoints, int *credit)
{
    /*DEBUG*/printf("\n--------------------------------------");
    /*DEBUG*/printf("\nVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV");
    /*DEBUG*/printf("\nPROGRAM IS IN calculateCourse FUNCTION\n");

    /* Dealing with a single course */

    int labMark, midtermMark, finalMark;

    printf("\nEnter lab mark : ");
    scanf("%d", &labMark);
    printf("Enter midterm mark : ");
    scanf("%d", &midtermMark);
    printf("Enter final mark : ");
    scanf("%d", &finalMark);

    float average =
    (
        (float) (0.5 * finalMark)
        +
        (float) (0.4 * midtermMark)
        +
        (float) (0.1 * labMark)
    ); // Might need (float)

    /*DEBUG*/printf("\n[DEBUG] average : %f", average);

    // int letterGradePoints; // I decided to use pass by reference in order to return 2 values

    if(average >=  0) *letterGradePoints = 0;
    if(average >= 50) *letterGradePoints = 1;
    if(average >= 60) *letterGradePoints = 2;
    if(average >= 70) *letterGradePoints = 3;
    if(average >= 80) *letterGradePoints = 4;
    /*DEBUG*/printf("\n[DEBUG] letterGradePoints in calculateCourse : %d\n", *letterGradePoints);

    // int credit; // I decided to use pass by reference in order to return 2 values

    printf("Enter the credit for the course : ");
    scanf("%d", credit);
    /*DEBUG*/printf("\n[DEBUG] *credit in calculateCourse : %d\n", *credit);
    /*DEBUG*/printf("/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\\n");
    /*DEBUG*/printf("--------------------------------------\n");

    /* We are done with a single course, we need all 5 courses for the gpa */
}

您将 GPA 打印为整数而不是浮点数。变化

printf("\nGPA of student %d is : %d", i + 1, studentGpas[i]);

printf("\nGPA of student %d is : %f", i + 1, studentGpas[i]);

here 中查找 printf 使用的格式说明符列表。