3 Function Code not working

Hi all.

I'm suppose to be writing a code that consists of three functions. The first function fills an array with temperatures inputted by the user. The next function determines the average of the temperatures in the array. The last function just displays the temperatures.

The only function that seems to work is the display function. :/

I believe my problem is with the array itself. When I actually do get the program to display, there is nothing but zeroes where the values should be. I've never been good with arrays, so I'm not entirely sure where I've gone wrong.

I also keep receiving a warning message stating:

'main function.cpp(45): warning C4715: 'ComputeAverageTemp' : not all control paths return a value
main function.cpp(75): warning C4700: uninitialized local variable 'AverageTemp' used

I'm not sure exactly where I should be initializing AverageTemp (or if I even should).



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
#include <iostream>
using namespace std;

//Function to fill the array with the given temperatures.
void GetTemperatures(int Temperatures[], int NumTemperatures)
{
	for(int hour=0; hour < NumTemperatures; ++hour)
	{

		cout << "Please enter temperature for hour " << hour << ": ";
		cin >> Temperatures[12];

		//Checking to make sure the temperature falls in the appropriate range.
		while(Temperatures[12] > 130 || Temperatures[12] < -50)
		{
		cout << "The temperature you've entered exceeds the 
                appropriate range (-50 to +130). Please enter an appropriate temperature: ";
		cin >> Temperatures[12];
		};

	}

	return;
}

//Compute average of the numbers in the array.
double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
	for (int hour=0; hour < NumTemperatures; hour++)
	
        int total = 0; 
        { 
			
		total += Temperatures[hour];

		return double ((total)/NumTemperatures);
	}
}

//Display the temperatures of each hour from the array. Also display the average.
void DisplayTemperatures(int Temperatures[], int NumTemperatures, double AverageTemp)
{
	cout << "Hour\tTemperature" << endl;
	for (int hour=0; hour < NumTemperatures; hour++)
	{ 
		cout << hour << "\t" << Temperatures[hour] << endl; 
	}

	cout << "Average Temperature: " << AverageTemp << endl;

	return;
}



int main()
{

	//Declarations

	int HourlyTemperatures[12] = {0};
	int NumTemperatures = 12;
	char end;
        //Attempt at initializing AverageTemp. Still results in error messages.
	double AverageTemp=0;



	do
	{
		GetTemperatures(HourlyTemperatures,  NumTemperatures);
		ComputeAverageTemp(HourlyTemperatures,  NumTemperatures);
		DisplayTemperatures(HourlyTemperatures,  NumTemperatures, AverageTemp);

		cout << "Do you want to continue? Enter Y or N.";
		cin >> end;


	} while (end != 'N' && end != 'n');



	return 0;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
	for (int hour=0; hour < NumTemperatures; hour++)
	
        int total = 0; 
        { 
			
		total += Temperatures[hour];

		return double ((total)/NumTemperatures);
	}
}

total and your return should be outside of the for loop

Should look like this
1
2
3
4
5
6
7
8
//Compute average of the numbers in the array.
double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
	int total = 0;
	for (int hour=0; hour < NumTemperatures; hour++)
		total += Temperatures[hour];
	return double ((total)/NumTemperatures);
}
I knew those brackets would get me. That definitely helps the program run without crashing/giving me an error.

However the array still doesn't seem to work. I'm not sure whether it's not filling or if it is, but I haven't passed it along to the Average function correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
         for(int hour=0; hour < NumTemperatures; ++hour)
	{

		cout << "Please enter temperature for hour " << hour << ": ";
		cin >> Temperatures[12];

		//Checking to make sure the temperature falls in the appropriate range.
		while(Temperatures[12] > 130 || Temperatures[12] < -50)
		{
		cout << "The temperature you've entered exceeds the 
                appropriate range (-50 to +130). Please enter an appropriate temperature: ";
		cin >> Temperatures[12];
		};

	}


Check the number in your array subscripts carefully.
I completely missed that. That's why I'm bad at arrays.

I've swapped that 12 out for the variable hour and now the numbers are showing up as they should.

The only thing still not functioning is the Average function. I've messed with it and it seems that the program is only returning the AverageTemp variable I initialized in the main function (I changed the initial value from 0 to 22 and sure enough that was what was returned when I ran the program).

I tried moving it to the Average function, but that results in the AverageTemp variable in Main to become unidentified.

1
2
3
4
5
6
7
8
9
10
11
//Compute average of the numbers in the array.
double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
	int total = 0;
	for (int hour=0; hour < NumTemperatures; hour++)
		total += Temperatures[hour];
	// Try - cout << "Your average is " << (total/NumTemperatures) << endl;
	// You can also remove the data type "double" from the return line
	// You don't need to define your return data type, the function does that for you
	return (total/NumTemperatures);
}


That is because you return the value, but you don't assign that value to any variables.

1
2
3
// Returns the average, but is unused.
ComputeAverageTemp(HourlyTemperatures,  NumTemperatures);
// AverageTemp = ComputeAverageTemp(HourlyTemperatures, NumTemperatures); // This would work 
Last edited on
Sorry for taking so long to respond. That worked like a charm.

I went with the second option, since displaying of the info is suppose to take place with the display function.

Thank a ton for all the help. Much appreciated!
Topic archived. No new replies allowed.