indexOfLowestTemp identifier not returning true index of lowest temp found.

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?

Thank you for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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?
Can we see the implementation of your Weatherinfo class, please.

BTW, your function indexOfHighestTemp could demonstrate undefined behaviour, since you don't actually initialise highestIndex.
is logic bug.
you check current vs lowest, and update index, but you NEVER UPDATE LOWEST.
so if you have
100
90
80
95

it will return 95 because 95 is lower than 100, the original lowest....
Last edited on
Well spotted, @jonnin!

Same is actually true of highest. So, that's two mistakes in the function said to be right.
In other words:
the indexOfHighestTemp reports the correct index

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.
Topic archived. No new replies allowed.