2016-10-15 34 views
-1

私は宿題に問題があり、教授は応答していません。 getHighestgetLowest関数の月の文字列名を返すように配列を取得するにはどうすればよいですか?intを文字列に変換する方法並列配列

私は詳細を追加する必要があることを伝え続けます。このコードを削除したので、現在はメインとgetLowestgetHighestの機能が残ります。それでも私はまだ詳細が必要だと言いますので、私は質問を提出できるように詳細を追加するためにこれをタイプしています。

ありがとうございます!

#include <iostream> 
#include <string> 
#include <iomanip> 
using namespace std; 

//Function prototypes 
void getRainfall(double[], int); //To retrieve the user input. 
double getTotal(double[], int); //To total the rainfall amounts. 
double getAverage(double[], int); //To get the average rainfall. 
double getLowest(double[], int, int&); //Returns the lowest value, provides     the index of the lowest value in the last parameter. 
double getHighest(double[], int, int&); //Returns the highest value,  provides the index of the highest value in the last parameter. 

//Global Variable 
const int NUM_MONTHS = 12; 

//Array names 
double rainfall[NUM_MONTHS]; 
string month[NUM_MONTHS] = { "January", "February", "March", "April", "May",  "June", "July", "August", "September", "October", "November", "December" };   //Month array to hold names of months 

int main() 
{ 
    //Declare variables 
    double total, average; 
    int low, high; 
    string lowMonth, highMonth; 

    //Call Functions 
    getRainfall(rainfall, NUM_MONTHS); //To retrieve the user input. 
    total = getTotal(rainfall, NUM_MONTHS); //To total the rainfall amounts. 
    average = getAverage(getTotal, NUM_MONTHS); //To get the average rainfall. 
    lowMonth = getLowest(rainfall, NUM_MONTHS, low); //Returns the lowest value, provides the index of the lowest value in the last parameter. 
    highMonth = getHighest(rainfall, NUM_MONTHS, high); //Returns the highest value, provides the index of the highest value in the last parameter. 

    //Display the following: 
    cout << "The total rainfall for the year is: " << total << endl; 
    cout << "The average rainfall for the year is: " << fixed << showpoint << setprecision(2) << average << endl; 
    cout << "Least amount of rainfall fell in: " << highMonth << endl; 
    cout << "Most amount of rainfall fell in: " << lowMonth << endl; 

    return 0; 
} 


//******************************************************************************************* 
//    double getLowest(double amount[], int size)         * 
// Returns the lowest value, provides the index of the lowest value in the last parameter. * 
//******************************************************************************************* 

double getLowest(double amount[], int NUM_MONTHS, int &low) 
{ 
    low = amount[0]; //Variable to hold lowest value. 
    int lowMonth = 0; //Set low value to intial rainfall value. //Variable to return month element location. 

    for (int index = 0; index < NUM_MONTHS; index++) 
    { 
     if (amount[index] < low) 
     { 
      low = amount[index]; 
      lowMonth = index; 
     } 
    } 

    return lowMonth; 
} 

//********************************************************************************************* 
//    double getHighest(double amount[], int size)         * 
// Returns the highest value, provides the index of the highest value in the last parameter. * 
//********************************************************************************************* 

double getHighest(double amount[], int NUM_MONTHS, int &highMonth) 
{ 
    //high = amount[0]; //Variable to hold highest value 
    months = amount[0]; 
    double highMonth = 0; //Variable to hold highest value 

    for (int index = 0; index < NUM_MONTHS; index++)  
    { 
     if (amount[index] > high) 
     { 
      highMonth = amount[index]; 
      months = index; 
     } 
    } 

    return highMonth; 
} 
+0

ヒント: 'main'を最初に書いてください。 'main'を使わずに他の関数をテストすることはできず、関数の結果をテストして表示する方法を理解することで、関数の書き方を理解するのに役立ちます。 [テスト駆動開発の詳細](https://en.wikipedia.org/wiki/Test-driven_development)すべての機能がどのようなものであるかを知ったら、それらを1つずつ実装してテストしてください。一度にすべてをテストすることで、チェックしなければならない表面積が増え、バグの影響が拡大し、相互に隠れてしまいます。 – user4581301

+0

あなたが今持っているところでは、 'main()' intに 'lowMonth'と' highMonth'を作り、 'month [lowMonth]'と 'month [highMonth]'を出力するのはどうでしょうか? –

答えて

0

だからあなたの質問は非常に不明確であり、あなたのコードは合計混乱ですが、私はあなたが機能getHighestも、月の名前の文字列を返すようにしたいと仮定?
まあ、一つの機能だけ変数の一つのタイプを返すことができますが、リファレンスでそれを行うことができます:

double getHighest(double amount[], int NUM_MONTHS, int &highMonth, string &name) 
{ 
    //high = amount[0]; //Variable to hold highest value 
    months = amount[0]; 
    double highMonth = 0; //Variable to hold highest value 

    for (int index = 0; index < NUM_MONTHS; index++)  
    { 
     if (amount[index] > high) 
     { 
      highMonth = amount[index]; 
      months = index; 
      name = month[index]; 
     } 
    } 
    return highMonth; 
} 

しかし、一般的にあなたのコードがかなりの欠陥があります。たとえば highMonthの上書きが問題を引き起こす可能性があり、なぜグローバルに定義されていますが、NUM_MONTHSに合格するのですか?そして、私はこのコードがあまりにも複雑すぎる理由を正確には知りません。私はまた、getHighestgetLowestの機能も機能していないと思う。

double getHighest(double amount[], string &name) 
{ 
    double highest = 0; 
    double highPos = 0; 
    for(unsigned int i = 0; i < NUM_MONTHS; i++) 
    { 
     if(highest <= amount[i]) 
     { 
      highest = amount[i]; 
      highPos = i; 
     } 
    } 
    name = month[highPos]; 
    return highest; 
} 

編集:または、あなただけの文字列を返すようにしたい場合、あなたはこのようにそれを行うことができます。

文字列&名) {より良いアプローチとして私はこのような機能を試してみました //高値=金額[0]; //最高値を保持する変数 months =量[0]; double highMonth = 0;

for (int index = 0; index < NUM_MONTHS; index++)  
    { 
     if (amount[index] > high) 
     { 
      highMonth = amount[index]; 
      months = index; 
      name = month[index]; 
     } 
    } 
    return highMonth; 
} 

しかし、一般的に、あなたのコードの最高値を保持する//変数はかなりの欠陥があります。たとえば highMonthの上書きが問題を引き起こす可能性があり、そしてなぜあなたはそれがグローバルに定義されていても、NUM_MONTHSを渡すのですか?そして、私はこのコードがあまりにも複雑すぎる理由を正確には知りません。私はまた、getHighestgetLowestの機能も機能していないと思う。より良いアプローチとして、私はこのような関数を試してみよう:

string getHighest(double amount[], string &name) 
{ 
    double highest = 0; 
    double highPos = 0; 
    for(unsigned int i = 0; i < NUM_MONTHS; i++) 
    { 
     if(highest <= amount[i]) 
     { 
      highest = amount[i]; 
      highPos = i; 
     } 
    } 
    name = month[highPos]; 
    return name; 
} 
+2

コードは完全な混乱ではありません。それは機能に編成され、機能はほとんど意味をなさない。 OPのニーズは、教科書をもっと激しく打つ必要があり、構文を理解して、彼らが何をしているのか、機能の中でやる必要がないのかをもう少し考えてみる必要があります。しかし、質問は不明である。質問が理にかなっていないときは、答えないでください。 – user4581301

+0

ありがとうございます。私はそれが完全な混乱であり、どこにいてもわかる。私は読んでレビューの質問に取り組んできましたが、配列は私のためにクリックされていません。それはオンラインクラスであり、私の教授は私が宿題に取り組んでいる間は利用できません。 – beverlyras

+0

私の質問は、getHighest関数とgetLowest関数で、intではなくstring名を返す必要があります。私はちょうど方法がわからない。 – beverlyras

関連する問題