I need to figure out how to get the min/max in this array. I heard that it can be done using the algorithm library. we have yet to learn that. any help would be appreciated.

``````#ifndef Lab4_h
#define Lab4_h

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

#endif``````
``````#include <iostream>
#include <algorithm>
using namespace std;

#include "nlab4.h"

int main ()
{
const int NumTemperatures = 23;
int HourlyTemperatures[NumTemperatures] = {0};
double AverageTemp = 0.0;
int min=0, max=0;

GetTemperatures (HourlyTemperatures, NumTemperatures);
AverageTemp = ComputeAverageTemp (HourlyTemperatures, NumTemperatures);
DisplayTemperatures (HourlyTemperatures, NumTemperatures, AverageTemp);

return 0;
}

void GetTemperatures (int Temperatures[], int NumTemperatures)
{
int Count = 0;
int CurrentTemp = 0;
int TimerCount = 0;

while (Count < 24)
{
if (CurrentTemp > -50 && CurrentTemp < 131)
{
cout<<"Temperature at "<<TimerCount++<<":00? ";
cin>>CurrentTemp;

Temperatures[Count++] = CurrentTemp;
}

else
{
cout<< "Incorrect temperature input \n";
cout<<"Please input temperature between -50 an 130 degrees \n";
cout<<"Re-input temperature: \n";
cin>>CurrentTemp;
}
}
}

double ComputeAverageTemp (int Temperatures[], int NumTemperatures)
{
double AverageTemp = 0.0;
int Count = 0;
int Sum = 0;

for (Count = 0; Count < 24; Count++)
{
Sum = Sum + Temperatures[Count];
}

AverageTemp = Sum / NumTemperatures;

return AverageTemp;
}

void DisplayTemperatures (int Temperatures[], int NumTemperatures, double AverageTemp)
{

int Count = 0;

cout << "*******************************\n";
cout << "Hour\t\tTemperature\n";

while (Count < 24)
{
cout << "0" << Count << ":00\t\t" << Temperatures[Count++] << endl;
}

cout << "*******************************\n";
cout << "High Temp: \t\t\n";
cout << "Low Temp: \t\t\n";
cout << "Average Temp:\t" << AverageTemp << endl;
cout << "*******************************\n";

}``````

You do not need to use the stl library to calculate the min / max . Try writing the code on your own .. Let me give you a head start

For Min.

1. Create a temp variable and give it a value of 30,000 [or any such large value].
2. Iterate through your array. At each point compare the value in the temp variable in the array.
3. If the value in the temp variable is less than the value in the array then do nothing. Else store the value in the array in the temp variable.

I hope you get the idea

You do not need to use the stl library to calculate the min / max . Try writing the code on your own .. Let me give you a head start

For Min.

1. Create a temp variable and give it a value of 30,000 [or any such large value].

Set the temp variable to the first element of the array.

I heard that it can be done using the algorithm library.

I agree that unless you are supposed to use that you should write it yourself.

Be a part of the DaniWeb community

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