Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. //DISPLAY 7.12 Sorting an Array
  2. //Tests the procedure sort.
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. using namespace std;
  7. void fill_array(vector<string> &a);
  8. //Precondition: size is the declared size of the array a.
  9. //Postcondition: number_used is the number of values stored in a.
  10. //a[0] through a[number_used - 1] have been filled with
  11. //nonnegative integers read from the keyboard.
  12.  
  13. void sort(vector<string> &a);
  14. //Precondition: number_used <= declared size of the array a.
  15. //The array elements a[0] through a[number_used - 1] have values.
  16. //Postcondition: The values of a[0] through a[number_used - 1] have
  17. //been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1].
  18.  
  19. void swap_values(string& v1, string& v2);
  20. //Interchanges the values of v1 and v2.
  21.  
  22. int index_of_smallest(const vector<string> a, int start_index);
  23. //Precondition: 0 <= start_index < number_used. Referenced array elements have
  24. //values.
  25. //Returns the index i such that a[i] is the smallest of the values
  26. //a[start_index], a[start_index + 1], ..., a[number_used - 1].
  27.  
  28. int main( )
  29. {
  30. cout << "This program sorts numbers from lowest to highest.\n";
  31.  
  32. vector<string> sample_array;
  33. fill_array(sample_array);
  34. sort(sample_array);
  35.  
  36. cout << "In sorted order the numbers are:\n";
  37. for (int index = 0; index < sample_array.size(); index++)
  38. cout << sample_array[index] << " ";
  39. cout << endl;
  40.  
  41. return 0;
  42. }
  43.  
  44. //Uses iostream:
  45. void fill_array(vector<string> &a)
  46. {
  47. /*string q = "z";
  48. a.push_back(q);
  49. string w = "k";
  50. a.push_back(w);
  51. string e = "j";
  52. a.push_back(e);*/
  53.  
  54. string buffer;
  55. int i =1;
  56. cout << "Enter the " << i << "st string in the list: ";
  57. getline(cin,buffer);
  58. a.push_back(buffer);
  59. i++;
  60. while(buffer.length() != 0)
  61. {
  62. if(i == 2)
  63. {
  64. cout << "Enter the " << i << "nd string in the list: ";
  65. getline(cin, buffer);
  66. a.push_back(buffer);
  67. i++;
  68. }
  69. else if( i == 3)
  70. {
  71. cout << "Enter the " << i << "rd string in the list: ";
  72. getline(cin, buffer);
  73. a.push_back(buffer);
  74. i++;
  75. }
  76. else
  77. {
  78. cout << "Enter the " << i << "th string in the list: ";
  79. getline(cin, buffer);
  80. a.push_back(buffer);
  81. i++;
  82. }
  83. }
  84.  
  85. }
  86.  
  87. void sort(vector<string>&a)
  88. {
  89. int index_of_next_smallest;
  90. cout << "lol" << endl;
  91. //<The rest of the definition of fill_array is given in Display 7.9.>
  92. for (int index = 0; index < a.size() - 1; index++)
  93. {//Place the correct value in a[index]:
  94. index_of_next_smallest =
  95. index_of_smallest(a, index);
  96. cout << "lol" << endl;
  97. swap_values(a[index], a[index_of_next_smallest]);
  98. //a[0] <= a[1] <=...<= a[index] are the smallest of the original array
  99. //elements. The rest of the elements are in the remaining positions.
  100. }
  101. }
  102.  
  103.  
  104. void swap_values(string& v1, string& v2)
  105. {
  106. string temp;
  107. temp = v1;
  108. v1 = v2;
  109. v2 = temp;
  110. }
  111.  
  112.  
  113. int index_of_smallest(const vector<string> a, int start_index)
  114. {
  115. cout << "lol2" << endl;
  116. string min;
  117. cout << "lol3"<< endl;
  118. min = a[start_index];
  119. cout << "lol4"<< endl;
  120. int index_of_min = start_index;
  121.  
  122. for (int index = start_index + 1; index < a.size(); index++)
  123. if (a[index] < min)
  124. {
  125. min = a[index];
  126. index_of_min = index;
  127. //min is the smallest of a[start_index] through a[index]
  128. }
  129.  
  130. return index_of_min;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement