Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. /*
  2. Alex Mardikian
  3. CS 250
  4. 01 September 2011
  5.  
  6. Excercise 01 - Vector and Arrays
  7. */
  8.  
  9. #include <cstdlib> // for rand
  10. #include <ctime> // for rand
  11. #include <iostream>
  12. #include <vector>
  13. #include <Windows.h> // for sleep to prevent rand seed time from being the same
  14.  
  15. using namespace std;
  16.  
  17. void fillVector(vector<int>& v, int size);
  18. // fillVector - Either randomly fills a vector or has user input values
  19. // @param vector<int>& - The vector to fill
  20. // @param int - Predefined amount of elements to randomly fill vector with
  21.  
  22. void printVector(const vector<int>& v);
  23. // printVector - Prints the vector contents to the console
  24. // @param const vector<int>& - Vector to print to console
  25.  
  26. bool equals(const vector<int>& v1, const vector<int>& v2);
  27. // equals - Checks if two vectors are equal
  28. // @param const vector<int>& - The first vector to compare
  29. // @param const vector<int>& - The second vector to compare
  30. // @return bool - True if they are equal, else false
  31.  
  32. void fillArray(int *a, int size);
  33. // fillArray - Eitehr randomly fills an array or has user input values
  34. // @param int* - Pointer to the array to fill
  35. // @param int - Size of the array
  36.  
  37. void printArray(const int *a, int size);
  38. // printArray - Prints the array contents to the console
  39. // @param const int* - Pointer to the array to print
  40. // @int - size of the array to print
  41.  
  42. void replaceElement(int *a, int size);
  43. // replaceElement - Asks the user to input a value to replace the largest
  44. // value in the array with
  45. // @param int* - The array to replace the largest value of
  46. // @param int - Size of the array
  47.  
  48. int main( )
  49. {
  50. // Predefine size for array and vectors if using random fill
  51. cout << "Enter the size of the array (and random filled vectors): ";
  52. int size = 0;
  53. cin >> size;
  54. cout << endl;
  55.  
  56. // Create both vectors and array
  57. vector<int> v1, v2;
  58. int *a = new int[size];
  59.  
  60. // Print out to console the options for filling vectors and array
  61. cout << "Vector 1: ";
  62. fillVector(v1, size);
  63. cout << "Vector 2: ";
  64. fillVector(v2, size);
  65. cout << "Array: ";
  66. fillArray(a, size);
  67.  
  68. // Print the contents of both vectors to the console
  69. cout << "\nVector 1 contents: ";
  70. printVector(v1);
  71. cout << "Vector 2 contents: ";
  72. printVector(v2);
  73.  
  74. // Check if both vectors are equal and print to console
  75. if (equals(v1, v2))
  76. cout << "\nVector 1 and vector 2 are equal." << endl;
  77. else
  78. cout << "\nVector 1 and vector 2 are not equal." << endl;
  79.  
  80. // Print to console array contents
  81. cout << "\nArray contents: ";
  82. printArray(a, size);
  83.  
  84. // Have user input a new value to replace the largest value in the array with
  85. replaceElement(a, size);
  86.  
  87. // Print the the midified array to the console
  88. cout << "\nModified Array: ";
  89. printArray(a, size);
  90.  
  91. // Delete and set pointer to NULL
  92. delete [] a;
  93. a = NULL;
  94.  
  95. cout << endl;
  96. system("Pause");
  97. return 0;
  98. }
  99.  
  100. void fillVector(vector<int>& v, int size)
  101. {
  102. v.clear();
  103. // Got lazy with testing so having the option for random fill made the check if equals quick to test
  104. cout << "1 for manual fill, 2 for random fill: ";
  105. int option;
  106. cin >> option;
  107.  
  108. if (option == 1)
  109. {
  110. cout << "\nEnter integer values to fill the vector(-1 to exit): ";
  111. int value;
  112. cin >> value;
  113.  
  114. while (value != -1)
  115. {
  116. v.push_back(value);
  117. cin >> value;
  118. }
  119.  
  120. cout << endl;
  121. }
  122. else
  123. {
  124. Sleep(1000); // Fixes problem where the vecotrs or array would generate the same values based on time seed
  125. srand((unsigned)time(0));
  126.  
  127. for(int i = 0; i < size; i++)
  128. v.push_back((rand() % (10 * size)) + 1);
  129. }
  130. }
  131.  
  132. void printVector(const vector<int>& v)
  133. {
  134. for(unsigned int i = 0; i < v.size(); i++)
  135. cout << v[i] << " ";
  136.  
  137. cout << endl;
  138. }
  139.  
  140. bool equals(const vector<int>& v1, const vector<int>& v2)
  141. {
  142. return (v1==v2)?true:false;
  143. }
  144.  
  145. void fillArray(int *a, int size)
  146. {
  147. // Too lazy to enter values every time the program ran
  148. cout << "1 for manual fill, 2 for random fill: ";
  149. int option;
  150. cin >> option;
  151.  
  152. if (option == 1)
  153. {
  154. cout << "\nEnter integer values to fill the array(-1 to exit): ";
  155. int value;
  156. cin >> value;
  157.  
  158. int idx = 0;
  159. while (value != -1)
  160. {
  161. a[idx] = value;
  162. idx++;
  163. cin >> value;
  164. }
  165. }
  166. else
  167. {
  168. Sleep(1000); // Fixes problem where the vecotrs or array would generate the same values based on time seed
  169. srand((unsigned)time(0));
  170.  
  171. for(int i = 0; i < size; i++)
  172. a[i] = (rand() % (10 * size)) + 1;
  173. }
  174. }
  175.  
  176. void printArray(const int *a, int size)
  177. {
  178. for(int i = 0; i < size; i++)
  179. cout << a[i] << " ";
  180.  
  181. cout << endl;
  182. }
  183.  
  184. void replaceElement(int *a, int size)
  185. {
  186. cout << "\nEnter a value to replace the largest in the array: ";
  187. int newElement;
  188. cin >> newElement;
  189.  
  190. int idx = 0;
  191.  
  192. for(int i = 1; i < size; i++)
  193. if (a[idx] < a[i])
  194. idx = i;
  195.  
  196. a[idx] = newElement;
  197. }
Add Comment
Please, Sign In to add comment