如何在C语言中添加平均值计算作为动态数据结构?

问题描述 投票:0回答:1
void calculateAverage(struct List *list) {
        int count = 0;
        int sumGerman = 0, sumEnglish = 0, sumMathematik = 0;
        double aveGerman = 0, aveEnglish = 0, aveMathematik = 0;

        // Sum grades
        while (current != NULL) {
            struct student *currentStudent = current->data;
            // Add only positive values
            if (currentStudent->german != 0 && currentStudent->english != 0 &&
            currentStudent->mathematik != 0) {
                count++;
                sumMathematik += currentStudent->mathematik;
                sumGerman += currentStudent->german;
                sumEnglish += currentStudent->english;

            }
            // Select Next Node
            currentStudent = currentStudent->next;
        }
        aveMathematik = (count > 0) ? (double) sumMathematik / count: 0;
        aveGerman = (count > 0) ? (double) sumGerman / count: 0;
        aveEnglish = (count > 0) ? (double) sumEnglish / count: 0;

        printf("Mathe: %.2f\n", aveMathematik);
        printf("Deutsch: %.2f\n", aveGerman);
        printf("Englisch: %.2f\n", aveEnglish);
    }
    return 0;
}


编译器给我一个错误:此处不允许函数定义 无效计算平均(结构列表*列表){

我尝试删除结尾: 返回0; }

但是没有用。我该如何解决这个问题。

c data-structures dynamic
1个回答
0
投票

您似乎面临函数定义放置的问题。当您尝试在另一个函数或代码块中定义函数时,会出现错误“此处不允许函数定义”。

确保

calculateAverage
函数定义位于任何其他函数或块之外,并且正确放置在 C 程序的结构中。

// Include necessary headers

// Define structures and other data types if needed

// Function declarations if needed

// Define the structure for a student
struct student {
    int german;
    int english;
    int mathematik;
    // Other fields if needed
    struct student *next; // Assuming a linked list structure
};

// Define the structure for a linked list node
struct List {
    struct student *data;
    struct List *next;
};

// Function to calculate average grades
void calculateAverage(struct List *list) {
    // Function implementation
    // ...
}

// Main function or other functions if needed

int main() {
    // Code for your main function
    // ...

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.