473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Retrieving the index of the maximum value in a vector

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
3 17899
horace1
1,510 Expert 1GB
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
Thank you Horace!

This site rocks! Much appreciated.

Cheers

M
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

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

Similar topics

0
by: Guy Deprez | last post by:
Hi, i'm having a problem to create indexes. STEP 1 ----------- Connection is OK (you can find the string at the end of the message) Table ("Couleurs") creation is OK STEP 2. Index Creation
9
by: Till Crueger | last post by:
Hi, I have to implement some simple sorting algorithm. I am NOT asking for you to do my homework, but my question is rather on how to store the integers. I recall reading once in here that there...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
4
by: codewarr2000 | last post by:
Having problem with retrieving a class instance item from a Vector. This is the result of the code below. Also a weird note: If I dont declare as: TYPE_VECTOR_BANKED_MEMORY_DATA...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
8
by: ALiX | last post by:
Hi all, In my code I use different vectors of the same size to hold some data. At some point I need to iterate through all vectors at the same time. Now, the question is what type should the...
6
by: jj_frap | last post by:
I'm new to programming in Python and am currently writing a three-card poker simulator. I have completed the entire simulator other than determining who has the best hand (which will be far more...
3
by: BlackShadow33p1 | last post by:
I'm trying to write a program in C++ that gets the handles of all the visible entries in the windows taskbar. The method I've used so far is to send the TB_GETBUTTON message to the taskbar. ...
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.