Advertisement
M4ritimeSeeker

PASS Week 3/31 Solutions

Mar 28th, 2021
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. PASS Week 3/31 Solutions (MY SOLUTIONS)
  2.  
  3. Problem #1
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. void loadArray(int[]);
  10. void printArray(int[]);
  11.  
  12. int main(void){
  13.  
  14. srand(time(NULL));
  15. int ary[100];
  16.  
  17. loadArray(ary);
  18. printArray(ary);
  19.  
  20. return 0;
  21. }
  22.  
  23. void loadArray(int ary[]){
  24.  
  25. for (int i = 0; i < 100; i++){
  26. ary[i] = rand() % 999 + 1;
  27. }
  28.  
  29. return;
  30. }
  31.  
  32. void printArray(int ary[]){
  33.  
  34. for (int i = 0; i < 100; i++){
  35. if (i % 10 == 0){
  36. printf("\n");
  37. }
  38. printf("%3d ", ary[i]);
  39. }
  40. printf("\n");
  41.  
  42. return;
  43. }
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. Problem #2
  63.  
  64. #include <stdio.h>
  65. #include <stdlib.h>
  66. #include <time.h>
  67.  
  68. void loadArray(int[]);
  69. void searchArray(int[]);
  70. void printArray(int[]);
  71.  
  72. int main(void){
  73.  
  74. srand(time(NULL));
  75. int ary[100];
  76.  
  77. loadArray(ary);
  78. searchArray(ary);
  79. //printArray(ary);
  80.  
  81. return 0;
  82. }
  83.  
  84. void loadArray(int ary[]){
  85.  
  86. for (int i = 0; i < 100; i++){
  87. ary[i] = rand() % 999 + 1;
  88. }
  89.  
  90. return;
  91. }
  92.  
  93. void searchArray(int ary[]){
  94.  
  95. int search = 0;
  96. int counter = 0;
  97.  
  98. printf("Enter a value to be searched for: ");
  99. scanf("%d", &search);
  100.  
  101. for (int i = 0; i < 100; i++){
  102. if (ary[i] == search){
  103. printf("Number %d found at index %d.\n", search, i);
  104. counter++;
  105. }
  106. }
  107.  
  108. if (counter == 0){
  109. printf("Number not found");
  110. }
  111.  
  112.  
  113. return;
  114. }
  115.  
  116. void printArray(int ary[]){
  117.  
  118. for (int i = 0; i < 100; i++){
  119. if (i % 10 == 0){
  120. printf("\n");
  121. }
  122. printf("%3d ", ary[i]);
  123. }
  124. printf("\n");
  125.  
  126. return;
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. Problem #3 (Actually Problem #4, changed it cause original #3 was too hard)
  150.  
  151. #include <stdio.h>
  152. #include <stdlib.h>
  153. #include <time.h>
  154.  
  155. void loadArray(int[]);
  156. void searchArray(int[]);
  157. void BubbleSort(int[],int);
  158. void printArray(int[]);
  159.  
  160. int main(void){
  161.  
  162. srand(time(NULL));
  163. int ary[100];
  164.  
  165. loadArray(ary);
  166. printArray(ary);
  167. BubbleSort(ary, 99);
  168. printArray(ary);
  169. //searchArray(ary);
  170.  
  171.  
  172. return 0;
  173. }
  174.  
  175. void loadArray(int ary[]){
  176.  
  177. for (int i = 0; i < 100; i++){
  178. ary[i] = rand() % 999 + 1;
  179. }
  180.  
  181. return;
  182. }
  183.  
  184. void searchArray(int ary[]){
  185.  
  186. int search = 0;
  187. int counter = 0;
  188.  
  189. printf("Enter a value to be searched for: ");
  190. scanf("%d", &search);
  191.  
  192. for (int i = 0; i < 100; i++){
  193. if (ary[i] == search){
  194. printf("Number %d found at index %d.\n", search, i);
  195. counter++;
  196. }
  197. }
  198.  
  199. if (counter == 0){
  200. printf("Number not found\n");
  201. }
  202.  
  203.  
  204. return;
  205. }
  206.  
  207. void BubbleSort(int aryIn[],int last)
  208. {
  209. int temp = 0;
  210. for(int current = 0; current < last; current++){
  211.  
  212. for(int walker = last; walker > current; walker--) {
  213.  
  214. if(aryIn[walker] < aryIn[walker -1]) {
  215. temp = aryIn[walker];
  216. aryIn[walker] = aryIn[walker - 1];
  217. aryIn[walker - 1] = temp;
  218.  
  219. }//end if
  220. }//end inner for
  221. }//end outer for
  222.  
  223. return;
  224. }//end bubbleSort
  225.  
  226. void printArray(int ary[]){
  227.  
  228. for (int i = 0; i < 100; i++){
  229. if (i % 10 == 0){
  230. printf("\n");
  231. }
  232. printf("%3d ", ary[i]);
  233. }
  234. printf("\n");
  235.  
  236. return;
  237. }
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265. Problem #4 (Actually Problem #5)
  266.  
  267. #include <stdio.h>
  268. #include <stdlib.h>
  269. #include <time.h>
  270.  
  271. void loadArray(int[]);
  272. void searchArray(int[]);
  273. void BubbleSort(int[],int);
  274. int BinarySearch(int[], int, int);
  275. void printArray(int[]);
  276.  
  277. int main(void){
  278.  
  279. srand(time(NULL));
  280. int ary[100];
  281. int userInput = 0;
  282.  
  283. loadArray(ary);
  284. printArray(ary);
  285. BubbleSort(ary, 99);
  286. printArray(ary);
  287.  
  288. printf("Enter a value to be searched for: ");
  289. scanf("%d", &userInput);
  290.  
  291. if (BinarySearch(ary, 99, userInput) != -1){
  292. printf("Number found at index %d\n", BinarySearch(ary,99,userInput));
  293. }
  294. else{
  295. printf("Number not found\n");
  296. }
  297.  
  298.  
  299. return 0;
  300. }
  301.  
  302. void loadArray(int ary[]){
  303.  
  304. for (int i = 0; i < 100; i++){
  305. ary[i] = rand() % 999 + 1;
  306. }
  307.  
  308. return;
  309. }
  310.  
  311. void searchArray(int ary[]){
  312.  
  313. int search = 0;
  314. int counter = 0;
  315.  
  316. printf("Enter a value to be searched for: ");
  317. scanf("%d", &search);
  318.  
  319. for (int i = 0; i < 100; i++){
  320. if (ary[i] == search){
  321. printf("Number %d found at index %d.\n", search, i);
  322. counter++;
  323. }
  324. }
  325.  
  326. if (counter == 0){
  327. printf("Number not found\n");
  328. }
  329.  
  330.  
  331. return;
  332. }
  333.  
  334. void BubbleSort(int aryIn[],int last)
  335. {
  336. int temp = 0;
  337. for(int current = 0; current < last; current++){
  338.  
  339. for(int walker = last; walker > current; walker--) {
  340.  
  341. if(aryIn[walker] < aryIn[walker -1]) {
  342. temp = aryIn[walker];
  343. aryIn[walker] = aryIn[walker - 1];
  344. aryIn[walker - 1] = temp;
  345.  
  346. }//end if
  347. }//end inner for
  348. }//end outer for
  349.  
  350. return;
  351. }//end bubbleSort
  352.  
  353. int BinarySearch(int list[], int end, int target) {
  354.  
  355. int location = 0, first = 0, mid, last = end;
  356. while(first <= last) {
  357. mid = (first + last) /2;//Find the middle
  358.  
  359. if(target > list[mid])
  360. first = mid + 1; //Look in the upper half of list
  361.  
  362. else if (target < list[mid])
  363. last = mid - 1; //Look in the lower half of list
  364.  
  365. else
  366. first = last + 1;//Found the item force quit
  367.  
  368. }//end while
  369.  
  370. if(list[mid] == target)//What if item is at index 0?
  371. return mid;
  372.  
  373. else
  374. return -1;
  375. }//end BinarySearch
  376.  
  377. void printArray(int ary[]){
  378.  
  379. for (int i = 0; i < 100; i++){
  380. if (i % 10 == 0){
  381. printf("\n");
  382. }
  383. printf("%3d ", ary[i]);
  384. }
  385. printf("\n");
  386.  
  387. return;
  388. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement