Advertisement
Guest User

Untitled

a guest
Nov 5th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. //---------------------------------------------------
  2. // Purpose: A simple menu-based program for processing
  3. // an array of integers in many different ways.
  4. // Each array operation is implemented in a
  5. // separate function and called in the main program.
  6. //
  7. // Author: Heather R
  8. //---------------------------------------------------
  9.  
  10. #include <cstdlib>
  11. #include <iostream>
  12. #include <cmath>
  13. using namespace std;
  14.  
  15.  
  16. //function prototypes
  17.  
  18. void randomize(int array[], const int size, const int range);
  19. void print(int array[], const int size);
  20. int get_command();
  21. int find_min(int data[], const int size);
  22. int find_max(int data[], const int size);
  23.  
  24. int num_prime(int data[], const int size);
  25.  
  26. int find_sum(int data[], const int size);
  27. int find_sum_squared(int data[], const int size);
  28. int num_even(int data[], const int size);
  29. int num_odd(int data[], const int size);
  30.  
  31. int search(int data[], const int size);
  32. bool is_sorted(int data[], const int size);
  33. int sort(int data[], const int size);
  34.  
  35.  
  36. bool is_sorted(int data[], const int size)
  37. {
  38. // check if array is in sorted order
  39. bool sorted = true;
  40. for (int i=1; i<size; i++)
  41. {
  42. if (data[i-1] > data[i])
  43. sorted = false;
  44. }
  45. return sorted;
  46. }
  47.  
  48.  
  49. int find_sum_squared(int data[], const int size)
  50. {
  51. int squared_sum = 0;
  52.  
  53. for (int index = 0; index <size; index++)
  54. {
  55. squared_sum += ( data[index]* data[index]);
  56.  
  57. }
  58.  
  59. return squared_sum;
  60.  
  61. }
  62.  
  63.  
  64. //------------------------------------
  65.  
  66. int num_even(int data[], const int size)
  67. {
  68. int num_even = 0;
  69.  
  70. for (int index = 0; index < size; index++)
  71. {
  72. if (data[index] % 2 == 0)
  73. num_even +=1;
  74. }
  75.  
  76. return num_even;
  77. }
  78.  
  79.  
  80. //------------------------------------
  81.  
  82. int find_max(int data[], const int size)
  83. {
  84. int location = 0;
  85. // Find max value
  86. int max = data[0];
  87. for (int index = 0; index < size; index++)
  88. {
  89. if (data[index] > max)
  90. {max = data[index];
  91. location = index;}
  92. }
  93.  
  94. return location;
  95.  
  96. }
  97.  
  98. //---------------------------------------------------
  99. int find_min(int data[], const int size)
  100. {
  101. int location = 0;
  102. int min = data[0];
  103. for (int index = 0; index < size; index++)
  104. {
  105. if (data[index] < min)
  106. {min = data[index];
  107. location = index;}
  108. }
  109.  
  110. return location;
  111.  
  112. }
  113. //---------------------------------------------------
  114.  
  115. int num_prime(int data[], const int size)
  116. {
  117.  
  118. //Declare variable for the amount of values that are not prime
  119. int not_prime = 0;
  120. int Num_Prime = 0;
  121. bool Prime = true;
  122.  
  123. for (int index = 0; index < size; index++)
  124.  
  125. {
  126. if (data[index] == 1)
  127. {Prime = false;
  128. not_prime++;}
  129. if ((data[index] > 2) && (data[index] % 2 == 0))
  130. {Prime = false;
  131. not_prime++;}
  132. if ((data[index] > 3) && (data[index] % 3 == 0))
  133. {Prime = false;
  134. not_prime++;}
  135. if ((data[index] > 5) && (data[index] % 5 == 0))
  136. {Prime = false;
  137. not_prime++;}
  138. if ((data[index] > 7) && (data[index] % 7 == 0))
  139. {Prime = false;
  140. not_prime++;}
  141.  
  142. Num_Prime = (size - not_prime);
  143.  
  144. }
  145.  
  146. return Num_Prime;
  147. }
  148.  
  149.  
  150. //---------------------------------------------------
  151.  
  152. int Search(int data[], int size)
  153. {
  154.  
  155. int i = 0;
  156. int location = 0;
  157.  
  158. int desired = 0;
  159. cout << "Enter the value you'd like to check ";
  160. cin >>desired;
  161.  
  162. // Search array using divide and conquer
  163.  
  164.  
  165. bool found = false;
  166. for (int i=0; i<size; i++)
  167. {
  168. if (data[i] == desired)
  169. {found = true;
  170. location = i;
  171. break;}
  172. else
  173. i= -1;
  174. }
  175.  
  176. return i;
  177. }
  178. //---------------------------------------------------
  179.  
  180. int sort(int data[], const int size)
  181. {
  182.  
  183. int Index, Pos, SmallPos, Val, SmallVal;
  184.  
  185.  
  186. for (Index = 0; Index < size; Index++)
  187. {
  188. // Find smallest value in unsorted part
  189. SmallPos = Index;
  190. for (Pos = Index; Pos < size; Pos++)
  191. if (data[Pos] < data[SmallPos])
  192. SmallPos = Pos;
  193.  
  194. // Swap value to end of sorted part
  195. SmallVal = data[SmallPos];
  196. data[SmallPos] = data[Index];
  197. data[Index] = SmallVal;
  198. cout << data[Index] << endl;
  199. }
  200.  
  201. }
  202. //---------------------------------------------------
  203. void randomize(int array[], const int size, const int range)
  204. {
  205. for (int index = 0; index < size; index++)
  206. array[index] = random() % range;
  207. }
  208.  
  209. //---------------------------------------------------
  210. void print(int array[], const int size)
  211. {
  212. for (int index = 0; index < size; index++)
  213. cout << array[index] << " ";
  214. cout << endl;
  215. }
  216.  
  217. int find_sum(int array[], const int size)
  218. {
  219. int sum = 0;
  220.  
  221. for (int index = 0; index <size; index++)
  222. { sum += array[index];
  223. }
  224.  
  225. return sum;
  226. }
  227.  
  228. //---------------------------------------------------
  229. int get_command()
  230. {
  231. cout << "\nWelcome to Array Processing Program (APP 1.0).\n";
  232. cout << " Your command options are:\n";
  233. cout << " 0 - quit the program.\n";
  234. cout << " 1 - print the array.\n";
  235. cout << " 2 - randomize the array.\n";
  236. cout << " 3 - find the location of minimum value in array.\n";
  237. cout << " 4 - find the location of maximum value in array.\n";
  238. cout << " 5 - find sum of all values in array.\n";
  239. cout << " 6 - find sum of squares of all values in array.\n";
  240. cout << " 7 - count number of even numbers in array.\n";
  241. cout << " 8 - count number of odd numbers in array.\n";
  242. cout << " 9 - count number of prime numbers in array.\n";
  243. cout << " 10 - search the array for a specified value.\n";
  244. cout << " 11 - check to see if the array is sorted in ascending order.\n";
  245. cout << " 12 - sort the array in ascending order using selection sort.\n";
  246. cout << "\nEnter command: ";
  247. int command = 0;
  248. cin >> command;
  249. return command;
  250. }
  251.  
  252. //---------------------------------------------------
  253. int main()
  254. {
  255. // Declare local variables
  256. const int DATA_SIZE = 20;
  257. int data[DATA_SIZE] = {0};
  258. int range = 0;
  259. int command = -1;
  260. int min = 0;
  261. int max = 0;
  262. int sum = 0;
  263. int sum2 = 0;
  264. int count = 0;
  265. int location = -1;
  266. bool sorted = false;
  267.  
  268. // Loop processing user commands
  269. while (command != 0)
  270. {
  271. command = get_command();
  272. switch (command)
  273. {
  274. case 0:
  275. cout << "Have a nice day.\n";
  276. break;
  277. case 1:
  278. print(data, DATA_SIZE);
  279. break;
  280. case 2:
  281. cout << "Enter desired range: ";
  282. cin >> range;
  283. randomize(data, DATA_SIZE, range);
  284. cout << "Array has been randomized.\n";
  285. break;
  286. case 3:
  287. min = find_min(data, DATA_SIZE);
  288. cout << "The location of the Minimum value = " << min << endl;
  289. break;
  290. case 4:
  291. max = find_max(data, DATA_SIZE);
  292. cout << "The location of the Maximum value = " << max << endl;
  293. break;
  294. case 5:
  295. sum = find_sum(data, DATA_SIZE);
  296. cout << "Sum of values = " << sum << endl;
  297. break;
  298. case 6:
  299. sum2 = find_sum_squared(data, DATA_SIZE);
  300. cout << "Sum of values squared = " << sum2 << endl;
  301. break;
  302. case 7:
  303. count = num_even(data, DATA_SIZE);
  304. cout << "Count of even numbers = " << count << endl;
  305. break;
  306. case 8:
  307. count = DATA_SIZE - num_even(data, DATA_SIZE);
  308. cout << "Count of odd numbers = " << count << endl;
  309. break;
  310. case 9:
  311. count = num_prime(data, DATA_SIZE);
  312. cout << "Count of prime numbers = " << count << endl;
  313. break;
  314. case 10:
  315. location = Search(data, DATA_SIZE);
  316. if (location == -1)
  317. cout << "The value was not found in the array.\n";
  318. else
  319. cout << "The value was found at location " << location << ".\n";
  320. break;
  321. case 11:
  322. sorted = is_sorted(data, DATA_SIZE);
  323. if (sorted)
  324. cout << "The array IS in sorted order.\n";
  325. else
  326. cout << "The array is NOT in sorted order.\n";
  327. break;
  328. case 12:
  329. sort(data, DATA_SIZE);
  330. cout << "Array has been sorted.\n";
  331. break;
  332. default:
  333. cout << "Error, invalid command.\n";
  334. }
  335. }
  336. return 0;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement