fork download
  1. //Roman Lara Delgado CSC5 Chapter 8, P.487, #6
  2. //
  3. /*******************************************************************************
  4. *
  5. * Arrange Names In Alphabetical Order
  6. *_______________________________________________________________________________
  7. * This program arranges in alphabetical order from A
  8. * through Z a set of names. Every name has a last name
  9. * and a first name.
  10. * ______________________________________________________________________________
  11. * INPUT
  12. * names[] : An arrange that holds the names.
  13. *
  14. * OUTPUT
  15. * A list of the names in alphabetical order.
  16. * ******************************************************************************
  17. * FUNCTION
  18. * ******************************************************************************
  19. * void selectionSort(string[], int)
  20. * ------------------------------------------------------------------------------
  21. * This function arranges the data in ascending order
  22. *
  23. * PARAMETERS:
  24. * array[] : Array to arrange it in ascending order.
  25. * size : The number of elements of the array.
  26. *******************************************************************************/
  27. #include <iostream>
  28. #include <string>
  29.  
  30. using namespace std;
  31.  
  32. void selectionSort(string[], int); //Function prototype.
  33.  
  34. int main()
  35. {
  36. const int NUM_NAMES = 20; //Size of the array.
  37.  
  38. //Array that holds the names.
  39. string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
  40. "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  41. "Taylor, Terri", "Johnson, Jill", "Allison, Jeff",
  42. "Looney, Joe", "Wolfe, Bill", "James, Jean",
  43. "Weaver, Jim", "Pore, Bob", "Rutherford, Greg",
  44. "Javens, Renee", "Harrison, Rose", "Setzer, Cathy",
  45. "Pike, Gordon", "Holland, Beth"};
  46.  
  47. selectionSort(names, NUM_NAMES); //Sorting the names.
  48.  
  49. //Output results
  50. for(int count = 0; count < NUM_NAMES; count++)
  51. {
  52. cout << names[count] << endl;
  53. }
  54. return 0;
  55. }
  56.  
  57. void selectionSort(string array[], int size)
  58. {
  59. int startScan;
  60. int minIndex;
  61. string minValue;
  62.  
  63. for(startScan = 0; startScan < (size - 1); startScan++)
  64. {
  65. minIndex = startScan;
  66. minValue = array[startScan];
  67.  
  68. for (int index = startScan + 1; index < size; index++)
  69. {
  70. if(array[index] < minValue)
  71. {
  72. minValue = array[index];
  73. minIndex = index;
  74. }
  75. }
  76. array[minIndex] = array[startScan];
  77. array[startScan] = minValue;
  78. }
  79. }
  80.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Allen, Jim
Allison, Jeff
Collins, Bill
Griffin, Jim
Harrison, Rose
Holland, Beth
James, Jean
Javens, Renee
Johnson, Jill
Looney, Joe
Pike, Gordon
Pore, Bob
Rose, Geri
Rutherford, Greg
Setzer, Cathy
Smith, Bart
Stamey, Marty
Taylor, Terri
Weaver, Jim
Wolfe, Bill