Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 25th, 2008, 10:31 AM   #1
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
Exclamation 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



c++ Syntax (Toggle Plain Text)
  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. }
gmann145 is offline   Reply With Quote
Old Mar 25th, 2008, 10:52 AM   #2
Arla
Hobbyist Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 227
Rep Power: 4 Arla is on a distinguished road
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.
Arla is offline   Reply With Quote
Old Mar 25th, 2008, 10:43 PM   #3
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 1st, 2008, 8:34 AM   #4
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
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 is offline   Reply With Quote
Old Apr 1st, 2008, 9:45 AM   #5
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
Exclamation Re: String Selection Sort

c++ Syntax (Toggle Plain Text)
  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 []'
gmann145 is offline   Reply With Quote
Old Apr 1st, 2008, 10:14 AM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Re: String Selection Sort

Quote:
Originally Posted by gmann145 View Post
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
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 1st, 2008, 10:39 AM   #7
gmann145
Newbie
 
Join Date: Jan 2008
Posts: 7
Rep Power: 0 gmann145 is on a distinguished road
Re: String Selection Sort

c++ Syntax (Toggle Plain Text)
  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)

Last edited by gmann145; Apr 1st, 2008 at 11:07 AM.
gmann145 is offline   Reply With Quote
Old Apr 19th, 2008, 5:17 PM   #8
misho
Newbie
 
Join Date: Apr 2008
Posts: 10
Rep Power: 0 misho is on a distinguished road
Re: String Selection Sort

None of your loops actually ever run:

c++ Syntax (Toggle Plain Text)
  1. for(int i=0; i>17; i++)

You make i=0 and the loop only runs if i>17.
misho is offline   Reply With Quote
Old Apr 19th, 2008, 7:09 PM   #9
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
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.
Jabo is offline   Reply With Quote
Old Apr 19th, 2008, 8:32 PM   #10
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,039
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 1:01 PM
Throwing an exception when using string constructor csrocker101 C# 3 Apr 8th, 2007 2:04 PM
Help with breaking apart a string csrocker101 C# 6 Apr 6th, 2007 7:50 AM
Function Parameters grimpirate PHP 10 Mar 14th, 2007 6:55 PM
selection sort hopeolicious C++ 2 Mar 15th, 2005 12:47 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:44 AM.

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