0

課題:

体操競技または飛び込み競技では、各競技者の得点は、最低得点と最高得点を切り捨て、残りの得点を加算することによって計算されます。審査員は 1 から 10 までの点数を与えます。1 が最も低く、10 が最も高くなります。入力ファイル (5 ~ 10 のスコアがあります) から審査員のスコアを読み取り、競技者が受け取ったポイントを小数点以下 2 桁でフォーマットして出力するプログラムを作成します。たとえば、入力ファイルには以下が含まれます: 9.2 9.3 9.0 9.9 9.5 9.5 9.6 9.8 ドロップされたスコア: 9.0 および 9.9 獲得したポイント: 56.90 スコアの正確な数がわからないため、入力ファイルから読み取られたスコアをカウントする必要があります。これを実際のサイズ、または配列に実際に格納されている要素の数として使用します。ファイルからデータを読み取る方法の詳細については、講義ノートを参照してください。

私のコード:

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cassert>

using namespace std;

void OpenFile(ifstream &ScoresFile);
void GetScores(ifstream &ScoresFile, double Scores[], int &size);
void MinMax(double Scores[], int size);

int main()
{
    ifstream ScoresFile;
    double Scores[10];
    int size;

    OpenFile(ScoresFile);
    GetScores(ScoresFile, Scores, size);
    MinMax(Scores, size);

    return(0);
}

// This function will prompt the user to name the input file that will store the scores.
void OpenFile(ifstream &ScoresFile)
{
    string infilename;

    cout << "Please enter the name of your input file: ";
    cin >> infilename;

    ScoresFile.open(infilename.c_str());
    assert(ScoresFile);
}

// This function will get the scores from the file and store them in an array.
void GetScores(ifstream &ScoresFile, double Scores[], int &size)
{
    int i = 0;

    do
    {
        ScoresFile >> Scores[i];
        i++;
    }
    while(ScoresFile);

    size = i;
}

// This function will figure out the minimum and maximum scores, and remove them from the array.
void MinMax(double Scores[], int size)
{
    double minScore = Scores[0];
    double maxScore = Scores[0];
    double points = 0;  
    int i;

    for(i = 0; i < 10; i++)     
    {
        points += Scores[i];
        if (Scores[i] > maxScore)
            maxScore = Scores[i];
        if (Scores[i] < minScore)
            minScore = Scores[i];
    }

    points = (Scores[i] - minScore - maxScore);

    cout << fixed << showpoint << setprecision(1) << points << endl;
}

私の問題: したがって、ファイル情報を取り込み(割り当てから同じ番号をコピーしてテキストファイルに配置しました)、それを配列に入れます。それはminScoreとmaxScoreを計算します(私は...それをテストし、配列のオーバーロードであると私が信じている追加のガベージコードとともに機能したと信じています)。この全体で行う必要があるのは、方程式から minScore と maxScore を削除し、残りの数値を追加して合計スコアを作成し、それを画面に出力することです。私はその部分で立ち往生しています。コードをそのまま使用してプログラムを実行すると、-9.9 の出力が得られます。

私が非常に感謝している正しい方向への助け/プッシュ。超凝った高度なコーディングはやめてください。プロジェクトが言っていることは、まさに教師が望んでいることです。:-) さらに、私はそれを理解していません...

4

1 に答える 1

0

MinMax 関数で

points = (Scores[i] - minScore - maxScore);

i の値はここでは 10 であるため、範囲外の配列にアクセスしようとしています。

代わりに、次のことを行うことができます。

points -= minScore + maxScore;
于 2013-10-25T14:09:00.800 に答える