You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

二叉搜索树查询选课学生成绩均值结果为0的问题排查

Hey Devin, let's dig into that average grade issue you're facing—sounds like everything else is clicking except this one calculation, which is super frustrating! Let's break down the most likely culprits based on what you described.

1. Verify Your Grade Total Accumulation Logic

First off, double-check that you're actually adding up each grade correctly when calculating the total. It's easy to slip up here:

  • Did you use total += current->grade instead of accidentally reassigning with total = current->grade? The latter would overwrite the total every time, leaving you with only the last grade (or 0 if the list is empty).
  • Is your loop properly traversing the entire grades linked list? Make sure your loop condition is current != NULL and that you're moving to the next node with current = current->next inside the loop. Example of what correct code might look like:
    int total_grades = 0;
    int grade_count = 0;
    GradeNode* temp = student->grade_list;
    while (temp != NULL) {
        total_grades += temp->score;
        grade_count++;
        temp = temp->next;
    }
    
2. Watch Out for Integer Division & Type Mismatches

Your hunch about integer types is spot-on—this is a super common pitfall!

  • If both total_grades and grade_count are integers, dividing them will use integer division, which truncates decimals. But if your result is always 0, that suggests either:
    • The total_grades variable is staying at 0 (meaning your accumulation logic is broken), or
    • grade_count is 0 (you're not counting grades correctly, or the student has no grades assigned), leading to undefined behavior (but some compilers/runtimes might output 0 here).
  • To fix this, cast one of the values to a floating-point type before division to get a decimal average:
    double average = (double)total_grades / grade_count;
    
3. Confirm Your Grades Linked List Actually Contains Data

Even though you can print student info, take a second to verify that the grades you think are stored are actually there. Add a quick debug print before calculating the average to check the total and count:

printf("Debug: %s's total grades = %d, count = %d\n", student->name, total_grades, grade_count);

If this outputs total=0 and count=0, your grades aren't being added to the linked list properly—go back and check how you're appending grades to the student's list.

4. Check BST Traversal (If Calculating Class Average)

If you're calculating an average across the entire roster, make sure your binary search tree traversal is visiting every student node. It's easy to miss a left or right subtree in recursive or iterative traversal code, which could mean you're only sampling a subset of students (or none at all, leading to a total of 0).


内容的提问来源于stack exchange,提问作者Devin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:29:21