I have to read numbers from a .txt file and sort them. I am trying to use selection sort. The .txt file looks like this:
3
33.3 92.5 4.5 6.29 5.3
94.3 -7.455 4.66 -9.33 -211.2
.03 .12 0.34 4.83 2.494
I am now working on the first 5 numbers (excluding the top number 3) which are 33.3, 92.5, 4.5, 6.29, and 5.3 trying to sort them. When I run the program now it gives me this:
-9.25596e+061 -9.25596e+061 4.5 33.3 92.5 sum= 141.89 average= 28.38
As you can see, the first three numbers, 33.3, 92.5, and 4.5 are sorted correctly. However, the 6.29 and 5.3 have been changed to -9.25596e+061 and placed at the beginning of the sort. I cannot figure out why.
Here is the code block that is supposed to do the sorting:
//////////////////////////////////////////////////////////////
int linecount = 0;
double arrayOne[20];
int pointermove;
in_stream >> pointermove;
while (!in_stream.eof())
{
for(int i=0; i<5; i++)
{
in_stream >> arrayOne[linecount];
sum += arrayOne[linecount];
average += arrayOne[linecount]/5;
for (int i = 0; i < 5; i++) //check sublist for smallest element
{
// Find the minimum in the list[i..listSize-1]
double currentMin = arrayOne[i]; //start with current element
int currentMinIndex = i; //note position
for (int j = i + 1; j < 5; j++)//loop over remaining elements
{
if (currentMin > arrayOne[j])
{
currentMin = arrayOne[j]; //if this element is less, note it as smallest
currentMinIndex = j;
}
}
// Swap list[i] with list[currentMinIndex] if found an element smaller than ith
if (currentMinIndex != i)
{
arrayOne[currentMinIndex] = arrayOne[i];
arrayOne[i] = currentMin;
}
}
cout << arrayOne[i] << " ";
}
cout << "sum= " << sum << " " << "average= " << setprecision(4) << average << endl;
Any help would be appreciated, thank you.