Not outputting lowest from array correctly
Oct 27, 2014 at 5:01am UTC
hey guys so when you run the program it will show you the correct max temp but minimum comes out as -9 something. help plz. i think it might have to do with the getTemp function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/********************************************************************************************
*Project: lowest and highest temperature
*File hw5.cpp
*authors: Damien Carney
*Date: 10/24/2014
*Description: use seperate functions to have a user partially fill an array with temperatures
and find highest and lowest temperature in the array and out put it to console.
*********************************************************************************************/
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
/**********************************************************************************************
*function: main
*description: call functions and output lowest and highest temperature acquired from functions.
**********************************************************************************************/
//prototype
void getTemp(double [], int size);
double getLowest(const double [], int );
double getHighest(const double [],int );
int main ()
{
double lowestTemp;
double highestTemp;
const int size = 100;
double temperature[size];
getTemp(temperature, size);
lowestTemp = getLowest(temperature, size);
highestTemp = getHighest(temperature, size);
cout << "the highest & lowest temperatures entered were: "
<< lowestTemp << " & " << highestTemp << endl;
}
/**********************************************************************************************
*function: getTemp
*description: call functions and output lowest and highest temperature acquired from functions.
**********************************************************************************************/
void getTemp(double temp[], int size)
{
int value;
for (int index=0; index <size; index++)
{
cout << "enter temperature #(-1 to exit) " << (index+1) << ": " ;
cin >> value;
if (value==-1)
break ;
temp [index]=value;
}
}
/**********************************************************************************************
*function: getLowest
*description: retreive lowest temperature that user entered
**********************************************************************************************/
double getLowest(const double array[], int size)
{
double lowest = array[0];
for (int count = 1; count < size; count++)
{
if (array[count] < lowest)
lowest = array[count];
}
return lowest;
}
/**********************************************************************************************
*function: getHighest
*description: retreive highest temperature that user entered
**********************************************************************************************/
double getHighest(const double array[],int size)
{
double highest = array[0];
for (int count = 1; count < size; count++)
{
if (array[count] > highest)
highest = array[count];
}
return highest;
}
Oct 27, 2014 at 5:16am UTC
If you will not enter all 100 values in getTemp, the rest will be filled with random values which would be checked by other functions. Use C++ containers line vector.
Or make getTemp to return actual array size which would be used later.
Oct 27, 2014 at 5:23am UTC
could you show me an example?
Oct 27, 2014 at 5:26am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
int count = 0; // to count how many numbers have been inputed to array
void getTemp(double [], int size);
int main ()
{
double lowestTemp;
double highestTemp;
const int size = 10;
double temperature[size];
getTemp(temperature, size);
highestTemp=temperature[0];
lowestTemp =temperature[0];
for (int i=0; i<count; i++)
{
if (temperature[i]>highestTemp)
highestTemp=temperature[i];
if (temperature[i]<lowestTemp)
lowestTemp =temperature[i];
}
cout << "the highest & lowest temperatures entered were: "
<< lowestTemp << " & " << highestTemp << endl;
}
void getTemp(double temp[], int size)
{
int value;
for (int index=0; index <size; index++)
{
cout << "enter temperature #(-1 to exit) " << (index+1) << ": " ;
cin >> value;
if (value==-1)
break ;
temp [index]=value;
count++;
}
}
Oct 27, 2014 at 6:04am UTC
↑ How not to do
You need to modify getTemp(), its usage and other function calls:
1 2 3 4 5 6 7 8 9 10 11 12 13
void getTemp(double temp[], int size)
{
int value;
int index=0
for ( ; index <size; index++) {
cout << "enter temperature #(-1 to exit) " << (index+1) << ": " ;
cin >> value;
if (value==-1)
break ;
temp [index]=value;
}
return index;
}
1 2 3
int actual_size = getTemp(temperature, size);
lowestTemp = getLowest(temperature, actual_size );
highestTemp = getHighest(temperature, actual_size );
Oct 27, 2014 at 6:43am UTC
@MiiNiPaa do you mean the
why?
Oct 27, 2014 at 7:16am UTC
1) You moved well defind function back to main() which is counterproductive.
2) You unnesesarily created a global state which is usually is bad thing
3) Mainly you made getTemp() function non-reentrant:
1 2 3 4 5
double temperature[size];
getTemp(temperature, size);
//Use data
getTemp(temperature, size);
//Whoops, forgot to reset count
1 2 3
double temperature[size];
double somedata[size];
//Extra work to make sure both arrays have corect size associated after input
1 2 3 4 5
double temperature[size];
double other_data[size];
std::async(get_temp, temperature, size);
std::async(get_temp, other_data, size);
//Is not going to work
Oct 27, 2014 at 7:49am UTC
so its code expandability thing.
Topic archived. No new replies allowed.