However, it doesn't work properly when the array has more than one element with the same max value...
You have not given any clarification on the current behaviour and expected behaviour when array has more than one element with the same max value. I am assuming that the expected behaviour is to get the last max element index, when there are more than one occurrence of max element in the array, because the max_element()
will give the iterator to first max element as it use operator<
to compare the elements of container, which you are already doing in your program.
Your program is having undefined behaviour because its accessing array element beyond the size of array:
max_element(age, age + num + 1)
here, the max_element()
will end up accessing array beyond its size -
age + num + 1 -> age + 8 + 1 -> age + 9
You should not add 1
to age + num
.
There is a overload of max_element() library function which takes the comparison function object as argument, which returns true
if the first argument is less than the second. The signature of the comparison function should be equivalent to the following:
bool cmp(const Type1 &a, const Type2 &b);
So, you can write your own comparator function which will return true
when the first argument is <=
second, like this:
bool mycompare (const int& lhs, const int& rhs) {
// Using <= operator, so that we get last occurrence of max element in
// the array if max element has more than one occurrence in the given array
return lhs <= rhs;
}
and pass it to max_elemene()
library function
std::max_element(age, age + num, mycompare)
^^^^^^^^^
Make these changes in your program and check.
I hope I can resolve this using int array[] since I started programming a few days ago, but if I have to use another type of array, let me know so I can go and learn about it.
The C++ has Containers Library, go through it. For the time being, sequence container array
and vector
will be of our interest. When working in C++ language, it is discouraged to use plain C style array, instead, you should use either array
or vector
based on your requirement. I will suggest you to use the vector
so that you can give different set of inputs and no need to take care of size every time if the input size is different.
Read about std::begin, std::end and std::distance. With all these, you don't need to find number of elements in your container and don't need to do pointer arithmetic to calculate the index of max_element()
returned iterator.
Putting these altogether, implementation of finding index of largest element (index of last occurrence, when largest element has more than one occurrence in input set of elements):
#include <iostream>
#include <algorithm>
#include <vector>
bool mycompare(const int& lhs, const int& rhs) {
// Comparing with <= operator to get the index of last occurrence of largest
// element in the array, if it occur multiple times in array.
return lhs <= rhs;
}
int main() {
std::vector<int> age;
int num;
std::cout << "enter number of elements in age array : " << std::endl;
std::cin >> num;
for (int i = 0; i < num; ++i) {
int input;
std::cout << "Age " << i + 1 << ": ";
std::cin >> input;
age.push_back (input);
}
std::cout << "The index of largest element is : "
<< std::distance (age.begin(),
std::max_element (age.begin(), age.end(), mycompare))
<< std::endl;
return 0;
}
Output:
# ./a.out
enter number of elements in age array :
3
Age 1: 45
Age 2: 23
Age 3: 45
The index of largest element is : 2
# ./a.out
enter number of elements in age array :
5
Age 1: 32
Age 2: 45
Age 3: 67
Age 4: 88
Age 5: 56
The index of largest element is : 3
If you are aware of Lambda Expression, you can use it in place of mycompare()
, like this:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
std::vector<int> age;
int num;
std::cout << "enter number of elements in age array : " << std::endl;
std::cin >> num;
for (int i = 0; i < num; ++i) {
int input;
std::cout << "Age " << i + 1 << ": ";
std::cin >> input;
age.push_back(input);
}
std::cout << "The index of largest element is : "
<< std::distance (age.begin(),
std::max_element(age.begin(), age.end(),
[](const int& lhs, const int& rhs){
return lhs <= rhs;
}))
<< std::endl;
return 0;
}
Suggestions:
1). Avoid adding using namespace std;
in your program. Check this.