By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Retrieving the index of the maximum value in a vector

P: 23
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
Jan 2 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
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)

M
the function call
Expand|Select|Wrap|Line Numbers
  1. *max_element (tempvec.begin(),tempvec.end())
  2.  
returns an iterator to the maximum element - to get the associated index you can use the distance() function
http://www.sgi.com/tech/stl/distance.html

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;
  3.  
the expression
Expand|Select|Wrap|Line Numbers
  1. distance(tempvec.begin(), max_element (tempvec.begin(),tempvec.end())
  2.  
returns the distance between the begining of the vector and the iterator to the maximum
Jan 3 '07 #2

P: 23
Thank you Horace!

This site rocks! Much appreciated.

Cheers

M
Jan 3 '07 #3

P: 38
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.