Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // fileIO
  4. //
  5. // Created by Diego Gonzalez on 2/24/17.
  6. // Copyright © 2017 diego. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <sstream>
  11. #include <vector>
  12. #include <string>
  13. #include <cstdlib>
  14. #include <fstream>
  15.  
  16. using namespace std;
  17.  
  18. /* Desc: Displays vector with no sorting
  19. * @param: list vector to be displayed
  20. */
  21. void displayUnsortedList( vector <int> vec){
  22.  
  23. for(int i = 0; i< vec.size(); i ++){
  24. if( (i) % 10 == 0 && i > 0){
  25. cout << "\n";
  26. }
  27.  
  28. cout << vec[i] << " ";
  29. }
  30.  
  31.  
  32. };
  33.  
  34. /* Desc: Displays freq of values
  35. * @param: list vector to be displayed
  36. */
  37. void displayFreq(vector<int> list){
  38. int val[100];
  39. for(int i = 0; i <= 100; i++){
  40. val[i] = 0;
  41. }
  42. for(int i = 0; i< list.size(); i ++){
  43. val[list[i]]+=1;
  44. }
  45.  
  46. for(int i = 1; i <= 100; i++){
  47. cout << i << " Frequency: " << val[i] << "\n";
  48. }
  49.  
  50. }
  51.  
  52. /* Dec: Sort and displays
  53. * @param: list vector to be displayed
  54. */
  55. vector<int> sortList(vector <int> list){
  56.  
  57. for(int i = 0; i < list.size()-1; i++){
  58. for(int b = 0; b < list.size()-1-i; b++ ){
  59. if(list[b] > list[b + 1]){
  60. //swap
  61. int temp = list[b];
  62.  
  63. list[b] = list[b+1];
  64. list[b+1] = temp;
  65. }
  66. }
  67. }
  68. return list;
  69. };
  70.  
  71. /* Displays menu
  72. *
  73. */
  74. void displayMenu(){
  75.  
  76. cout << "\n1) Display Unsorted List\n"
  77. << "2) Display Sorted List\n"
  78. << "3) Display Frequinces\n"
  79. << "4) Quit \n";
  80. }
  81.  
  82. /*
  83. * @returns integer input
  84. */
  85. int checkUserInput(){
  86.  
  87. int error = 0;
  88. int number;
  89. do{
  90. if(cin >> number ){
  91. error = 1;
  92. }else{
  93. cin.clear();
  94. cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  95. cout << "Bad Input try again :";
  96. }
  97.  
  98. }while(error == 0);
  99.  
  100. return number;
  101. };
  102.  
  103.  
  104. /*
  105. * Desc: get input within bounds
  106. * @param: upper upper bound
  107. * @param: lower lower bouned
  108. * @returns input within bounds
  109. */
  110. int checkIntInRange(int lower, int upper){
  111.  
  112. int num = checkUserInput();
  113. while(num < lower || num > upper){
  114. cout << "Bad Input: ";
  115. num = checkUserInput();
  116. }
  117.  
  118. return num;
  119. };
  120.  
  121.  
  122. int main(int argc, const char * argv[]) {
  123. fstream file;
  124. srand(time(NULL));
  125. int input;
  126. vector<int> values;
  127.  
  128. file.open("values.txt", ios::in);
  129.  
  130. if(file){
  131. while(file >> input){
  132. values.push_back(input);
  133. }
  134. file.close();
  135. }else{
  136. cout << "errror";
  137. }
  138.  
  139. int exit = 0;
  140. do{
  141. displayMenu();
  142.  
  143. int number = checkIntInRange(1,10);
  144. switch (number) {
  145. case 1:
  146. cout << "Unsorted list: \n";
  147. displayUnsortedList(values);
  148. break;
  149.  
  150. case 2:
  151. cout << "Sorted list: \n";
  152. displayUnsortedList(sortList(values));
  153. break;
  154.  
  155. case 3:
  156.  
  157. displayFreq(values );
  158. break;
  159.  
  160. case 4:
  161.  
  162. exit = 1;
  163. break;
  164.  
  165.  
  166.  
  167. }
  168.  
  169.  
  170. }while(exit == 0);
  171.  
  172.  
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement