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

标签 c type-conversion pass-by-reference

问题是我无法获得正确的 GPA。我怀疑除法和类型转换。以下是我想要做的确切问题,但我想了解我的代码缺少什么。

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

编辑:从上面的问题可以看出,它应该计算 GPA,但是当我尝试分别给每门类(class) 50、60、70 分和 3 个学分时,我没有得到像 0 这样的有意义的输出。

(原始作业需要 30 名学生和 5 门类(class),但我定义了它们并将其更改为 2 门类(class) 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 打印为整数而不是 float 。改变

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

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

here获取 printf 使用的格式说明符列表。

关于c - C 语言的 GPA 计算器不起作用。可能是类型转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49479788/

相关文章:

c - 有序链表的链表

c++ - 如果你有两个同名函数(重载),并且有一个转换构造函数,转换构造函数会被调用吗?

ruby - 严格将字符串转换为整数(或零)

c++ - 段错误多线程 C++ 11

pass-by-reference - := and => in Ada?有什么区别

c - 使用 vsprintf 将字符串添加到另一种格式字符串的有效方法

c++ - C 和 C++ 可执行文件之间的区别?

c - 用户输入字符评估为常数字符

c - malloc 不转换为结构

c++ - 它是通过指针传递的吗?