I am suppose to read ints from a text file and sort them as I insert them into an array of size 10 without going out of bounds.
Doing that was not a problem, but I was told I need to read the WHOLE text file and sort data, but the data must be sorted BEFORE being put into an array, I do not see how this is possible with an array of only 10 and a text file with 15 digits. How can my program know if one of the numbers beyond 10 should or should not be in the array?
Also I am not allowed to use any advanced sorting functions..so I am at a complete loss. Any help would be appreciated, Thanks!
void sortArray(int iArray[])
{
string fileName = "";
fstream inFile;
int tmp = 0;
int size = 10;
//Prompt user to input the name of the file
cout << "please enter file name: ";
cin >> fileName;
//Open file
inFile.open(fileName);
int j = 0;
int i = 0;
//Insertion sort, compares data as its read to the array and swaps numbers if needed
while( !inFile.eof() && i < size)
{
inFile >> tmp;
iArray[i] = tmp;
j = i;
while (j > 0 && iArray[j-1] > iArray[j])
{
swap(iArray[j], iArray[j-1]);
j--;
}
i++;
}
inFile.close();
}