Write a function that accepts an array of non-negative integers and returns the second largest integer in the array. Return -1 if there is no second largest.
1
Expert's answer
2012-08-28T12:00:07-0400
#include<iostream>
// a is the array // n is its length int max2(int * a, int n) { int m1=a[0]; // the maximal element // find maximal element of the array for (int i=1; i<n; i++) { if (m1<a[i]) m1=a[i];
}; int m2=-1; // the second maximal element for (int i=0; i<n; i++) { if (m1==a[i]) continue; // skip maximal element if ( m2 < a[i] ) m2=a[i]; }; return m2; };
// test int main() { int a[] = {31,4,6,0,17,8}; std::cout << max2(a, 6) << std::endl;
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
You're welcome. We are glad to be helpful. If you really liked our service please press like-button beside answer field. Thank you!
Thank you for the response....
Leave a comment