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;
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]
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.

I suspect if you properly format your code an error will show itself. I think because of bad formatting, your 4 nested `for()` loops are causing a problem.

So? My comment stands... format properly.

Getting back to the code itself, I would strongly recommend separating the sorting operation into it's own function, which would let you test it separately from the input and output code. This would help narrow down the location of the problem. Something like this should do:

``void …``

## All 12 Replies

I also tried this using bubble sort. I got the exact same output. It must mean my problem isn't with the way I'm sorting it.

Bubble Sort:

``````//////////////////////////////////////////////////////////////

int linecount = 0;
double arrayOne;
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++)
{
double currentMin = arrayOne[i];
int currentMinIndex = i;

bool needNextPass = true;

for (int k = 1; k < 5 && needNextPass; k++)
{
needNextPass = false;
for (int i = 0; i < 5 - k; i++)
{
if (arrayOne[i] > arrayOne[i + 1])
{
double temp = arrayOne[i];
arrayOne[i] = arrayOne[i + 1];
arrayOne[i + 1] = temp;

needNextPass = true;
}
}
}
}
cout << arrayOne[i] << " ";
out_stream << arrayOne[i] << " ";
}
out_stream << "sum= " << sum << "   " << "average= " << setprecision(4) << average << endl;
cout << "sum= " << sum << "   " << "average= " << setprecision(4) << average << endl;``````

I suspect if you properly format your code an error will show itself. I think because of bad formatting, your 4 nested `for()` loops are causing a problem.

Well, the last 3 for loops are taken straight from a block of code that I am supposed to use. The bubble sort has 3 for loops which I am not able to change.

So? My comment stands... format properly.

You are very condescending. I'm not sure if you are trying to help or just make yourself feel better because nothing you have said has helped thus far. If I knew how to "format properly" don't you think I would have? I don't need to know what to do, I need to know how.

You are very condescending. I'm not sure if you are trying to help or just make yourself feel better because nothing you have said has helped thus far. If I knew how to "format properly" don't you think I would have? I don't need to know what to do, I need to know how.

I post a link to an explanation on how to format properly and I'm condescending because you can't tell what a link is? I'll keep my real condescending comment to myself.

While WaltP may seem brusque, he's actually quite knowledgable; you'd do well to listen to him on this point.

As for how to format code, WaltP's earlier link (which I'll re-post here for you, in case you missed it) is an excellent primer on the subject This link here gives details on different approaches to indent style, with a specific style (Allman) highlighted. A broad search on indent style or brace style should turn up a plethora of information. One point to be made is that the specific style you choose is less important than being consistent, both with what style you use and in applying it; inconsistently formatting code can be as bad or worse than not styling at all.

If all else fails, you might also look into editing your code with an IDE that has an auto-indent feature. I have been using Code::Blocks a lot lately, and that comes with the Astyle tool (under Plugins). Visual C++ Express has a good auto-styler as well.

What editor or IDE are you using? Chances are, it already has support for auto-indentation, even if you don't know it.

Getting back to the code itself, I would strongly recommend separating the sorting operation into it's own function, which would let you test it separately from the input and output code. This would help narrow down the location of the problem. Something like this should do:

``````void bsort(double arrayOne[], int size)
{
for (int i = 0; i < size; i++)
{
double currentMin = arrayOne[i];
int currentMinIndex = i;

bool needNextPass = true;

for (int k = 1; k < size && needNextPass; k++)
{
needNextPass = false;
for (int i = 0; i < size - k; i++)
{
if (arrayOne[i] > arrayOne[i + 1])
{
double temp = arrayOne[i];
arrayOne[i] = arrayOne[i + 1];
arrayOne[i + 1] = temp;

needNextPass = true;
}
}
}
}
}``````

NOTE: this is not tested code. I simply modified what you already had into a stand-alone function; you'll still need to debug it.

I post a link to an explanation on how to format properly and I'm condescending because you can't tell what a link is? I'll keep my real condescending comment to myself.

While WaltP may seem brusque, he's actually quite knowledgable; you'd do well to listen to him on this point.

As for how to format code, WaltP's earlier link (which I'll re-post here for you, in case you missed it) is an excellent primer on the subject This link here gives details on different approaches to indent style, with a specific style (Allman) highlighted. A broad search on indent style or brace style should turn up a plethora of information. One point to be made is that the specific style you choose is less important than being consistent, both with what style you use and in applying it; inconsistently formatting code can be as bad or worse than not styling at all.

If all else fails, you might also look into editing your code with an IDE that has an auto-indent feature. I have been using Code::Blocks a lot lately, and that comes with the Astyle tool (under Plugins). Visual C++ Express has a good auto-styler as well.

What editor or IDE are you using? Chances are, it already has support for auto-indentation, even if you don't know it.

Thank you very much. And I understand he may be knowledgeable, but knowledge is useless if you can't convey it in an appropriate manner. You on the other hand are very helpful and I can tell by your tone that you want to help. Thank you I will try your suggestions.

Thank you so much Schoil-R-LEA, putting it into a function how you did worked ;)

Now I have a new problem. If you could help again I would be very grateful. I am trying to sort the different lines independently of each other, however when it is sorting them now it is sorting them as if all the numbers are one large string and I cannot figure out why. Thank you if you are able to help. Here is my code:

``````while (!in_stream.eof())
{
for(int i=0; i<5; i++)
{
in_stream >> arrayOne[linecount];
sum += arrayOne[linecount];
average += arrayOne[linecount]/5;
bsort(arrayOne,5);
}

for (int i = 0; i < 5; i++)
{
cout << arrayOne[i] << " ";
out_stream << arrayOne[i] << " ";
}

out_stream << "sum= " << sum << "   " << "average= " << setprecision(4) << average << endl;
cout << "sum= " << sum << "   " << "average= " << setprecision(4) << average << endl;

///////////////////////////////////////////////////////////////////////////////////

for(int i=5; i<10; i++)
{
in_stream >> arrayOne[linecount];
sum2 += arrayOne[linecount];
average2 += arrayOne[linecount]/5;
bsort(arrayOne,5);
}

for (int i = 0; i < 5; i++)
{
cout << arrayOne[i] << " ";
out_stream << arrayOne[i] << " ";
}

out_stream << "sum= " << setprecision(3) << sum3 << "   " << "average= " << setprecision(3) << average3 << endl;
cout << "sum= " << setprecision(5) << sum2 << "   " << "average= " << setprecision(4) << average2 << endl;``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.