0

I was trying to find the Maximum average marks of student in 2D vector of string. After compilation i am getting Runtime error.

For Eg . The Average Marks of students are : James 80 , Nick 65 , Amit 50 , Fernando 40. Now the maximum Average among them are James 80

Is there any issue inside Lambda function ? Why is the below program Crashing?

#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<vector<string> > vect{{"James", "70"}, {"Fernando", "40"},
                               {"Nick", "60"},  {"James", "90"},
                               {"Nick", "70"},  {"Amit", "50"}};

  auto it = max_element(vect.cbegin(), vect.cend(),
                        [](auto const& left, auto const& right) {
                          return stoi(left[1]) < stoi(left[2]);
                        });

  cout << (*it)[0] << " : " << (*it)[1] << endl;
  return 0;
}

Expected output : James 80

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
susjaisw
  • 37
  • 2
  • 1
    You mean `James 90`? – john Aug 08 '19 at 06:48
  • 6
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Aug 08 '19 at 06:49
  • 8
    Did you mean `return stoi(left[1]) < stoi(right[1]);`? – Daniel Langr Aug 08 '19 at 06:51
  • James Average is (90 + 70)/2= 80 Which is maximum. – susjaisw Aug 08 '19 at 06:52
  • But you're not calculating or printing any average? – Some programmer dude Aug 08 '19 at 06:53
  • @user8736236 What does average have to do with anything? You are trying to print the maximum element. Anyway Daniel has told you the answer. – john Aug 08 '19 at 06:53
  • You cannot do this by `std::max_element` alone. First, you need to calculate average values (for instance, by using some associative container) and then find the maximum. – Daniel Langr Aug 08 '19 at 06:56
  • What kind of runtime error? Your debugger will most likely tell you what's wrong with your code or at least where the error lies. – nada Aug 08 '19 at 06:56
  • Thanks Daniel.Can you please help me to get the maximum average among all. My code is not calculating any average. Please help me to get the maximum Average. Please provide me the sample code – susjaisw Aug 08 '19 at 06:58
  • @nada To be fair, the OP does use `std::stoi` so they do understand that bit. – john Aug 08 '19 at 07:02
  • @user8736236 StackOverflow is not a code-writing service. You should try something first by yourself and then ask if you run into troubles. I gave you a hint how to proceed. – Daniel Langr Aug 08 '19 at 07:07
  • 1
    `std::vector` is a fairly terrible type for holding the marks for a student. Can you not write use `struct Student { std::string name; int mark; };`? – Caleth Aug 08 '19 at 09:06
  • I can do it other way as well but how can we do it using the above input – susjaisw Aug 08 '19 at 09:08

1 Answers1

4

You have two small issues in your lambda. First (and as noted in the comments), it should compare left and right, not left and left. Second, the indices are incorrect - left[2] is an out of bounds access which yields undefined behavior. You can fix both with this:

[](auto const& left, auto const& right)
    { return stoi(left[1]) < stoi(right[1]); }
    //                            ^^^^^ ^
lubgr
  • 37,368
  • 3
  • 66
  • 117
  • 1
    Thanks i got the issue now. Can you please help me to get the Maximum Average among all student. for eg. James Average is (90 + 70)/2= 80 Which is maximum – susjaisw Aug 08 '19 at 07:01