Help with Temperature Program with Arrays

Hello everyone, I'm having a difficult time with a program for class and cannot figure out what is wrong with the code. Any help would be appreciated!

Here is the code for the program:

Main CPP File:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Lab4.h"
using namespace std;

int main()
	{
		const int NumTemperatures = 24;
		int HourlyTemperatures[NumTemperatures];
		int LowTemp = 0;
		int HighTemp = 0;
		double AverageTemp = 0.0;
	
		GetTemperatures(HourlyTemperatures, NumTemperatures);
		AverageTemp = ComputeAverageTemp(HourlyTemperatures, NumTemperatures);
		DisplayTemperatures(HourlyTemperatures, NumTemperatures, AverageTemp);
	
		return 0;
	}

void GetTemperatures(int Temperatures[], int NumTemperatures)
	{
	int CurrentTemp = 0;
	for (int counter = 0; counter < NumTemperatures; counter++)
		{
		cout << "Please enter the temperature for " << counter << ":00? ";
		while (true)
			{
				cin >> CurrentTemp;
			
				if (CurrentTemp > -49 && CurrentTemp < 131) 
					{
					break; 
				
					}
						else 
						{
							cout << "That temperature is not within range\n";
							cout << "Please enter a temperature between -50 and 130 degrees...\n";
							cout << "Enter new temperature: ";
				
						}
	}
		
				        
				        Temperatures[counter] = CurrentTemp;
			    }
		}

	    double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
	    {
		        double AverageTemp = 0.0;
		        int Count = 0;
		        int Sum = 0;
	
			        for (Count = 0; Count != NumTemperatures; Count++)
			        {
			            Sum = Sum + Temperatures[Count];
			        }
	
			        AverageTemp = Sum / NumTemperatures;
	
			        return AverageTemp;
		    }

void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp)
	    {
		        int Count = 0;
	
			        cout << "\n";
		        cout << "\n";
		        cout << setw(5) << "Hour" << setw(32) << "Temperature \n";
	
			        while (Count != NumTemperatures)
			        {
			            cout << setw(3) << Count++ << ":00" << setw(24) << Temperatures[Count] << endl;
				    }
					void pause();
{
				cout << "Hit any key to continue ";
				cin.ignore(2);
}
	
			cout << "\n";
		    cout << " High Temperature: " << setw(13) << "\n";
	        cout << " Low Temperature: " << setw(14) << "\n";
		    cout << " Average Temperature: " << setw(8) << AverageTemp << endl;
		    cout << "\n";
}


Here is the header file:

1
2
3
4
5
6
7
8
#ifndef Lab4_h
#define Lab4_h

void GetTemperatures(int Temperatures[], int NumTemperatures);
double ComputeAverageTemp(int Temperatures[], int NumTemperatures);
void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp);

#endif 


The program: a simple program to calculate temperatures for each hour of the day. It will be outputted in the format coded and will also calculate highest, lowest, and average temperatures (I have not focused on the high and low yet). I keep getting output for hour 23:00 as -8..... which I am assuming is an error with the array size? Tried different settings for it however and cannot get it to work.

I understand no one wants to do anyone's homework, but any resources or tips would be greatly appreciated.
Line 77 - think about the value of Count in Temperatures[Count]
Thank you for the help, I changed the Count++ on the output and now it properly displays all the temperatures.

While I did not think I would have an issue with the highest/lowest temp, it is only displaying 0 for highest:

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Lab4.h"
using namespace std;

int main()
{
	const int NumTemperatures = 24;
	int HourlyTemperatures[NumTemperatures];
	int Low = 0;
	int highValue = 0;
	double AverageTemp = 0.0;

	GetTemperatures(HourlyTemperatures, NumTemperatures);
	AverageTemp = ComputeAverageTemp(HourlyTemperatures, NumTemperatures);
	DisplayTemperatures(HourlyTemperatures, NumTemperatures, AverageTemp, highValue);
	highValue = High(HourlyTemperatures, NumTemperatures);

	return 0;
}

void GetTemperatures(int Temperatures[], int NumTemperatures)
{
	int CurrentTemp = 0;
	for (int counter = 0; counter < NumTemperatures; counter++)
	{
		cout << "Please enter the temperature for " << counter << ":00? ";
		while (true)
		{
			cin >> CurrentTemp;

			if (CurrentTemp > -49 && CurrentTemp < 131)
			{
				break;

			}
			else
			{
				cout << "That temperature is not within range\n";
				cout << "Please enter a temperature between -50 and 130 degrees...\n";
				cout << "Enter new temperature: ";

			}
		}


		Temperatures[counter] = CurrentTemp;
	}
}

double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
	double AverageTemp = 0.0;
	int Count = 0;
	int Sum = 0;

	for (Count = 0; Count != NumTemperatures; Count++)
	{
		Sum = Sum + Temperatures[Count];
	}

	AverageTemp = Sum / NumTemperatures;

	return AverageTemp;
}

int High(int Temperatures[], int NumTemperatures)
{
	
	int highValue = Temperatures[0];
	for (int Count = 0; Count < NumTemperatures; Count++)
	{
		if (Temperatures[Count] > highValue)
		{
			highValue = Temperatures[Count];
		}
	}
	return highValue;

}

void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp, int highValue)
{
	int Count = 0;

	cout << "\n";
	cout << "\n";
	cout << setw(5) << "Hour" << setw(32) << "Temperature \n";

	while (Count < 24)
	{
		cout << setw(3) << Count << ":00" << setw(24) << Temperatures[Count] << endl;
		Count++;
	}
	void pause();
	{
		cout << "Hit any key to continue ";
		cin.ignore(2);
	}

	cout << "\n";
	cout << " High Temperature: " << setw(13) << highValue << "\n";
	cout << " Low Temperature: " << setw(14) << "\n";
	cout << " Average Temperature: " << setw(8) << AverageTemp << endl;
	cout << "\n";
}


Header:
1
2
3
4
5
6
7
8
9
10
#ifndef Lab4_h
#define Lab4_h

void GetTemperatures(int Temperatures[], int NumTemperatures);
double ComputeAverageTemp(int Temperatures[], int NumTemperatures);
void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp, int High);
int High(int Temperatures[], int NumTemperatures);


#endif 


After looking at several examples, I'm not sure why it is not returning highValue, and the Temperatures[0] is there to tell the program to start from the beginning of the array correct?
You're welcome.

Look at the order that you are calling your functions in main().
Thank you! I've been working on this program for so long and it all has been very basic mistakes lol. Now everything works as it should.

Here is my final code if anyone else can benefit from it on these forums (seeing the simple mistakes I was making from the original and the fixed code):

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include <iomanip>
#include <cmath>
#include "Lab4.h"
using namespace std;

int main()
{
	const int NumTemperatures = 24;
	int HourlyTemperatures[NumTemperatures];
	int highValue = 0;
	int lowValue = 0;
	double AverageTemp = 0.0;

	GetTemperatures(HourlyTemperatures, NumTemperatures);
	AverageTemp = ComputeAverageTemp(HourlyTemperatures, NumTemperatures);
	highValue = High(HourlyTemperatures, NumTemperatures);
	lowValue = Low(HourlyTemperatures, NumTemperatures);
	DisplayTemperatures(HourlyTemperatures, NumTemperatures, AverageTemp, highValue, lowValue);


	return 0;
}

void GetTemperatures(int Temperatures[], int NumTemperatures)
{
	int CurrentTemp = 0;
	for (int counter = 0; counter < NumTemperatures; counter++)
	{
		cout << "Please enter the temperature for " << counter << ":00? ";
		while (true)
		{
			cin >> CurrentTemp;

			if (CurrentTemp > -49 && CurrentTemp < 131)
			{
				break;

			}
			else
			{
				cout << "That temperature is not within range\n";
				cout << "Please enter a temperature between -50 and 130 degrees...\n";
				cout << "Enter new temperature: ";

			}
		}


		Temperatures[counter] = CurrentTemp;
	}
}

double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
	double AverageTemp = 0.0;
	int Count = 0;
	int Sum = 0;

	for (Count = 0; Count != NumTemperatures; Count++)
	{
		Sum = Sum + Temperatures[Count];
	}

	AverageTemp = Sum / NumTemperatures;

	return AverageTemp;
}

int High(int Temperatures[], int NumTemperatures)
{
	int highValue;
	highValue = Temperatures[0];
	for (int Count = 1; Count < NumTemperatures; Count++)
	{
		if (Temperatures[Count] > highValue)
		
			highValue = Temperatures[Count];
		
	}
	return highValue;

}

int Low(int Temperatures[], int NumTemperatures)
{
	int lowValue;
	lowValue = Temperatures[0];
	for (int Count = 1; Count < NumTemperatures; Count++)
	{
		if (Temperatures[Count] < lowValue)
			lowValue = Temperatures[Count];
	}
	return lowValue;
}

void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp, int highValue, int lowValue)
{
	int Count = 0;

	cout << "\n";
	cout << "\n";
	cout << setw(5) << "Hour" << setw(32) << "Temperature \n";

	while (Count < 24)
	{
		cout << setw(3) << Count << ":00" << setw(24) << Temperatures[Count] << endl;
		Count++;
	}
	void pause();
	{
		cout << "Hit any key to continue ";
		cin.ignore(2);
	}

	cout << "\n";
	cout << " High Temperature: " << setw(13) << highValue << "\n";
	cout << " Low Temperature: " << setw(14) << lowValue << "\n";
	cout << " Average Temperature: " << setw(8) << AverageTemp << endl;
	cout << "\n";
}

Topic archived. No new replies allowed.