#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 PostI 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 …
All 6 Replies
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
tinstaafl 1,176 Posting Maven
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
tinstaafl 1,176 Posting Maven
JamesCherrill 4,733 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.