私は宿題に問題があり、教授は応答していません。 getHighest
とgetLowest
関数の月の文字列名を返すように配列を取得するにはどうすればよいですか?intを文字列に変換する方法並列配列
私は詳細を追加する必要があることを伝え続けます。このコードを削除したので、現在はメインとgetLowest
とgetHighest
の機能が残ります。それでも私はまだ詳細が必要だと言いますので、私は質問を提出できるように詳細を追加するためにこれをタイプしています。
ありがとうございます!
#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;
}
ヒント: 'main'を最初に書いてください。 'main'を使わずに他の関数をテストすることはできず、関数の結果をテストして表示する方法を理解することで、関数の書き方を理解するのに役立ちます。 [テスト駆動開発の詳細](https://en.wikipedia.org/wiki/Test-driven_development)すべての機能がどのようなものであるかを知ったら、それらを1つずつ実装してテストしてください。一度にすべてをテストすることで、チェックしなければならない表面積が増え、バグの影響が拡大し、相互に隠れてしまいます。 – user4581301
あなたが今持っているところでは、 'main()' intに 'lowMonth'と' highMonth'を作り、 'month [lowMonth]'と 'month [highMonth]'を出力するのはどうでしょうか? –