Advertisement
Guest User

Untitled

a guest
May 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. /*
  2. * Author: Bradlee Hall
  3. * Title: Grouping App
  4. * Description: This program reads in names from a file, randomly groups the
  5. * names and displays them
  6. * Date Created: 1/20/2017
  7. * Date Last Modified: 5/25/2017
  8. */
  9.  
  10. #include <algorithm>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <string>
  14. #include <time.h>
  15. #include <vector>
  16.  
  17. using namespace std;
  18.  
  19. struct person {
  20.  
  21. string firstName;
  22. int group;
  23. };
  24.  
  25. bool compareByGroup(const person &, const person &);
  26.  
  27. int main() {
  28.  
  29. int numGroups = 0, groupSize = 0, roundStud = 0;
  30.  
  31. vector<person> names;
  32.  
  33. fstream file;
  34. string throwAway;
  35. person student;
  36.  
  37. srand(time(NULL));
  38.  
  39. //Open the file
  40. file.open("names.txt");
  41.  
  42. if (!file) {
  43. cerr << "Cannot read file" << endl;
  44. return 1;
  45. }
  46.  
  47. //Query the user for the amount of students in the groups
  48. cout << "How many students would you like in a group? ";
  49.  
  50. cin >> groupSize;
  51.  
  52. //Load the names into a vector array
  53. while (!file.eof()) {
  54.  
  55. file >> student.firstName;
  56. names.push_back(student);
  57.  
  58. //throw away the last names and the rest of the line
  59. getline(file, throwAway);
  60. }
  61.  
  62. //Randomly shuffle them
  63. random_shuffle(names.begin(), names.end());
  64.  
  65. //Query the user as to group size
  66. while (groupSize != 2 && groupSize != 3 && groupSize != 4 && groupSize != 5) {
  67. cerr << "Please enter 2, 3, 4 or 5: ";
  68. cin >> groupSize;
  69. }
  70.  
  71. roundStud = names.size();
  72.  
  73. //If the number of of students that cannot be divided evenly into a group
  74. //is less than the size of a little more than half the size of a group
  75. //just make groups a little bigger
  76. if ((names.size() % groupSize) < ((groupSize / 2) + 1)) {
  77.  
  78. //Round down to the next lowest group number multiple
  79. while ((roundStud % groupSize != 0)) {
  80.  
  81. roundStud--;
  82. }
  83.  
  84. }
  85. //Otherwise make a few slightly smaller groups
  86. else{
  87.  
  88. //Round up to the next highest group number multiple
  89. while ((roundStud % groupSize != 0)) {
  90.  
  91. roundStud++;
  92. }
  93. }
  94.  
  95. numGroups = (roundStud / groupSize);
  96.  
  97. //Assign groups one-by-one for equal distribution
  98. for(int i = 0; i < names.size(); i++)
  99. names[i].group = ((i % numGroups) + 1);
  100.  
  101. //Sort the vector based on the groups
  102. sort(names.begin(), names.end(), compareByGroup);
  103.  
  104. cout << endl;
  105.  
  106. int currGroup = 1;
  107.  
  108. //Display the groups
  109. while (!names.empty()) {
  110.  
  111. cout << "Group " << currGroup << " consists of: " << endl;
  112.  
  113. while((names.size() != 0) && (names.back().group == currGroup)) {
  114.  
  115. cout << names.back().firstName << endl;
  116. names.pop_back();
  117. }
  118.  
  119. cout << endl;
  120.  
  121. currGroup++;
  122. }
  123.  
  124. system("pause");
  125.  
  126. return 0;
  127. }
  128.  
  129. bool compareByGroup(const person &a, const person &b)
  130. {
  131. return (a.group > b.group);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement