Hello
I've just joined this forum, although I have used it regularly in the last couple of months to help in my learning of programming. I have learned a lot (I feel), and I'm now hoping to not only resolve the errors I have in my coding, but to gain some insight on best practices and thinking so that I can become a better programmer. With that, would anyone object to taking a look at this and letting me know where and how I can improve?
Thanks so much.
mommabear
Now, the assignment as written states:
DDI&T a C++ program to input, store, and process hourly temperatures for each hour of the day (i.e., 24 temperatures). Your program should be divided logically into the following parts (note: this does not mean that each part needs to necessarily be a separate function):
1. In function main() declare an array, int HourlyTemperatures[NumTemperatures] (where NumTemperatures should be declared as a constant with the value, 24), that will be used to hold one temperature for each hour of the day. Declare other variables and constants as you feel are necessary.
2. Pass the HourlyTemperatures array to a function,
void GetTemperatures(int Temperatures[], int NumTemperatures);
This function must interactively prompt for and input temperatures for each of the 24 hours in a day (0 through 23). For each temperature that is input, verify that its value lies in the range of minus 50 degrees and plus130 degrees (i.e., validate your input values). If any value is outside the acceptable range, ask the user to re-enter the value until it is within this range before going on to the next temperature (HINT: use a loop which only exits when a value in the specified range has been entered). Store each validated temperature in its corresponding element of the 24-element integer array passed as a parameter to the function.
3. Next pass the filled array to a function,
double ComputeAverageTemp(int Temperatures[ ] , int NumTemperatures);
This function computes the average temperature of all the temperatures in the array and returns this average to the calling function.
4. Finally, pass your array and the computed average temperature to another function,
void DisplayTemperatures(int Temperatures[], int NumTemperatures,
double AverageTemp);
which displays the values of your temperature array in a columnar format followed by the values for the high temperature, low temperature, and average temperature for the day. NOTE: If you want to create separate function(s) to find and return the high and low temperature values, then feel free to do so!
The resulting output should look like this:
Hour Temperature
00:00 42
01:00 42
. . . . . . . // Your program must include all of these too!
22:00 50
23:00 48
High Temperature: 68
Low Temperature: 42
Average Temperature: 57.4
5. Since you have created functions to perform each of the functional steps of your solution, your main() function should be quite simple. The pseudo code for main() might look something like this:
int main()
{
// declare local constant(s), variable(s), and array(s).
do
{
// call GetTemperatures(Temperatures[], NumTemperatures)
// call ComputeAverageTemp(Temperatures[ ], NumTemperatures)
// call DisplayTemperatures(Temperatures[], NumTemperatures,
AverageTemp)
} while // user wants to process more days of temperatures
return 0;
}
6. Test your program with at least two (2) different sets of temperatures. Make sure you enter values to adequately test your temperature-validation code (e.g., temperatures below –50 and above +130 degrees).
In producing the output shown above, you may find that it won’t all fit on the screen at the same time (i.e., the top-most items will scroll out of view). One possible solution to this is to strategically place “pause(s)” in your output so that you can capture what is already displayed on your screen before continuing. A very simple function to pause your output is shown below. Place a call to this function anywhere you want your output to stop so you capture the output thus far before continuing.
void pause()
{
cout << “Hit any key to continue “;
cin.ignore(1); // In your particular implementation the parameter value may have
// to be changed to 2 for the ‘ignore(…)’ function to work properly
}
The errors that I am getting at the Debug phase are:
1>Linking...
1>LAB4.obj : error LNK2019: unresolved external symbol "void __cdecl DisplayTemperatures(int * const,int,int)" (?DisplayTemperatures@@YAXQAHHH@Z) referenced in function _main
1>C:\Users\Lisa\Documents\Lisa\School\CS115\LAB4\Debug\LAB4.exe : fatal error LNK1120: 1 unresolved externals
And the actual code (broken up into 3 files as requested) are:
FREE_FUNCTION.H
// PROJECT FILES
// LIST ALL PROGRAM AND HEADER FILES IN THE PROJECT
// LAB4.CPP
// IOSTREAM.h
// FREE_FUNCTION.CPP
//=============================================================================
// PROCESS THIS FILE ONLY PER PROJECT
#ifndef FREE_FUNCTION_H // Avoid duplicate compilations
#define FREE_FUNCTION_H //
//=============================================================================
// INCLUDE FILES
//
//=============================================================================
// CONSTANT DEFINITION
//=============================================================================
// EXTERNAL CLASS VARIABLE
// NONE
//=============================================================================
// FUNCTION PROTOTYPES
void GetTemperatures(int Temperatures[], int NumTemperatures);
int ComputeAverageTemp(int Temperatures[] , int NumTemperatures, int AverageTemp);
void DisplayTemperatures(int Temperatures[], int NumTemperatures, int AverageTemp);
int Low(int Temperatures [], int NumTemperatures);
int High(int Temperatures[], int NumTemperatures);
//=============================================================================
// Class Object
//============================================================================
//=============================================================================
// END OF CONDITIONAL BLOCK
#endif
//=============================================================================
// END OF HEADER FILE
//=============================================================================
FREE_FUNCTION.CPP
// Program Description: This is the function code for gathering, finding low, high, and mean, and then displaying.
//
//
// INPUTS: twenty-four temps.
//
//
// OUTPUTS: Will ask for and should return the temp for each of the 24 hours as well as the high, low, and mean.
//
//=============================================================================
// INCLUDE FILES
#include <iostream> // for cin,cout
#include <iomanip> // for formatting cout
#include <windows.h>
using namespace std;
#include "free_function.h" // for linking to main cpp file
//=============================================================================
// CONSTANT DEFINITIONS
//
//=============================================================================
// EXTERNAL CLASS VARIABLES
//
//=============================================================================
//*****************************************************************************
// BEGINNING OF PROGRAMMING CODE
//*****************************************************************************
// GetTemperatures asks user to input values which are tested again min and max perameters and then
// stores the values into the Temperatures array in main
void GetTemperatures (int Temperatures[], int NumTemperatures)
{
int CurrentTemp = 0;
for ( int i=0; i < NumTemperatures; i++)
{
cout << "Input hourly temp: ";
cin >> Temperatures [i];
while (( Temperatures[i] >130)||(Temperatures[i]< -50))
{
cout << "Out of range - Try Again";
--i;
}
}
}
// takes the values in the array and returns high, low, and mean
int ComputeAverageTemp(int Temperatures [], int NumTemperatures, int AverageTemp)
{
//average the values in the array
int sum=0;
for(int i=0;i < NumTemperatures;i++)
{
sum += Temperatures[i];
}
AverageTemp = sum / NumTemperatures;
return AverageTemp;
}
int High(int Temperatures [], int NumTemperatures)
{
//find the highest value in the array
int highValue = Temperatures[0];
for(int i=0;i<NumTemperatures;i++)
{
if(Temperatures[i] > highValue)
{
highValue = Temperatures[i];
}
}
return highValue;
}
int Low(int Temperatures [], int NumTemperatures)
{
//find the lowest value in the array
int lowValue = Temperatures[0];
for (int i = 0; i < NumTemperatures; i++)
{
if (Temperatures[i] < lowValue)
{
lowValue = Temperatures[i];
}
}
return lowValue;
}
// displays the full run of values, the high, low, and mean
void DisplayTemperatures(int Temperatures[], int NumTemperatures, int AverageTemp, int High, int Low)
{
cout << fixed << setprecision (2);
cout << setw(5) << "Hour" << setw(9) << " Temperature\n";
cout << setw(11) << Temperatures [NumTemperatures] <<setw(12) << NumTemperatures << endl;
Sleep(1500);
cout << setw(8) << "Average" << setw(9) << AverageTemp << endl;
cout << setw(8) << "High" << setw(9) << High << endl;
cout << setw(8) << "Low" << setw(9) << Low << endl;
return;
}
MAIN_LAB.CPP
// Program Description : RECORD HOURLY TEMPS AND CALCULATE STATS
//
//
// INPUTS: ASKS USER FOR TEMPS
//
//
// OUTPUTS: GIVES THE HOURLY TEMPS, HIGHS, LOWS, AND MEAN
//
//=============================================================================
// INCLUDE FILES
#include <iostream> // for cin,cout
#include <limits> // Required for numeric_limits
#include <windows.h>
#include "free_function.h"
using namespace std;
//=============================================================================
// CONSTANT DEFINITIONS
//
//=============================================================================
// EXTERNAL CLASS VARIABLES
//
//=============================================================================
//*****************************************************************************
// BEGINNING OF PROGRAMMING CODE
//****************************************************************************
int main()
{
// declare local constant(s), variable(s), and array(s)
const int NumTemperatures = 5;
int Temperatures[NumTemperatures];
int AverageTemp=0;
char run;
cout << endl;
cout << "Press 'A' continue - Press 0 to quit.";
cin >> run;
do
{
GetTemperatures(Temperatures, NumTemperatures);
Sleep(1500);
ComputeAverageTemp(Temperatures, NumTemperatures, AverageTemp);
Sleep(1500);
Low (Temperatures, NumTemperatures);
Sleep(1500);
High(Temperatures, NumTemperatures);
Sleep(1500);
DisplayTemperatures(Temperatures, NumTemperatures, AverageTemp);
Sleep(3500);
} while (run == 0);
cout << " Okay - Another Time ";
return 0;
}