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).
#include <iostream>
usingnamespace 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];
returndouble ((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;
}
double ComputeAverageTemp(int Temperatures[], int NumTemperatures)
{
for (int hour=0; hour < NumTemperatures; hour++)
int total = 0;
{
total += Temperatures[hour];
returndouble ((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];
returndouble ((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.
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.
//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