Ich habe Probleme mit meinen Hausaufgaben und mein Professor reagiert nicht. Wie bekomme ich meine Arrays den String-Namen des Monats für die Funktionen getHighest
und getLowest
zurückgeben?Parallele Arrays, wie man int in String verwandelt
Er sagt mir immer, dass ich mehr Details hinzufügen muss, entfernte ich Code von diesem, so habe ich jetzt nur die wichtigsten und getLowest
und getHighest
Funktionen übrig. Es sagt, dass ich noch mehr Details brauche, also tippe ich das, um mehr Details hinzuzufügen, damit ich meine Frage einreichen kann.
Vielen Dank im Voraus!
#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;
}
Hinweise: Schreiben Sie 'Haupt' zuerst. Sie können die anderen Funktionen nicht ohne 'main' testen und herausfinden, wie Sie die Ergebnisse der Funktionen testen und anzeigen, hilft Ihnen herauszufinden, wie Sie die Funktionen schreiben werden. [Mehr zur Testgetriebenen Entwicklung.] (Https://en.wikipedia.org/wiki/Test-driven_development) Sobald Sie wissen, wie alle Funktionen aussehen müssen, implementieren und testen Sie sie einzeln. Alles auf einmal zu testen erhöht die Oberfläche, die überprüft werden muss, und vergrößert die Wirkung von Fehlern, wenn sie aufeinander aufbauen und sich gegenseitig verbergen. – user4581301
Mit was hast du jetzt, warum machst du nicht 'lowMonth' und' highMonth' in 'main()' ints, und gibst nur 'month [lowMonth]' und 'month [highMonth]' aus? –