2016-03-31 4 views
-4

誰かが私に答えを渡すとは思っていませんが、私はいくつかのガイダンスを探しています。私のC++クラスでは、先週、5人の裁判官にそれぞれユーザーが入れなければならなかったスコアを持っていたプログラムを書いたので、最高と最低のスコアを使わずに平均を見つける必要がありました。ループとif文を使ってこれを行いました。C++入門アレイを使用してプログラムを改訂するためのヘルプを探しています

私の先生は私たちに戻ってアレイを使用するように頼んだが、元のコードをちょっと修正するだけでいいが、現在は5〜20人の裁判官がいるので、その番号を入力してください。どのスポットを配列で置き換えることができるのか分かりません。

私はすでに人が判事の額に入れることができる場所を持っていますが、それを配列に入れる方法もわからないので、審査員は現時点で未使用の変数です。これが私のコードの外観です今のように。前もって感謝します!

#include <iostream> 

using namespace std; 

//Function prototypes 

void getJudgeData(double &x); 

double findLowest(double ,double ,double ,double,double); 

double findHighest(double,double,double,double,double); 

void calcAverage(double,double,double,double,double); 

double judges; 
//Program begins with a main function 

int main()  
{ 
    //Declare variables 

    double judgeScore1,judgeScore2,judgeScore3; 

    double judgeScore4,judgeScore5; 

    cout<<"Scores given by all five judges: \n\n"; 

    //Function calls to get each judge data 
    cout<< "How many Judges are there? (Must be 5 to 20): "; 
    cin>> judges; 

    while(judges<5||judges>20){ 
     cout<< "Sorry there must be 5 to 20 judges\n"; 
     cout<< "How many Judges are there? (Must be 5 to 20): "; 
     cin>> judges; 

     getJudgeData(judgeScore1); 

     getJudgeData(judgeScore2); 

     getJudgeData(judgeScore3); 

     getJudgeData(judgeScore4); 

     getJudgeData(judgeScore5); 

     //Function call to obtain average 

     calcAverage(judgeScore1,judgeScore2,judgeScore3, 

     judgeScore4,judgeScore5); 

     //Pause the system for a while  
    } 
} 
//Method definition of getJudgeData 

void getJudgeData(double &x)  
{  
    //Prompt and read the input from the user and check //input validation 

    cout<<"Enter score of a Judge (you will do this 5 times): "; 

    cin>>x; 

    while (x < 0 || x > 10)  
    { 
     cout<<"ERROR: Do not take judge scores lower than 0 or higher than 10.Re-enter again: "; 

     cin>>x;  
    } 

} 

//Method definition of findLowest 

double findLowest(double a,double b,double c, double d,double e)  
{  
    double lowest; 

    lowest=a; 

    if (b<lowest)  
     lowest=b; 

    if (c<lowest)  
     lowest=c; 

    if (d<lowest)  
     lowest=d; 

    if (e<lowest) 

    lowest=e; 

    return lowest;  
} 


//Method definition of findHighest 

double findHighest(double a,double b,double c, double d,double e)  
{  
    double highest; 

    highest=a; 

    if (b>highest) 

    highest=b; 

    if (c>highest)  
     highest=c; 

    if (d>highest)  
     highest=d; 

    if (e>highest)  
     highest=e; 

    return highest; 
} 




void calcAverage(double a,double b,double c,double d, double e)  
{ 

    //Declare variables 

    double lowest; 

    double highest; 

    double sum; 

    double average; 

    //Function call to retrieve lowest score 

    lowest=findLowest(a,b,c,d,e); 

    //Function call to retrieve highest score 

    highest=findHighest(a,b,c,d,e); 

    //Calculate the total sum 

    sum=a+b+c+d+e; 

    //Subtract highest and lowest scores from the total 

    sum=sum-highest; 

    sum=sum-lowest; 

    //Calculate the average of the three number after 

    //dropping the highest and lowest scores 

    average=sum/3; 

    //Display output 

    cout<<"Highest score of five numbers:"<<highest<<endl; 

    cout<<"Lowest score of five numbers:"<<lowest<<endl; 

    cout<<"Average when highest and lowest scores are removed: "<<average<<endl;  
} 
+0

私は裁判官のトラックスコアを維持するために、配列を使用することをお勧めしたい、 'ダブルjudgeScore [nbrOfJudges]'のように。今度は、judgeScore [2]によって審査員スコアにアクセスできます。 std :: vectorを使用することが許されているなら、配列の代わりにそれをお勧めします。 – pingul

+0

あなたの質問は何ですか?コードを正しくインデントします。 –

答えて

1

judgeScore1、...、judgeScore5に保存するのではなく、ジャッジのスコアを保持するために配列を使用します。それは裁判官の数であるとして

double judgeScore[20]; 

変数「判断」は、「INT」として宣言されるべきである:配列のサイズは、裁判官の最大数は20であるべきです。

この関数は、5倍の値ではなく、double型の配列を受け入れる必要があります。だから、代わりに:

double findLowest(double ,double ,double ,double,double); 

関数は次のようになる。

double findLowest(double s[]); 

機能の本体は、計算を行うために「for」ループの限界として変数「判断」を使用する必要があります。

0

配列を使用するように関数を変更します。

#include <iostream> 

double get_max(double judge_scores[], int size) 
{ 
    double max = judge_scores[0]; 
    for (int i = 0; i < size; ++i) 
    { 
     if (judge_scores[i] > min) 
     { 
      max = judge_scores[i]; 
     } 
    } 
    return max; 
} 

変更し、1つのようなあなたのすべての機能:たとえば、ここで、アレイで使用するためget_max機能です。私はあなたの問題をコード化し、ここで私が得たmainです:

int main() 
{ 
    const int MAX_NUM_JUDGES = 20; 

    // make everything in the array 0 
    double judge_scores[MAX_NUM_JUDGES] = { 0 }; 

    // make it return the "amount" of array you need- the number of judges 
    int num_judges = get_num_judges(); 

    // fill the array up to that "amount" 
    // internally uses int get_judge_data() to get a score in the correct range 
    fill_score_array(judge_scores, num_judges); 

    // get the average. Internally, this 
    // - computes the sum 
    // - calls the max and min functions and subtracts those from the sum 
    // - divides that sum by (num_judges - 2) 
    // (because you're throwing away the max and min) and returns that. 
    double avg = calculate_curved_average(judge_scores, num_judges); 

    //print everything 
    std::cout << " Highest: " << get_max(judge_scores, num_judges) << std::endl; 
    std::cout << " Lowest: " << get_min(judge_scores, num_judges) << std::endl; 
    std::cout << " Avg when highest and lowest scores are removed: " << avg << std::endl; 

    // If you're not on Windows, don't use this :) 
    system("PAUSE"); 
}