unsigned maxIndex( const double a[], unsigned elements ); Return the index of th
ID: 3648287 • Letter: U
Question
unsigned maxIndex( const double a[], unsigned elements );Return the index of the largest element. If there is no largest element, complain and die. If there is a tie for the largest element, then return the largest element that has the larger subscript.
For example, if the array were {1.1, 3.3, 2.2, 3.3, -4.4}, then the function should return 3.
this far, i have
int main(){
unsigned c[] = {1.1, 3.3, 2.2, 3.3, -4.4};
cout<<"maxInd: "<<maxIndex(c, 5)<<endl;
}
unsigned maxIndex( const double a[], unsigned elements ){
int maxIndex = 0;
for( unsigned i = 0; i < elements; ++i )
if( a[maxIndex] < a[i] )
maxIndex = i;
return maxIndex;
}
Explanation / Answer
I still dont get what you meant by "If there is no largest element, complain and die." In any case there will be a largest element. Trivially this situation never arises. I think the above code that you have written ll work properly except for the fact that when there are two largest number it ll return the index of the number that occurs first. To handle this bug change the if condition as a[maxIndex]Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.