Hello
I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors?? (using Dev-C++ compiler)
Please see task and attempt below:
TASK : 1.8.1 Exercise 1A - Temperature tracker
Write a program that tracks temperatures during an experiment that takes 24 hours. The user should input the temperature. This temperature could be negative. The user should then input the time (in seconds, from the beginning of the experiment) that the temperature was recorded. In other words, if a temperature of 40 degrees was measured half an hour after the experiment was started, the user should input 40 and 1800 (which is 30*60). Remember to check that the number of seconds the user enters is within the allowable range, i.e. within 24 hours from the start of the experiment).
The temperature and time should be stored in two separate vectors. The program should allow users to keep inserting temperatures and times until a temperature of -999 is entered.
The program should then find the maximum temperature, and the times that the maximum temperature occurred (note that there may be more than one time at which the maximum occurred). The time should be in the format hh:mm:ss - in other words, you will have to convert the seconds to hours, minutes, and seconds. The program should also find the average temperature for the duration of the experiment. The figure below shows an example of the output:
"Maximum temperature was 23 degrees at time/s:
2:5:30
22:18:50
Average temperature: 24 degrees."
ATTEMPT:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <ios>
#include <string>
#include <vector>
using std::cin; using std::sort;
using std::cout; using std::endl;
using std::string; using std::streamsize;
using std::vector; using std::setprecision;
using std::max;
int main()
{
// ask for and read the first temperature.
// Tell the user they can enter -999 when they have finished.
cout << "Enter the first temperature. When done EOF should be -999: ";
double temp, time;
cin >> temp;
vector<double> tempvec; // declare a vector for holding the temperatures
vector<double> timevec; // declare a vector for holding the times
// the number and sum of temperatures read so far
int count = 0;
double sum = 0;
// enter the loop while the temperature is not EOF
while (temp != -999) {
++count;
sum += temp;
tempvec.push_back(temp); // push back the temperature entered
// into the tempvec vector.
// ask for and read the time the temperature was recorded. The reason
// for asking for the time in the loop is to prevent the system
// asking for a time if the user has entered an EOF.
cout << "Enter the time this was taken. "
<< "\n(in seconds, from the start of the experiment): ";
cin >> time;
// if the user enters a value outside the range of the 24 hour window
// tell them to restart and indicate the range they must use.
if (time > 86400) {
cout << "Please enter a time in the range (0 - 86400): "
"Start again! " // don't know how to loop to re-enter the time??
<< endl;
system("PAUSE");
return 1;
}
timevec.push_back(time); // push back the time entered
// into the timevec vector.
cout << "Enter your next temperature or EOF: ";
cin >> temp;
}
// test to see that the user has entered at least one temperature
typedef vector<double>::size_type vec_sz;
vec_sz size = tempvec.size();
if (size == 0) {
cout << endl << "You must enter some temperatures. "
"Please try again." << endl;
system("PAUSE");
return 1;
}
// work out the average temperature
double average;
average = sum/count;
// hh:mm:ss converting seconds into this time format. ??Don't know
cout << endl
<< "Maximum temperature was " << *max_element (tempvec.begin(),
tempvec.end())
<< " degrees at time/s: " << endl;
// below is nonsense at this stage
cout << *min_element (timevec.begin(), timevec.end())
<< endl;
cout << "Average temperature: " << average << " degrees"
<< endl;
system("PAUSE");
return 0;
}
Sorry, but I've been tearing my hair out all day. Still very new and very confused.
Thanks for taking the time to help.
Cheers
M