Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   String Selection Sort (http://www.programmingforums.org/showthread.php?t=15478)

gmann145 Mar 25th, 2008 10:31 AM

String Selection Sort
 
hey all,, i have to use a selection sort function to search an array of strings, this is what ive got so far, im not sure where to put the selectionSort in the main and so still unsure if its correct



:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void selectionSort(char [], int);
  5.  
  6.  
  7. int main()
  8. {
  9.         const int numNames = 20, size = 17;
  10.         char names[numNames][size]={"Collins, Bill", "Smith, Bart", "Allen, Jim",
  11.                                                                 "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  12.                                                                 "Taylor, Terri", "Looney, Joe", "Wolfe, Bill",
  13.                                                                 "James, Jean", "Weaver, Jim", "Pore, Bob",
  14.                                                                 "Mann, Gregg", "Schneider, Jon", "Johnson, Jill",
  15.                                                                 "Harrison, Rose", "Setzer, Cathy",
  16.                                                                 "Pike, Gordon", "Holland, Beth"};
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. system("pause");
  24. return 0;
  25. }
  26.  
  27.  
  28.       void selectionSort(char arrays[][SIZE], int rows)
  29.       {
  30.           int startScan, minIndex, index;
  31.           int z;
  32.                   char minValue[NUM_NAMES][size];
  33.  
  34.                   for (startScan = 0; startScan < (rows - 1); startScan++)
  35.                   {
  36.                         z = startScan;
  37.                         strncpy(minValue[0], arrays[startScan], 20);
  38.  
  39.                         for (index = startScan + 1; index < rows; index++)
  40.                         {
  41.                                 if (strcmp(arrays[index], minValue[NUM_NAMES]) < 0)
  42.                                 {
  43.                                         strncpy(minValue[0], arrays[index], 20);
  44.                                         minIndex = arrays[z];
  45.                 }
  46.             } 
  47.                         strncpy(arrays[startScan], arrays[minIndex] , 20);
  48.           }
  49.       }


Arla Mar 25th, 2008 10:52 AM

Re: String Selection Sort
 
May just be me, but I have no clue what you are trying to actually achieve.

Can you post some examples of what the results should be and that may make it clearer what you are trying to do, and what your question is.

Jimbo Mar 25th, 2008 10:43 PM

Re: String Selection Sort
 
You probably want to call it after you've declared your list of strings. And then afterwards, either have a function to verify that it's sorted correctly, or print the sorted array and manually verify.

gmann145 Apr 1st, 2008 8:34 AM

Re: String Selection Sort
 
basically the program is supposed to print the names of the array out in alphabetical based on the first name that you see, which happens to be their last for example:


Allen, Jim
Holland, Beth

gmann145 Apr 1st, 2008 9:45 AM

Re: String Selection Sort
 
:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5.  
  6. void selectionSort(char [], int);
  7. void showArray(char [], int);
  8.  
  9.  
  10. int main()
  11. {
  12.         const int numNames = 20, size = 17;
  13.         char names[numNames][size]= { "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
  14.                                                                 "Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
  15.                                                                 "Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
  16.                                                                 "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
  17.                                                                 "Moretti, Bella", "Wu, Hong", "Patel, Renee",
  18.                                                                 "Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
  19.                                                                 "Kelly, Sean", "Holland, Beth" };
  20.  
  21. cout << "The unsorted string is: \n";
  22. showArray(names, size);
  23. selectionSort(names, SIZE);
  24. cout << "The sorted string is: \n";
  25. showArray(names, size);
  26.  
  27. system("pause");
  28. return 0;
  29. }
  30.  
  31.  
  32. selectionSort(names, size);
  33.  
  34. showArray(names, size);
  35. system("pause");
  36. return 0;
  37. }
  38.  
  39.  
  40.       void selectionSort(char arrays[][size], int rows)
  41.       {
  42.           int startScan, minIndex, index;
  43.           int z;
  44.                   char minValue[NUM_NAMES][size];
  45.           for(int i=0,; i>17; i++)
  46.                   {
  47.                           minValue[i]=arrays[0][i];
  48.  
  49.                   }
  50.  
  51.                   for (startScan = 0; startScan < (rows - 1); startScan++)
  52.                   {
  53.                         z = startScan;
  54.                         strncpy(minValue[0], arrays[startScan], 20);
  55.  
  56.                         for (index = startScan + 1; index < rows; index++)
  57.                         {
  58.                                 if (strcmp(arrays[index], minValue[NUM_NAMES]) < 0)
  59.                                 {
  60.                                         strncpy(minValue[0], arrays[index], 20);
  61.                                         minIndex = arrays[z];
  62.                 }
  63.             } 
  64.                         strncpy(arrays[startScan], arrays[minIndex] , 20);
  65.           }
  66.       }
  67.  
  68.  
  69. void showArray(char arrays[], int size)
  70. {
  71.         for (int count = 0; count < size; count++)
  72.                 cout<<arrays[count] << " ";
  73.         cout<<endl;
  74. }





some work of i've added since starting, i keep getting an error that says cannot convert parameter 1 from 'char [20][17]' to 'char []'

Jimbo Apr 1st, 2008 10:14 AM

Re: String Selection Sort
 
Quote:

Originally Posted by gmann145 (Post 143299)
some work of i've added since starting, i keep getting an error that says cannot convert parameter 1 from 'char [20][17]' to 'char []'

I'm pretty sure the problem is in your showArray, where you pass it something of type char[][] but expect something of type char[]. Change the function to accept your list of arrays, and you should be fine (logic should all still work appropriately, I think).

For future reference, please provide a line number with errors; makes it easier for us to find out where they are :icon_wink:

gmann145 Apr 1st, 2008 10:39 AM

Re: String Selection Sort
 
:

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. const int numNames = 20, size = 17;
  6. void selectionSort(char [][size], int);
  7. void showArray(char [][size], int);
  8.  
  9.  
  10. int main()
  11. {
  12.  
  13.         char names[numNames][size]= { "Collins, Bill", "Smith, Bart", "Michalski, Jacob",
  14.                                                                 "Griffin, Jim", "Sanchez, Manny", "Rubin, Sarah",
  15.                                                                 "Taylor, Tyrone", "Johnson, Jill", "Allison, Jeff",
  16.                                                                 "Moreno, Juan", "Wolfe, Bill", "Whitman, Jean",
  17.                                                                 "Moretti, Bella", "Wu, Hong", "Patel, Renee",
  18.                                                                 "Harrison, Rose", "Smith, Cathy", "Conroy, Patrick",
  19.                                                                 "Kelly, Sean", "Holland, Beth" };
  20.  
  21. cout << "The unsorted string is: \n";
  22. showArray(names, numNames);
  23. selectionSort(names, numNames);
  24. cout << "The sorted string is: \n";
  25. showArray(names, numNames);
  26.  
  27. system("pause");
  28. return 0;
  29. }
  30.  
  31.  
  32.  
  33.       void selectionSort(char arrays[][size], int rows)
  34. {
  35.           int startScan= 0, minIndex;
  36.           char minValue[17];
  37.  
  38.         for(int i=0; i>17; i++)
  39.         {
  40.         minValue[i]=arrays[startScan][i];
  41.         }
  42.  
  43.                 for (startScan = 0; startScan < (rows - 1); startScan++)
  44.                   {
  45.                         minIndex = startScan;
  46.                         for(int i=0; i>17; i++)
  47.                   {
  48.                           minValue[i]=arrays[startScan][i];
  49.                   }
  50.  
  51.  
  52.                         for (int index = startScan + 1; index < rows; index++)
  53.                         {
  54.                                 if (strcmp(arrays[index], minValue) < 0)
  55.                                 {
  56.                                         for(int i=0; i>17; i++)
  57.                                         {
  58.                           minValue[i]=arrays[index][i];
  59.                   }
  60.                                         minIndex = index;
  61.             }
  62.                                 } 
  63.                         for(int i=0; i>17; i++)
  64.                         {
  65.                           arrays[minIndex][i]=arrays[startScan][i];
  66.                         }
  67.                           for(int i=0; i>17; i++)
  68.                                 {
  69.                           arrays[startScan][i]=minValue[i];
  70.                                 }
  71.                                         }
  72. }
  73.  
  74.  
  75. void showArray(char arrays[][size], int rows)
  76. {
  77.         for (int count = 0; count < rows; count++)
  78.                 cout<<arrays[count] << " "<<endl;
  79.         cout<<endl;
  80. }




haha ok now ive got everything working it just seems to not want to sort the names (X_X)

misho Apr 19th, 2008 5:17 PM

Re: String Selection Sort
 
None of your loops actually ever run:

:

  1. for(int i=0; i>17; i++)


You make i=0 and the loop only runs if i>17.

Jabo Apr 19th, 2008 7:09 PM

Re: String Selection Sort
 
actually, it will run once 17 is exceeded; if that ever happens. I haven't looked at the code to see.

lectricpharaoh Apr 19th, 2008 8:32 PM

Re: String Selection Sort
 
Quote:

Originally Posted by Jabo
actually, it will run once 17 is exceeded; if that ever happens. I haven't looked at the code to see.

Since it's initialized to zero there, the next statement that could modify it would be either in the loop (which won't execute if it's zero) or after the loop (in which case it's too late, because the loop is done). The only possible way this could execute is if some other process, thread, or hardware (think memory-mapped I/O) modified the variable in between the initialization and the test. As it wasn't declared volatile, I don't see this happening, as the value would probably be cached in a register. In fact, given the lack of the volatile keyword as a clue to the compiler, any intelligent optimizer would probably eliminate the loop entirely.


All times are GMT -5. The time now is 5:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC