max_element function in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

max_element function in c++

code 1: // #include<iostream> #include<algorithm> int main(){ int arr[7]{1,2,3,6,4,5,3}; int *m = std::max_element(arr, arr + 7); std::cout << *m; return 0; } // code 2: // #include<iostream> #include<algorithm> void rr(int arr){ int *m = std::max_element(arr, arr + 7); std::cout << *m; ; } int main(){ int arr[7]{1,2,3,6,4,5,3}; rr(arr) return 0; } // when i am compiling code 1 , it is ok. But when i am compiling 2nd one its saying error.Can anyone tell me what is the problem and how to solve it ? I want to take the max value from function

7th Aug 2020, 11:23 AM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar
2 Answers
+ 3
In the second code, you are trying to pass the array as a single integer, not as a pointer to integers. The parameter type should be int*. There is also a semicolon missing after the function call to "rr()" in main.
7th Aug 2020, 11:28 AM
Shadow
Shadow - avatar
0
Thanks for you reply ... And sorry for late reply ....i was a little sick ......
10th Aug 2020, 12:00 PM
Md. Mursalatul Islam Pallob
Md. Mursalatul Islam Pallob - avatar