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

C语言数组求和始终为0问题求助(含正态分布公式)

C语言数组求和异常与正态分布实现问题

我是编程新手,编写的包含数组的C语言代码计算数组总和时始终得到0值,同时尝试实现正态分布公式($y = \frac{1}{p \cdot \sqrt{2\pi}} \cdot e^{-\frac{1}{2} \cdot (\frac{a-x}{p})^2}$)相关的成绩概率密度计算,但求和与平均值的计算均未成功,代码如下:

/* 
a 代表学生的成绩
y 代表取得该成绩的概率密度
x 代表成绩的平均值
p 代表标准差,衡量成绩的离散程度
*/
#include <stdio.h>
#include <math.h>

float curve(int a, float p, float x);

int main()
{
    int i = 0, grade[100], sum = 0, n = 0, success = 0, fail = 0, low = 100, high = 0;
    float avg, sd;
    printf("enter the grade:\n");
    scanf("%d", &grade[i]);
    while (grade[i] != -1)
    { 
        // grade[i] >=60? success++:fail++;
        // if (grade[i]>high)
        // high=grade[i];
        // if (grade[i]<low)
        // low=grade[i];
        n++;
        i++;
        sum = sum + (grade[i]);
        printf("enter the grade:\n");
        scanf("%d", &grade[i]);
    }
    for (i = 0; i < n; i++)
    {
        printf("%d\n", grade[i] + 10);
    }
    avg = sum / n;
    // sd=pow(garde)
    printf("the sum of the class is %d\n", sum);
    printf("the average of the class is %f\n", avg);
    printf("the number of the student who passed the exam is %d\n", success);
    printf("the number of the student who failed the exam is %d\n", fail);
    printf("the highest mark in the class is %d\n", high);
    printf("the lowest mark in the class is %d\n", low);
    if (avg < 50)
    {
        printf("raise suggested\n");
        printf("the new average of the class is %f\n", (avg + 10));
    }
    else if (avg > 50 && avg < 60)
    {
        printf("curve suggested\n");
    }
    return 0;
}

float curve(int a, float p, float x)
{
    float y;
    y = (1 / (p * sqrt(2 * (3.14159)))) * pow((2.718), ((-1 / 2) * ((a - x) / p) * ((a - x) / p)));
    return y;
}

问题分析与修复方案

1. 数组求和始终为0的核心错误

循环逻辑顺序完全颠倒:

  • 原代码中,刚输入grade[i]后直接执行i++,然后累加grade[i]——此时grade[i]还未被赋值(是未初始化的垃圾值),且第一个输入的grade[0]从未被加入sum。
  • 修正:先累加当前的grade[i],再执行i++和下一次输入。

2. 其他功能失效问题

  • success/fail、low/high变量始终为初始值:相关判断代码被注释,取消注释即可正常统计。
  • 平均值计算错误:sum和n都是整数,sum/n会执行整数除法导致结果截断,需将其中一个转为浮点数,比如(float)sum / n。

3. 正态分布函数的精度与计算错误

  • 整数除法陷阱:1、-1/2都是整数运算,结果会被截断为0,需改为1.0、-0.5(或-1.0/2)。
  • e的近似值不够精确:用math.h提供的exp()函数代替pow(2.718, ...),计算更准确。
  • π的近似值可替换为M_PI(需确保编译器支持,或保留3.14159)。

修正后的完整代码

/* 
a 代表学生的成绩
y 代表取得该成绩的概率密度
x 代表成绩的平均值
p 代表标准差,衡量成绩的离散程度
*/
#include <stdio.h>
#include <math.h>

float curve(int a, float p, float x);

int main()
{
    int i = 0, grade[100], sum = 0, n = 0, success = 0, fail = 0, low = 100, high = 0;
    float avg, sd;
    printf("enter the grade:\n");
    scanf("%d", &grade[i]);
    while (grade[i] != -1)
    { 
        // 统计及格/不及格人数
        grade[i] >= 60 ? success++ : fail++;
        // 更新最高分
        if (grade[i] > high)
            high = grade[i];
        // 更新最低分
        if (grade[i] < low)
            low = grade[i];
            
        sum += grade[i]; // 先累加当前成绩
        n++;
        i++;
        printf("enter the grade:\n");
        scanf("%d", &grade[i]);
    }
    for (i = 0; i < n; i++)
    {
        printf("%d\n", grade[i] + 10);
    }
    avg = (float)sum / n; // 浮点数除法计算平均值
    
    // 计算标准差(补充原代码缺失的逻辑)
    float variance = 0.0;
    for (i = 0; i < n; i++) {
        variance += pow(grade[i] - avg, 2);
    }
    sd = sqrt(variance / n);
    
    printf("the sum of the class is %d\n", sum);
    printf("the average of the class is %.2f\n", avg);
    printf("the standard deviation is %.2f\n", sd);
    printf("the number of the student who passed the exam is %d\n", success);
    printf("the number of the student who failed the exam is %d\n", fail);
    printf("the highest mark in the class is %d\n", high);
    printf("the lowest mark in the class is %d\n", low);
    if (avg < 50)
    {
        printf("raise suggested\n");
        printf("the new average of the class is %.2f\n", (avg + 10));
    }
    else if (avg > 50 && avg < 60)
    {
        printf("curve suggested\n");
        // 示例:计算某成绩的概率密度
        int sample_grade = 70;
        float density = curve(sample_grade, sd, avg);
        printf("Probability density for grade %d is %.6f\n", sample_grade, density);
    }
    return 0;
}

float curve(int a, float p, float x)
{
    float y;
    // 修正整数除法,使用exp函数计算自然指数
    y = (1.0 / (p * sqrt(2 * M_PI))) * exp(-0.5 * pow((a - x)/p, 2));
    return y;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:44:55