471,881 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,881 software developers and data experts.

Retrieving the index of the maximum value in a vector


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:
Average temperature: 24 degrees."


#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) {
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;
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;
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(),
<< " 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;

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.


Jan 2 '07 #1
3 17623
1,510 Expert 1GB

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)

the function call
Expand|Select|Wrap|Line Numbers
  1. *max_element (tempvec.begin(),tempvec.end())
returns an iterator to the maximum element - to get the associated index you can use the distance() function

e.g. to print the time associated with the maximum temperature
Expand|Select|Wrap|Line Numbers
  1. << "Maximum temperature was " << *max_element (tempvec.begin(),tempvec.end())
  2. << "   at time " << timevec.at(distance(tempvec.begin(), max_element (tempvec.begin(),tempvec.end())))<< " ?????? " << endl;
the expression
Expand|Select|Wrap|Line Numbers
  1. distance(tempvec.begin(), max_element (tempvec.begin(),tempvec.end())
returns the distance between the begining of the vector and the iterator to the maximum
Jan 3 '07 #2
Thank you Horace!

This site rocks! Much appreciated.


Jan 3 '07 #3
Some more points:

hours, minutes, and seconds can be calculated like this:

int time_in_seconds;
int hours = time_in_seconds / 3600;
int minutes = (time_in_seconds / 60) % 60;
int seconds = time_in_seconds % 60;

system("PAUSE") is unportable; don't use it.

You might want to try a vector of std::pairs, rather than managing two vectors. You would have to define your own comparison function, though.
Jan 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Guy Deprez | last post: by
9 posts views Thread by Till Crueger | last post: by
29 posts views Thread by Hagen | last post: by
4 posts views Thread by codewarr2000 | last post: by
14 posts views Thread by Sean C. | last post: by
8 posts views Thread by ALiX | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.