I thought I had posted this earlier but...
I am working on a sorting problem using counting sort but am getting a crash when I run the program. I have fixed some of the problems but I am still missing something. I just need a extra pair of eyes to find the rest.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
void countSort(int nums[], int size);
int main()
{
int i;
char filename[50];
int arraysize = 8; //the size of the array with all of its elements;
int numbers[8]; //the number of elements in the array
ifstream infile;
cout<<"Enter the name of the file you wish to open "<<endl;
cin>>filename;
infile.open(filename); //open the input file
if(!infile.is_open()) //if the input file does not open
{
cout<<"Could not open the file "<<filename<<" The program will now close."<<endl;
exit(EXIT_FAILURE); //exit the program
}
while(infile.good()) //if the file opens
{
for(i = 0; i <= arraysize; i++) //read all of the integers from the file...
infile >> numbers[i]; //..into the array, numbers
}
countSort(numbers, arraysize);
for(i = 0; i <= arraysize; i++)
cout << numbers[i] << endl; //prints out the numbers to the console
infile.close(); //close input file
system("PAUSE");
return EXIT_SUCCESS; //exit the program
}
void countSort(int numbers[], int size)
{
int i, counter, j;
int min = numbers[0];
int max = min;
int a[8], b[8], c[max];
for(i = 0; i <= size; i++)
{
if(numbers[i] < min)
min = numbers[i];
else if(numbers[i] > max)
max = numbers[i];
}
for(i = 0; i <= max; i++)
c[i] = 0;
for(j = 0; j <= 8; j++)
c[a[j]] = c[a[j]] + 1;
for(i = 0; i <= max; i++)
c[i] = c[i] + c[i - 1];
for(j = 0; j <= 8; j--)
{
b[c[a[j]]] = a[j];
c[a[j]] = c[a[j]] -1;
}
}