I am trying to fill an integer array from a file and then sort the array in order and count the number of occurrences of each element. My question is how do i correctly fill the array from the file? What I have is not working correctly, I think it is in line 26 and 27 but im not sure any help is greatly appreciated thanx
#include <iostream>
#include <fstream>
void sort(int sample_array[], int size);
int main( )
{
usingnamespace std;
cout << "This program sorts numbers from lowest to highest.\n";
int sample_array[50], number_used;
ifstream infile;
char letter;
string line;
infile.open("hw9input.txt");
if (infile.fail())
{
cout << "input file could not be opened" << endl;
system("pause");
exit(1);
}
int index = 0;
while (! infile.eof() )
{
getline (infile,line);
sample_array[index++] ;
}
cout << endl;
sort(sample_array, number_used);
cout << "In sorted order the numbers are:\n";
for (int index = 0; index < number_used; index++)
cout << sample_array[index]<< endl;;
cout << endl;
system("pause");
return 0;
}
/////////////////////////////////////////
// sorts array of integers ascendingly //
/////////////////////////////////////////
void sort(int sample_array[], int size)
{
int out, in, temp;
for (out = 0; out < size-1; out++) // for each element index
for (in = out+1; in < size; in++) // compare with one element index down
if (sample_array[out] > sample_array[in]) // if out element index is greater than
{ // in element index
temp = sample_array[in]; // swap them
sample_array[in] = sample_array[out];
sample_array[out] = temp;
}
}
while (! infile.eof() )
{
getline (infile,line); //display the file contents
cout << line << endl;
}
for (int index = 0; index < SIZE; index++)
{
getline(infile, numlist[index], ','); // fill the array with the file contents
}
and it still doesn't work
does anyone have any ideas?
thank you so much i've been trying to fill this array for hours now, and of course the answer is super simple, now I can start testing the sort function and adding the counter to count how many times each element occurs in the array. Thank you again