M4ritimeSeeker

PASS Week 10/27

Oct 25th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 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 to create 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.  
  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.  
  97. /*** CALL YOUR FUNCTION HERE ***/
  98.  
  99.  
  100. //Loop to Print values AFTER reversal
  101. for(int i = 0; i < 10; i++) {
  102. printf("Element %d is %d\n", i, ary[i]);
  103. }
  104.  
  105. return 0;
  106. }//end main
  107.  
  108. /* REVERSE ARRAY FUNCTION */
  109. void reverseArray(int x[], int size)
  110. {
  111. /*** LOOP GOES HERE ***/
  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.
  151.  
  152. Example
  153. ISBN: 0-07-881809-5 (assume no dashes in your function)
  154.  
  155. Code Weight Weighted 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 220
  168.  
  169. 220 % 11 = 0 (Valid ISBN)
  170.  
  171.  
  172.  
  173. /*** SKELETON CODE ***/
  174.  
  175. /* This program is a test driver for the ISBN
  176. * validation function. This function tests the
  177. * weighted sum and value to determine if the
  178. * ISBN number is valid.
  179. */
  180. #include <stdio.h>
  181.  
  182. //Function prototype
  183. int isbnTest(int[]);
  184.  
  185. int main(void)
  186. {
  187. //local declarations
  188. int ary[10];
  189.  
  190. //local statements
  191. //Loop to scan 10 values from user one at a time (NO DASHES)
  192. for(int i = 0; i < 10; i++)
  193. {
  194. printf("Enter in 10 numbers one at a time: ");
  195. scanf("%d", &ary[i]);
  196. }
  197.  
  198. /*** CALL FUNCTION AND CHECK VALIDITY HERE ***/
  199.  
  200. return 0;
  201. }//end main
  202.  
  203. //ISBN TEST FUNCTION
  204. int isbnTest(int ary[])
  205. {
  206. /*** WRITE FUNCTION BODY HERE (Variables, Loop, etc) ***/
  207.  
  208. }//end fuction
Advertisement
Add Comment
Please, Sign In to add comment