Advertisement
M4ritimeSeeker

PASS Week 03/24: Arrays

Mar 22nd, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. Problem #1:
  2.  
  3. Write a program that creates an array of 5 integer values using a for loop, 1 through
  4. 5. The iterator can be used as the values to be stored. The program should
  5. then use a second for loop to copy those elements into a second array, but this time
  6. you should use a for loop to enter the values into the new array backwards. The
  7. program should then use a third for loop to print out the first array to screen on one
  8. line, then a fourth for loop to print out the second array. The output should
  9. resemble this:
  10.  
  11. A: 1 2 3 4 5
  12. B: 5 4 3 2 1
  13.  
  14.  
  15.  
  16.  
  17. /*** SKELETON CODE ***/
  18.  
  19. /*
  20. * This program demonstrates how to copy one array to
  21. * another array and how to make use of the iterator.
  22. * By: L. Snedden
  23. */
  24. #include <stdio.h>
  25.  
  26. int main(void)
  27. {
  28. int a[5] = {0};
  29. int b[5] = {0};
  30.  
  31. //FIRST LOOP GOES HERE (This one initializes the values)
  32.  
  33. //SECOND LOOP GOES HERE (This one copies)
  34.  
  35. //THIRD LOOP GOES HERE (This one prints the first array)
  36.  
  37. //FOURTH LOOP GOES HERE (This one prints the second array)
  38.  
  39. return 0;
  40. }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. Problem #2:
  66.  
  67. Write a function that reverses the elements of an array of integers so that the last
  68. element becomes the first, the second from last becomes second, and so forth. The
  69. function is to reverse the elements in place, i.e., WITHOUT USING A SECOND ARRAY. You
  70. can use a variable to hold an element temporarily. Try changing up your program to use an odd-sized
  71. array or an even-sized array to test it out!
  72.  
  73.  
  74.  
  75. /*** SKELETON CODE ***/
  76.  
  77. #include <stdio.h>
  78.  
  79. //Function Prototype
  80. void reverseArray(int[], int);
  81.  
  82. int main(void)
  83. {
  84. //local declarations
  85. int ary[10];
  86.  
  87.  
  88. /*** INITIALIZE ARRAY VALUES HERE (Using a Loop) ***/
  89.  
  90. printf("Before reversal:\n");
  91. //Loop to Print all values
  92. for(int i = 0; i < 10; i++) {
  93. printf("Element %d is %d\n", i, ary[i]);
  94. }
  95.  
  96. /*** CALL YOUR FUNCTION HERE ***/
  97.  
  98. printf("\nAfter reversal:\n");
  99. //Loop to Print values AFTER reversal
  100. for(int i = 0; i < 10; i++) {
  101. printf("Element %d is %d\n", i, ary[i]);
  102. }
  103.  
  104. return 0;
  105. }//end main
  106.  
  107. /* REVERSE ARRAY FUNCTION */
  108. void reverseArray(int x[], int size)
  109. {
  110. /*** LOOP GOES HERE ***/
  111.  
  112. return;
  113.  
  114. }//end function
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. Problem #3:
  141.  
  142. The International Standard Book Number, ISBN, is used to uniquely identify a book.
  143. It is made of 10 digits. Write a function that tests an ISBN to see if it is valid. For an
  144. ISBN number to be valid, the weighted sum of the 10 digits must be evenly divisible
  145. by 11.
  146.  
  147. To determine the weighted sum, the value of each position is multiplied by its
  148. relative position, starting from the right, and the sum of the product is determined.
  149. Since the weighted sum modulus 11 is 0, the ISBN number is valid. Test your
  150. function with examples, use Amazon to get valid book ISBNs if you want.
  151.  
  152. Example
  153. ISBN: 0-07-881809-5 (assume no dashes in your function)
  154.  
  155. Code Weight Value
  156. 0 10 0
  157. 0 9 0
  158. 7 8 56
  159. 8 7 56
  160. 8 6 48
  161. 1 5 5
  162. 8 4 32
  163. 0 3 0
  164. 9 2 18
  165. 5 1 5
  166.  
  167. Weighted Sum is 220
  168.  
  169. 220 % 11 = 0 (Valid ISBN)
  170.  
  171.  
  172.  
  173.  
  174. SAMPLE OUTPUT:
  175.  
  176. Enter in 10 numbers one at a time: 0
  177. Enter in 10 numbers one at a time: 0
  178. Enter in 10 numbers one at a time: 7
  179. Enter in 10 numbers one at a time: 8
  180. Enter in 10 numbers one at a time: 8
  181. Enter in 10 numbers one at a time: 1
  182. Enter in 10 numbers one at a time: 8
  183. Enter in 10 numbers one at a time: 0
  184. Enter in 10 numbers one at a time: 9
  185. Enter in 10 numbers one at a time: 5
  186. The weighted sum is 220
  187. The ISBN is valid
  188.  
  189.  
  190.  
  191. /*** SKELETON CODE ***/
  192.  
  193. /* This program is a test driver for the ISBN
  194. * validation function. This function tests the
  195. * weighted sum and value to determine if the
  196. * ISBN number is valid.
  197. */
  198. #include <stdio.h>
  199.  
  200. //Function prototype
  201. int isbnTest(int[]);
  202.  
  203. int main(void)
  204. {
  205. //local declarations
  206. int ary[10];
  207. int result;
  208.  
  209. //local statements
  210. //Loop to scan 10 values from user one at a time (NO DASHES)
  211. for(int i = 0; i < 10; i++)
  212. {
  213. printf("Enter in 10 numbers one at a time: ");
  214. scanf("%d", &ary[i]);
  215. }
  216.  
  217. /*** CALL FUNCTION AND CHECK VALIDITY HERE ***/
  218.  
  219. return 0;
  220. }//end main
  221.  
  222. //ISBN TEST FUNCTION
  223. int isbnTest(int ary[])
  224. {
  225. /*** WRITE FUNCTION BODY HERE (Variables, Loop, etc) ***/
  226.  
  227. }//end fuction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement