My program needs to take the first 5 numbers from a file, sort them and print them out. Then take the next 5 numbers and sort just those 5 numbers and print them out. However, when I try to sort them independently it is sorting them all together as if I wanted all 10 to be sorted. The first sort is fine because it only deals with the first 5 numbers, but the second sort that I try to do includes the previous numbers too and I cannot figure out why. Can anyone help me please? 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;    
   selectionSort(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;
   selectionSort(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;

I hate to sound like a broken record, but you really need to get your code formatting fixed. As inconsistent as it is now, it's almost impossible to read.

As for the problem, the issue is that you are passing the whole array to the sorting function, rather than just the part you mean to sort. Try the following at line 28:

selectionSort(arrayOne + 5, 5);

This uses what is called 'pointer arithmetic', and while you want to be very careful with it, it should offset the array to the section you want to sort.

Sorry, I am so new to c++ that I can't tell a "formatted" code from an unformatted one. I tried to do that at line 28 but it's still doing the same thing. Thank you for trying, any other suggestions would be incredibly helpful, I have to turn this in in a few hours :(

Be a part of the DaniWeb community

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