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;
		}

Header:

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

.CPP File:

void DisplayTemperatures(int Temperatures[], int NumTemperatures, int AverageTemp, int High, int Low)

The header and cpp file definitions don't match although I'm not sure this is your issue.

I'd also recommend putting all includes into the header file rather then the .cpp (that is, all but the header include itself. :)). Find out what the error is before moving these though, just in case they throw an error themselves.

>>I'd also recommend putting all includes into the header file rather then the .cpp

NO absolutely not, if we are talking about best practices in coding. OP: The way you did it is fine. You include as few headers as you can in your headers and include the headers you need for the implementation in the CPP.

And yes, the error is because of the mismatch of the parameters of the function, and I'm sure of that. And your call to it is also mismatched:

DisplayTemperatures(Temperatures, NumTemperatures, AverageTemp);

You have more problems than that though. Your function ComputeAverageTemp takes a useless parameter (AverageTemp) and when you call it, you need to store the return value in you variable AverageTemp. The same goes for Low and High.

GetTemperatures(Temperatures, NumTemperatures); 
			  Sleep(1500);
			  AverageTemp = ComputeAverageTemp(Temperatures, NumTemperatures); //take the last parameter out of the ComputeAverageTemp function
			  Sleep(1500);
			  int lowTemp = Low (Temperatures, NumTemperatures);
			  Sleep(1500);
			  int highTemp = High(Temperatures, NumTemperatures);
			  Sleep(1500);
			  DisplayTemperatures(Temperatures, NumTemperatures, AverageTemp, lowTemp, highTemp); //update your header prototype for DisplayTemperatures
			  Sleep(3500);

Thanks so much kirennian and mike_2000_17 for the feedback and help. I've put to use both of your suggestions and my program compiles, links, and runs; that's fabulous progress. But the output is off, so I'm guessing I'm looking at a logical error that I need to track down and edit.

Let me post the output along with the function .cpp (since that's where the meat of the program takes place) and if you are so inclined, I'd welcome even more knowledge and wisdom.

OUTPUT


Press 'A' continue - Press 0 to quit.a
Input hourly temp: 55
Input hourly temp: 65
Input hourly temp: 76
Input hourly temp: 88
Input hourly temp: 99
Hour Temperature
-858993460 5
Average 0
High 99
Low 55
Okay - Another Time Press any key to continue . . .

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


With a little bit of troubleshooting, I've determined that my output is erroring out here (see edited cpp code) and I've already adjusted the High and Low output to display correctly.

EDITED FUNCTION CPP FILE

void DisplayTemperatures(int Temperatures[], int NumTemperatures, int AverageTemp, int lowTemp, int highTemp)
{
	cout << fixed << setprecision (2);
	cout << setw(5) << "Hour" << setw(9) << " Temperature\n";
	cout << setw(11) << Temperatures [NumTemperatures] <<setw(12) << NumTemperatures << endl;
	Sleep(1500);

NO absolutely not, if we are talking about best practices in coding. OP: The way you did it is fine. You include as few headers as you can in your headers and include the headers you need for the implementation in the CPP.

So would I be right in saying that you only include headers which are required for the classes actual header itself and that those which are required for the .cpp methods, you include at the top of the .cpp file? That makes logical sense I suppose.

// 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;
}

I presume the above is for output of all input temperatures. If this is the case, you'd need to loop through each case in this method but I don't see one... or am I missing something?

Assuming NumTemperatures is greater than 9.

void DisplayTemperatures(int Temperatures[], int NumTemperatures, int AverageTemp, int lowTemp, int highTemp)
{
	
	cout << fixed << setprecision (2);
	cout << "Hour" << setw(9) << " Temperature\n";
	for (int x=0;x<10;++x)
	{
		cout<< setw(2) << "0" << x << ":00" << setw(7) << Temperatures[x] << endl;
	}
	for (x=10;x<NumTemperatures;++x)
	{
		cout<< setw(3) << x << ":00" << setw(7) << Temperatures[x] << endl;
	}
	Sleep(1500);
	cout << setw(8) << "High" << setw(9) << highTemp << endl;
	cout << setw(8) << "Low" << setw(9) << lowTemp << endl;
	cout << setw(8) << "Average" << setw(9) << AverageTemp << endl;
	return;
}

Thank you so much mtbs1826 for your help. With the loop and a little logical working on the average function, this program works exactly as requested.

But more than that, I've learned that when I go too fast I can get ahead of myself and these sorts of errors pop up. I need to make sure that all the little details are taken care of in each part of the program before moving on, else none of it will work.

Thanks again for all of your wisdom and knowledge.

>>But more than that, I've learned that when I go too fast I can get ahead of myself and these sorts of errors pop up.

Great! As additional comment, I would say that the best way to go about a problem like this is to program it incrementally. In your case, it was simple and you were able to get all the functions programmed before compiling once, but then you get some errors that pile up and it becomes harder to find and debug them. Even, on simple problems, it's a good idea to compile many times, at every new function you add, this way you know exactly what causes the error in compilation or in the output of the newly added function. Of course, as you gain experience you can write more code or even complete libraries without compiling once, but let that come by itself, no rush.

@kirennian: >> So would I be right in saying that you only include headers which are required for the classes actual header itself and that those which are required for the .cpp methods, you include at the top of the .cpp file? That makes logical sense I suppose.

Absolutely, the point is that your header is intended to be included by other parts of the software (other source files or headers). So the more things you include in your header, you possibly will increase the size of the code that includes it, requiring more compilation time, and possibly additional linking requirements (adding more static libraries to the linking process that might not really be needed for the other parts of the software, but the compiler will need them to complete the code and solve the references).

commented: Thanks :) +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.