I am developing an Object-oriented program using an UML diagram. The assignment asks to use an array of objects with size 7, and to input data for "weekDay", "highTemp", "lowTemp", and "Rainfall". I already have everything set up correctly; however, the function indexOfLowestATemp is reporting the wrong index even though the indexOfHighestTemp reports the correct index?
int main()
{
WeatherInfo weather[7];
// Get data from user for each of the 7 days
int counter = userInput(weather, 7);
// Retrieve highest temperature found for a typical week
int highestIndex = indexOfHightestTemp(weather, 7);
// Retrieve lowest temperature found for a typical week
int lowestIndex = indexOfLowestTemp(weather, 7);
// Retrieve the total rainfall for a typical week
double total = totalRainFall(weather, 7);
// Calculate average temperature for high temps
double avgHigh = averageHigh(weather, 7);
// Calculate average temperature for low temps
double avgLow = averageLow(weather, 7);
// Display weather information
show(weather, 7);
cout << "Averages " << right << setw(10) << avgHigh << setw(10) << avgLow << setw(10) << (total / 7);
cout << "\n\nHighest reading: " << weather[highestIndex].getHighTemp() << " [" << weather[highestIndex].getWeekDay() << "]\n";
cout << "Lowest reading: " << weather[lowestIndex].getLowTemp() << " [" << weather[lowestIndex].getWeekDay() << "]\n";
cout << "Total rainfall: " << total << "\"\n";
}
int indexOfHightestTemp(WeatherInfo arr[], int size)
{
int highest = arr[0].getHighTemp();
int highestIndex;
for (int index = 1; index < size; ++index)
{
if (arr[index].getHighTemp() > highest)
{
highestIndex = index;
}
}
return highestIndex;
}
int indexOfLowestTemp(WeatherInfo arr[], int size)
{
int lowest = arr[0].getLowTemp();
int lowestIndex = 0;
for (int index = 1; index < size; ++index)
{
if (arr[index].getLowTemp() < lowest)
{
lowestIndex = index;
}
}
return lowestIndex;
}
Would you mind adding any other relevant code too? I can see you using "userInput()", but I’m not entirely sure how you working that part out exactly. And also your Weatherinfo class too?
is not quite true. Both functions return nonsense for same reason.
Unfortunately your test data does not reveal that the output of indexOfHighestTemp is wrong. It might return the number that you expect, but not for the right reason.
Overall, you do not need the "highest" variable. Use the index.