【问题标题】:c++ SAT score reader assignment, compute standard deviation bugc++ SAT分数阅读器分配,计算标准差错误
【发布时间】:2018-07-24 12:10:02
【问题描述】:

我正在开发一个读取 20 SAT 分数的程序。分数将被读取并存储到二维数组中。我写了两种我认为是我的问题的根源的方法。我确定的一个是计算 STD 函数。我收到错误消息:

1 [main] satscores_proj1 1920 cygwin_exception::open_stackdumpfile:将堆栈跟踪转储到 satscores_proj1.exe.stackdump

我正在使用带有 cygwin 编译器包的 netbeans,但是当我尝试使用 DevC++ 运行它时,弹出以下消息:

C:\Dev-Cpp\HW Assignments\satScores.cpp 在函数void compute_std(int (*)[2], int, int, double&, double&)' call of overloadedpow(int, int)' 是不明确的

#include <cstdlib>
#include <iostream>
#include <iomanip> 
#include <string>
#include <fstream>
#include <cmath>
#include <cctype>

using namespace std;

int main() 
{
    int sat[10][2], mathAvg, verbAvg;
    double mathStd, verbStd;

    void describe_program();
    void read_scores(int sat[10][2]);
    void compute_means(int sat[10][2], int& mathAvg, int& verbAvg);
    void compute_std(int sat[10][2],int mathAvg, int verbAvg, double& mathStd, double& verbStd) ;
    void show_results(int sat[10][2], int mathAvg, int verbAvg, double mathStd,  double verbstd);
    bool again();

    describe_program();
    read_scores(sat);
    compute_means(sat, mathAvg, verbAvg);
    compute_std(sat, mathAvg, verbAvg,  mathStd, verbStd);
    //show_results(sat, mathAvg, verbAvg, mathStd, verbStd);
    //again();
    return 0;
}

/*
 * 
 */

void compute_means(int sat[10][2], int& mathAvg, int& verbAvg)
{
    int mathScoreSum =0;
    int mathCount = 0;
    int verbScoreSum = 0;
    int verbCount = 0;

    for(int i = 0; i<10; i++)    
    {
        for(int j = 0; i<2; j++)
        {
            if(j==0){
                mathScoreSum += sat[i][j];
                mathCount++;
            }

            else{
                verbScoreSum += sat[i][j];
                verbCount++;
            }
        }
    }
    mathAvg = mathScoreSum / mathCount;
    verbAvg = verbScoreSum / verbCount;
}


void compute_std(int sat[10][2],int mathAvg, int verbAvg, double& mathStd, double& verbStd) 
{
    double mathVariance = 0;
    double verbVariance = 0;

    for(int i = 0; i<10; i++)    
    {
        for(int j = 0; i<2; j++)
        {

            if(j==0){
                mathVariance += pow((sat[i][j] - mathAvg), 2);
            }

            else{
                verbVariance += pow((sat[i][j] - mathAvg), 2);
            }

            mathStd = sqrt(mathVariance/9);
            verbStd = sqrt(verbVariance/9);
        }
    }
}

有人熟悉这两个错误吗?

【问题讨论】:

  • 是时候加载你的调试器了。

标签: c++ function math netbeans dev-c++


【解决方案1】:

您的分段错误是由for(int j=0;i&lt;2;j++) 引起的,请注意i。如果您使用调试器,则很明显。使用调试器:)

编译错误是由函数pow的多个声明引起的。我会尝试做::pow(etc...你也可以做像(double)variable这样的显式转换来消除重载问题。

此外,您不应该对从不执行相同代码两次的代码使用循环:

    for(int j = 0; j<2; j++)
    {
        if(j==0){
            mathScoreSum += sat[i][j]; //<--first, j is 0
            mathCount++;
        }

        else{
            verbScoreSum += sat[i][j]; //<--second, j is 1
            verbCount++;
        }
    }

应该是:

mathScoreSum += sat[i][0]; //<--first iteration of loop
mathCount++; //<--first iteration of loop
verbScoreSum += sat[i][1]; //<--second iteration of loop
verbCount++; //<--second iteration of loop

(或者,您可以使用 ScoreSum[2]Count[2] 代替当前的 4 个目标变量并分配给循环内的变量。)

这是一个删除不必要的 j 循环的完整示例:

void compute_std(int sat[10][2],int mathAvg, int verbAvg, double& mathStd, double& verbStd)
{
    double mathVariance = 0;
    double verbVariance = 0;

    for(int i = 0; i<10; i++)
    {
        mathVariance += pow((sat[i][0] - mathAvg), 2);
        verbVariance += pow((sat[i][1] - verbAvg), 2);
    }
    mathStd = sqrt(mathVariance/9);
    verbStd = sqrt(verbVariance/9);
}

【讨论】:

  • 你指的是哪个?我不需要for循环来循环遍历数组以增加计数并将所有mathScoreSums和verbScoreSums相加吗?我同意您对 ScoreSum[2] 和 Count[2] 的看法,但我仍然不明白为什么我不需要 for 循环。
  • @DarnellMcdonald 我指的是我引用的 j 循环。它只有两次迭代...01...并为它们执行不同的代码(因为你的 if 语句)。你看到“应该”的代码是如何exact 做同样的事情的吗?你的另一个 j 循环也可以消失。
  • @DarnellMcdonald - 我在我的答案中添加了一些 cmets,希望能为您澄清。
  • 另外你的verbVariance += 使用的是mathAvg 而不是verbAvg
  • 我只是想让你知道它是一个双数组。您需要遍历 double 数组,因此需要 j 循环。我实际上只是测试了程序的每个方面,它就像 j 循环的魅力一样。将信息发送到文本文件和 j 循环中的所有内容
【解决方案2】:

您是using namespace std,这在尝试区分std::pow 函数和#include &lt;cmath&gt; 中的pow 函数时会产生潜在冲突。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 2016-04-14
    • 2015-12-27
    • 2016-02-13
    • 2010-09-23
    • 2015-11-20
    • 2018-07-20
    • 1970-01-01
    相关资源
    最近更新 更多