1

Not sure if I’ve posted the right question for what I’m actually asking but here it is. I am trying to print the maximum and minimum rainfall from the sumofrain vector along with the corresponding year from the yearly vector.

Just to make it clear if the maximum rainfall in the sumofrain vector was the 7th value in the vector then the year I would like being output is the 7th value in the yearly vector ect.

6
  • What's your actual question? Commented Mar 4, 2015 at 15:51
  • How can i print the maximum and minimum value of the 'sumofrain' (which i've already done) with the corresponding year in the 'yearly' vector. For example if the maximum value being output is 7th in the 'sumofrain' vector then how can i also output the 7th value in the 'yearly' vector. @TheForestAndTheTrees Commented Mar 4, 2015 at 15:53
  • @IrrationalPerson It is in the question. Commented Mar 4, 2015 at 15:57
  • You don't need a full sort to do what you need, there's a much simpler way to do it. Commented Mar 4, 2015 at 15:58
  • 1
    Simplest way to do this would be to create a class to read everything into, instead of having everything in separate vectors. Commented Mar 4, 2015 at 16:03

3 Answers 3

1

Given that the vectors yearly and sumofrain are not jagged, you can simply check the distance from the beginning.

// Get iterator to the max element in container. auto it = std::max_element(std::begin(sumofrain), std::end(sumofrain)); // Get index of element pointed to by iterator. auto index = std::distance(std::begin(sumofrain), it); auto maxRain = *it; auto maxYear = yearly[index]; std::cout << "Wettest year: " << maxYear << ". "; std::cout << "Rainfall recorded: " << maxRain << " mm." << std:: endl; 
Sign up to request clarification or add additional context in comments.

5 Comments

when using cout to display this what variable would i use? and thank you for your help. @snps
@louiej15 Both maxRain and maxYear have type double (value type of the containers), and you can output these using std::cout and << operator.
This brings the error, 'it,index,maxYear,maxRain' does not name a type
What compiler are you using? Possibly it's not c++11 compliant, required for (that usage of) the auto keyword.
@louiej15 The modern use of the auto keyword for type deduction is introduced in C++11, the second most recent standard (current is C++14). If you are using an old compiler that does not support the more recent standards you might get these errors.
0

Store the sum-of-rain in a map of year -> double.

std::map<int, double> rainfall; while(file >> year >> month >> maxdeg >> mindeg >> afdays >> rainmm >> sunhours) { rainfall[year] += rainmm; frost.push_back(afdays); } 

Then, when the file is done reading, find the entry in the map with the lowest/highest rainfall, via e.g. Finding minimum value in a Map

The iterator you get back has ->first which is the year and ->second which is the actual amount of rainfall for that year.

Comments

0

You should store the iterator returned by max_element(sumofrain.begin(),sumofrain.end()) into a variable.

auto itMax = max_element(sumofrain.begin(),sumofrain.end()); 

Then, to find out the zero-based index of the found element:

itMax - sumofrain.begin(); 

And to find the value :

*itMax 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.