#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[100];
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 ;
}
Satish_14
0
Newbie Poster
Recommended Answers
Jump to PostHard 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.
Jump to PostActually to me it looks like the problem is the
return
on line 27, is controlled by thefor
block. Since it's possible for the control to bypass thefor
block(i.e. if start == end) thereturn
could be missed.I think what you're looking for …
All 6 Replies
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
tinstaafl
1,161
Posting Maven
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
tinstaafl
1,161
Posting Maven
JamesCherrill
4,667
Most Valuable Poster
Team Colleague
Featured Poster
DGPickett
20
Newbie Poster
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.