``````#include<iostream>
using namespace std;
int findmaxindex(int a[],int ,int);
void swap(int a[],int ,int);
int main(){
int n;
cout<<"give the number of elements to sort"<<endl;
cin>>n;
if(n>100){cout<<"invalid"<<endl;
return -1;
}
if(n<=0){cout<<"invalid"<<endl;
return -1;
}
int count,a;
cout<<"give"<<n<<"integers to sort"<<endl;
for(count=0;count<n;count++){
cin>>a[count];
}
int currtop,currmaxindex;
for(currtop=0;currtop<n;currtop++){
currmaxindex=findmaxindex(a,currtop,n);
swap(a,currtop,currmaxindex);
}
return 0;
}
int findmaxindex(int a[],int start,int end){
int i,currmaxindex=start;
for(i=start;i<end;i++){
if(a[i]>=a[currmaxindex]){
currmaxindex=i;
}return currmaxindex;
}

}
void swap(int a[],int index1,int index2){
int temp;
temp=a[index1];
a[index1]=a[index2];
a[index2]=temp;
return ;
}``````

## All 6 Replies

Hard to read with no indentation, but it looks like the function at line 27 has its return statement inside an if test, so if the if test fails control will evntually drop through to line 35 where the function finishes without having returned an int.

Actually to me it looks like the problem is the `return` on line 27, is controlled by the `for` block. Since it's possible for the control to bypass the `for` block(i.e. if start == end) the `return` could be missed.

I think what you're looking for is something like this:

``````int findmaxindex(int a[],int start,int end){
int i,currmaxindex=start;
for(i=start;i<end;i++){
if(a[i]>=a[currmaxindex]){
currmaxindex=i;
}
}
return currmaxindex;
}``````

Yes. The return is inside both a `for` and an `if` block, so either could prevent the return from ever being executed.

The return is outside the if block but inside the for block as this formatted code shows:

``````int findmaxindex(int a[],int start,int end){
int i,currmaxindex=start;
for(i=start;i<end;i++){
if(a[i]>=a[currmaxindex]){
currmaxindex=i;
}
return currmaxindex;
}
}``````

The if block has nothing to do with the error

Thanks for formatting the code, and yes, you're right.

I usually go with another int set to -max (0x80000000) when seeking a max, so the first is generally greater and replaces it. Using <= means equal values get copied, and as writes are more expensive than reads (think cache), why bother unless either you are required to get the last not the first, or -max is actually in the array?

You need to return the index after the loop, the major bug that the error is indirectly warning you about. More generally, even if you know some return in a loop or conditional block will fire always, you will get this warning unless there is a return before the ending curly brace, so either say 'break' or let the loop run out, and let the code fall on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.