Hey I'm currently writing a program which takes a file of data sorts it (using bubble sort) and for some reason I keep getting a segmentation fault.
#include <stdio.h>
#include <stdlib.h>
void compare(int numberstocheck[], int n)
{
int i, j, k;
int thebucket[n];
for(i = 0; i < n; i++) {
thebucket[i] = 0;
}
for(i = 0; i < n; i++) {
thebucket[numberstocheck[i]]++;
}
puts("2");
for(i = 0, j = 0; i < n; i++) {
for(k = thebucket[i]; k > 0; k--){
numberstocheck[j++] = i;
}
}
}
int main( int argc, char *argv[] )
{
if(argc == 3)
{
FILE *numbersfile;
numbersfile = fopen(argv[1], "r");
int numberslength = atoi (argv[2]);
int numberstocheck[numberslength];
//Get the numbers from file
char numbersarray[50];
int count = 0;
while (fgets(numbersarray, 50, numbersfile) != NULL)
{
numberstocheck[count] = atoi(numbersarray);
count++;
}
//Sort the array
compare(numberstocheck, numberslength);
double percentile = numberslength;
percentile = 9 * (percentile / 10);
int othernumber = percentile - 1;
int lowernumbers = numberstocheck[othernumber];
int booleanvalue = 0;
int finalpercentile = -1;
for(count = othernumber + 1; count < numberslength && booleanvalue == 0;
count++)
if (numberstocheck[count] > lowernumbers)
{
finalpercentile = numberstocheck[count];
booleanvalue = 1;
}
printf("The 90th percentile of this file is: %d\n", finalpercentile);
fclose(numbersfile);
}
else
printf("Something went wrong, please check your input!\n");
exit(EXIT_FAILURE);
}