
Patrick B. answered 06/29/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX_NUM_TEMPS (30)
typedef struct _TTempDataRec
{
int tempArray[MAX_NUM_TEMPS];
int minTemp;
int maxTemp;
long tempSum;
double avgTemp;
int numTemps;
} * TTempDataRec;
#define TEMP_DATA_REC_SIZE (sizeof(struct _TTempDataRec))
/**************************************************
Prompts and reads as input up to MAX_NUM_TEMPS temperature values, stores them in the array,
keeps track of the min, max, sum, and running total of the input temperatures.
populates the temp data rec structure with these statistics;
input concludes after MAX_NUM_TEMPS temeperatures have been input or the user
inputs -1, whichever happens first
then calculates the average of the temperatures
//ISSUE: What if the average temperature just so happens to be -1 ??? should be prompting YES/NO instead..
Negative temperatures are VALID!
**********************************************************/
int InputTemperatures(TTempDataRec tempDataRec)
{
//initializations
int curTemp = 1;
int iLoop=0;
memset(tempDataRec,0,TEMP_DATA_REC_SIZE);
tempDataRec->maxTemp = INT_MIN; //uses MAXINT and MININT in limits.h....
tempDataRec->minTemp = INT_MAX; // which are defined as 32768 and -32767
for (iLoop=0; iLoop<MAX_NUM_TEMPS; iLoop++)
{
//prompts for and then inputs the temperature
cout << " Temperature # " << (iLoop+1) << " : Please input the temperature or -1 to quit :>";
cin >> curTemp;
if (curTemp!=-1) // -1 was NOT input
{
tempDataRec->tempArray[iLoop] = curTemp; //stores in array
if (curTemp<tempDataRec->minTemp) { tempDataRec->minTemp = curTemp; } //updates min temp if less
if (curTemp>tempDataRec->maxTemp) { tempDataRec->maxTemp = curTemp; } //updates max temp if greater
tempDataRec-> tempSum += curTemp; //updates the running total
}
else
{
break; //user input -1, bails out
}
} //for loop
//# of temperatures read and the average of the temperatures
int iReturn = tempDataRec->numTemps = iLoop;
if (iReturn>0)
{
tempDataRec->avgTemp = tempDataRec->tempSum*1.0f / iReturn;
}
return(iReturn);
}
void OutputTemperatures(TTempDataRec tempDataRec, char * strMsg=NULL)
{
if (tempDataRec->numTemps>0)
{
if (strMsg!=NULL)
{
cout << "************************************************************" << endl;
cout << strMsg << endl;
}
cout << "************************************************************" << endl;
cout << "the " << tempDataRec->numTemps << " temperatures are : " << endl;
for (int iLoop=0; iLoop<tempDataRec->numTemps; iLoop++) { cout << tempDataRec->tempArray[iLoop] << " "; }
cout << endl << "----------------------------------------------------------------" << endl;
cout << " the total is " << tempDataRec->tempSum << endl;
cout << " the average is " << tempDataRec->avgTemp << endl;
cout << " the min temp is " << tempDataRec->minTemp << endl;
cout << " the max temp is " << tempDataRec->maxTemp << endl;
}
}
main()
{
struct _TTempDataRec tempDataRec;
InputTemperatures(&tempDataRec);
OutputTemperatures(&tempDataRec,(char*)"Temperature Statistics");
}