Advertisement
ssnr

Untitled

Nov 6th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. Assignment 10.1
  2.  
  3.  
  4. /*
  5. Name:
  6. Date:
  7. Assignment Number: 10.1
  8. Instructor: Dave Harden
  9. File: a10.cpp
  10.  
  11. This program demonstrates the use of a vector and iterators to navigate and
  12. access the vector. The program models capturing a set of records consisting
  13. of a name and a score. The vector elements are structs. The struct consists
  14. of a pointer to a C string to store a name, and an int to store a score.
  15.  
  16. The program begins by asking the user how many scores will be recorded.
  17. Then the vector is instantiated with the required size, and functions are
  18. called to input the scores, sort them, and then display them.
  19.  
  20. */
  21.  
  22. #include <iostream>
  23. #include <vector>
  24. using namespace std;
  25.  
  26. struct highscore{
  27. char* name;
  28. int score;
  29. };
  30.  
  31. const int MAX_NAMESIZE = 24;
  32.  
  33. void getVectorSize(int& size);
  34. void initializeData(vector<highscore>& scores);
  35. void sortData(vector<highscore>& scores);
  36. void displayData(const vector<highscore>& scores);
  37.  
  38. int main()
  39. {
  40. int size;
  41. getVectorSize(size);
  42. vector<highscore> scores(size);
  43.  
  44. initializeData(scores);
  45. sortData(scores);
  46. displayData(scores);
  47.  
  48. for (vector<highscore>::iterator i = scores.begin(); i < scores.end(); ++i){
  49. delete [] i->name;
  50. }
  51.  
  52. return 0;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59. /*
  60. void getVectorSize(int& size);
  61.  
  62. The number of scores to be entered is captured in the
  63. pass-by-reference parameter, size.
  64.  
  65. */
  66. void getVectorSize(int& size){
  67. cout << "How many scores will you enter?: ";
  68. cin >> size;
  69. cin.get();
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*
  77. void initializeData(vector<highscore>& scores);
  78.  
  79. Names and scores are input to the vector, scores.
  80.  
  81. */
  82. void initializeData(vector<highscore>& scores)
  83. {
  84. char temp[MAX_NAMESIZE];
  85. int counter = 1;
  86. vector<highscore>::iterator it;
  87.  
  88. for(it = scores.begin(); it < scores.end(); ++it)
  89. {
  90. cout << "Enter the name for score #" << counter << ": ";
  91. cin.getline(temp,MAX_NAMESIZE,'\n');
  92. it->name = new char[strlen(temp) + 1];
  93. strcpy(it->name, temp);
  94.  
  95. cout << "Enter the score for score #" << counter << ": ";
  96. cin >> it->score;
  97. cin.get();
  98. ++counter;
  99. }
  100. cout << endl;
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108. /*
  109. void sortData(vector<highscore>& scores);
  110.  
  111. The vector, scores, is sorted in descending order by score
  112. using a selection sort.
  113.  
  114. */
  115. void sortData(vector<highscore>& scores) {
  116.  
  117. vector<highscore>::iterator it;
  118. vector<highscore>::iterator itLargest;
  119. vector<highscore>::iterator itInner;
  120.  
  121. for(it = scores.begin(); it < scores.end() - 1; ++it){
  122. itLargest = it;
  123. for (itInner = it + 1; itInner < scores.end(); ++itInner){
  124. if (itInner->score > itLargest->score){
  125. itLargest = itInner;
  126. }
  127. }
  128. swap(*it, *itLargest); // or: iter_swap(it, itLargest);
  129. }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. /*
  137. void displayData(const vector<highscore>& scores);
  138.  
  139. Outputs to the console the names and scores stored in the
  140. vector, scores.
  141.  
  142. */
  143. void displayData(const vector<highscore>& scores)
  144. {
  145. vector<highscore>::const_iterator it;
  146. cout << "Top Scorers: " << endl;
  147.  
  148. for (it = scores.begin(); it < scores.end(); ++it)
  149. {
  150. cout << it->name << ": " << it->score << endl;
  151. }
  152. }
  153.  
  154.  
  155.  
  156.  
  157. // This version uses the range-based for loop, a C++11 feature.
  158. // "auto" means the type is set by the context. Here the
  159. // type is highscore, the type of the vector element. You
  160. // could read the for loop header as, "For each element i in
  161. // scores".
  162. //
  163. //void displayData(const vector<highscore>& scores)
  164. //{
  165. // cout << "Top Scorers: " << endl;
  166. // for (const auto & i: scores){
  167. // cout << i.name << ": " << i.score << endl;
  168. // }
  169. //
  170. //}
  171.  
  172. //------------------------------------------------------------------------
  173. //
  174. // This version uses the STL sort
  175.  
  176. /*
  177. Name:
  178. Date:
  179. Assignment Number: 10.1
  180. Instructor: Dave Harden
  181. File: a10.cpp
  182.  
  183. This program demonstrates the use of a vector and iterators to navigate and
  184. access the vector. The program models capturing a set of records consisting
  185. of a name and a score. The vector elements are structs. The struct consists
  186. of a pointer to a C string to store a name, and an int to store a score.
  187.  
  188. The program begins by asking the user how many scores will be recorded.
  189. Then the vector is instantiated with the required size, and functions are
  190. called to input the scores, sort them, and then display them.
  191.  
  192. */
  193.  
  194. #include <iostream>
  195. #include <vector>
  196. using namespace std;
  197.  
  198. struct highscore{
  199. char* name;
  200. int score;
  201. };
  202.  
  203. const int MAX_NAMESIZE = 24;
  204.  
  205. void getVectorSize(int& size);
  206. void initializeData(vector<highscore>& scores);
  207. void displayData(const vector<highscore>& scores);
  208. bool sortByScore(const highscore& lhs, const highscore& rhs); // this function is the third parameter
  209. // of the STL sort function.
  210.  
  211. int main()
  212. {
  213. int size;
  214. getVectorSize(size);
  215. vector<highscore> scores(size);
  216.  
  217. initializeData(scores);
  218. sort(scores.begin(), scores.end(), sortByScore); // call to STL sort.
  219. // the third parameter is a bool
  220. // function. Notice the arguments
  221. // are omitted in this function
  222. // parameter.
  223. displayData(scores);
  224.  
  225. for (vector<highscore>::iterator i = scores.begin(); i < scores.end(); ++i){
  226. delete [] i->name;
  227. }
  228.  
  229. return 0;
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236. /*
  237. void getVectorSize(int& size);
  238.  
  239. The number of scores to be entered is captured in the
  240. pass-by-reference parameter, size.
  241.  
  242. */
  243. void getVectorSize(int& size){
  244. cout << "How many scores will you enter?: ";
  245. cin >> size;
  246. cin.get();
  247. }
  248.  
  249.  
  250.  
  251.  
  252.  
  253. /*
  254. void initializeData(vector<highscore>& scores);
  255.  
  256. Names and scores are input to the vector, scores.
  257.  
  258. */
  259. void initializeData(vector<highscore>& scores)
  260. {
  261. char temp[MAX_NAMESIZE];
  262. int counter = 1;
  263. vector<highscore>::iterator it;
  264.  
  265. for(it = scores.begin(); it < scores.end(); ++it)
  266. {
  267. cout << "Enter the name for score #" << counter << ": ";
  268. cin.getline(temp,MAX_NAMESIZE,'\n');
  269. it->name = new char[strlen(temp) + 1];
  270. strcpy(it->name, temp);
  271.  
  272. cout << "Enter the score for score #" << counter << ": ";
  273. cin >> it->score;
  274. cin.get();
  275. ++counter;
  276. }
  277. cout << endl;
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284. /*
  285. bool sortByScore(const highscore &lhs, const highscore &rhs);
  286.  
  287. Provides the sort algorithm, which here is a descending sort
  288. by the data member, score, of the highscore struct. lhs and
  289. rhs are two highscore structs to be compared.
  290.  
  291. */
  292. bool sortByScore(const highscore &lhs, const highscore &rhs) {
  293. return lhs.score > rhs.score;
  294. }
  295.  
  296.  
  297.  
  298.  
  299.  
  300. /*
  301. void displayData(const vector<highscore>& scores);
  302.  
  303. Outputs to the console the names and scores stored in the
  304. vector, scores.
  305.  
  306. */
  307. void displayData(const vector<highscore>& scores)
  308. {
  309. vector<highscore>::const_iterator it;
  310. cout << "Top Scorers: " << endl;
  311.  
  312. for (it = scores.begin(); it < scores.end(); ++it)
  313. {
  314. cout << it->name << ": " << it->score << endl;
  315. }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement