Reputation: 1695
Every time i run the program I receive the message: Segmentation fault (core dumped). I tried doing a bit of research and it seems the issue has to do with allocating to illegal memory. I've also tried debugging the program and it seems the problem lies with in the linearsort() function since after commenting it out, the rest of the statements work correctly.
#include <iostream>
using namespace std;
int main()
{
void linearsort(int [], int);
int arr[10];
for( int j = 0; j < 10; j++)
arr[j] = j +1;
linearsort(arr,10);
for(int i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
cin.get();
return 0;
}
void linearsort(int arr[],int n)
{
int temp;
for(int pass = 0; pass < n - 1; n++)
for(int cand = pass + 1; cand < n; cand++){
if(arr[pass] > arr[cand]){
temp = arr[pass];
arr[pass] = arr[cand];
arr[cand] = temp;
}
}
}
Upvotes: 0
Views: 4433
Reputation: 15916
for(int pass = 0; pass < n - 1; n++)
You are incrementing the wrong value, n++
should be pass++
. Meaning right now, you're accessing out of bound indices in your array.
Upvotes: 8