0

Am using 3 parallel arrays and using a selection sort that is to sort by miles in descending order. It does sort in descending order but when for example i enter data in the input file as follows:

  • NC 33.9 5
  • VA 22.2 0
  • etc.

Then the program will print the 'VA' data twice and not display the 'NC' data. Parallel arrays not my idea neither is the selection sort. They are required. please help

void sortEntries( string initials[], double miles[], int shelters[] )
{
   int top, bottom, i, n;;
   string tempInitials;
   double tempMiles;
   int tempShelters;

   //Selection sorting for initials[]
   n = 14;
   for( top = 0; top < MAX_ENTRIES - 1; top++ )
   {  
      tempInitials = initials[smallest];
      tempMiles = miles[smallest];
      tempShelters = shelters[smalles];
      smallest = top; 
      //Locate smallest number between 1 and MAX_ENTRIES(14)
      for ( i = top; i < MAX_ENTRIES; i++ )
      {
         if ( initials[i] < tempMiles )
         {
            tempInitials = initials[i];
            tempMiles = miles[i];
            tempShelters = shelters[i];
            smallest = i;
         }
      }
      initials[top] = initials[smallest];
      miles[top] = miles[smallest];
      shelters[top] = shelters[smallest];
      initials[smallest] = tempInitials;
      miles[smallest] = tempMiles;
      shelters[smallest] = tempShelters;    
   }     
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3317020
  • 45
  • 1
  • 2
  • 10
  • First error: You are comparing a string to a double...what? – smac89 Apr 14 '14 at 03:39
  • Second error: parallel arrays. Use a single array of structs or classes containing the data you want to sort. Poor design, and an open invitation to unnecessary bugs. – user207421 Apr 14 '14 at 03:50
  • That is what i have been told numerous times. Truth is this is a hw assignment and the teacher has required use of 3 parallel arrays sorted with a selection sort.. – user3317020 Apr 14 '14 at 03:56
  • Iv now been able to get it to sort in descending order. but instead if i enter the data in the file out of order. When there is a big number over a smaller number it print the smaller number parallel array twice – user3317020 Apr 14 '14 at 03:59
  • Strange teaching. You don't need the first three assignments in the inner loop. You could do them once at the end, before the current assignments there, using '[smallest]' instead of '[i]' in each case. It would be clearer too. You would then see exactly what the problem is, as I can. – user207421 Apr 14 '14 at 04:18

0 Answers0