The program compiles but the numbers do not sort and it ends with "segmentation fault". Help? I am using a c++ compiler
void Selection_sort(int [], int);
void Swap (int*, int &, int &);
void Display_list(int [], int);
int main (){
const int count=10; // number of input values
int i=0;
int List[count];
cout << "Enter " << count << " values: ";
for (i=0; i<count; i++)
{
cin >> List[i];
}
Display_list(List, count); // before sorting
Selection_sort(List, count);
Display_list(List, count); // after sorting
return 0;
}
void Selection_sort(int L[], int cnt){
int largest;
int i;
int n=i-1;
for(i=1; i<10; i++)
{
if (L[i]>largest)
largest= L[i];
Swap(L,largest,n);
}
}
void Swap (int l[],int &A, int &B){
int temp;
temp=l[B];
l[B]=l[A];
l[A]=temp;
}
void Display_list(int L[], int cnt){
cout<<"The numbers in this array are : \n"<<endl;
for (int i=0; i<cnt; i++)
cout<<L[i]<<endl;
cout<<"\n"<<endl;
}
iandlargestare used uninitialized. 3.cntis unused. 4. inSwap, there's no reason forAandBto be references. 5. You should use consistent indentation. 6. You should share the code of the caller, as it might explain the crash (though the reason is probably thatndepends on uninitialized use ofi). 7. This is off-topic for code review. – asveikau Nov 28 '12 at 6:42