Advertisement
M4ritimeSeeker

PASS Week 3/10

Mar 8th, 2021 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. Problem #1
  2.  
  3. Write a program that simulates flipping a coin for 1000 times and report the
  4. number of heads and tails you get. Use a for loop to control the random number
  5. generation and two variables to store the heads vs tails counts. NOTE: use the
  6. random number generator.
  7.  
  8.  
  9.  
  10.  
  11. /*** SKELETON CODE ***/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16.  
  17. int main(void)
  18. {
  19. srand(time(NULL));
  20.  
  21.  
  22. /** LOCAL DECLARATIONS GO HERE (Need variables for heads and tails) ***/
  23.  
  24.  
  25. for(/* Put loop info here */){
  26.  
  27.  
  28. }
  29.  
  30.  
  31. //Print number of heads (Outside loop)
  32. //Print number of tails (Outside loop)
  33.  
  34. return 0;
  35. }//end main
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. Problem #2
  60.  
  61. https://www.mathsisfun.com/numbers/factorial.html
  62.  
  63. Write a program that will determine the factorial of a user supplied number using a
  64. for loop.
  65.  
  66.  
  67.  
  68. /*** SKELETON CODE ***/
  69.  
  70. /*
  71. * Compose a program using a for loop that will determine the
  72. * factorial of a user supplied number.
  73. * BY: Larry Snedden
  74. */
  75.  
  76. #include <stdio.h>
  77.  
  78. int main(void)
  79. {
  80. /** LOCAL DECLARATIONS GO HERE **/
  81.  
  82. //Local statements
  83. printf("Enter in a number for factorial: ");
  84. //SCANF STATEMENT GOES HERE
  85.  
  86. for(/* Put loop info here */){
  87.  
  88.  
  89. }
  90.  
  91. /** OUTPUT FACTORIAL RESULT HERE (Outside of Loop) **/
  92.  
  93. return 0;
  94. }//end main
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. Problem #3
  114.  
  115. Write a program that prints numbers in descending order. Using a while loop,
  116. prompt the user for the amount of numbers (integers) to print to screen up to 100.
  117. Print the numbers counting down from the number entered all the way down to the
  118. number 0, like a countdown. Every 10 numbers printed to screen, print a newline so
  119. that there is a maximum of 10 numbers per line. Your while loop should use a test
  120. condition to determine when to stop.
  121.  
  122.  
  123. Example Output:
  124.  
  125. Enter an integer between 1 and 100: 100
  126. 100 99 98 97 96 95 94 93 92 91
  127. 90 89 88 87 86 85 84 83 82 81
  128. 80 79 78 77 76 75 74 73 72 71
  129. 70 69 68 67 66 65 64 63 62 61
  130. 60 59 58 57 56 55 54 53 52 51
  131. 50 49 48 47 46 45 44 43 42 41
  132. 40 39 38 37 36 35 34 33 32 31
  133. 30 29 28 27 26 25 24 23 22 21
  134. 20 19 18 17 16 15 14 13 12 11
  135. 10 9 8 7 6 5 4 3 2 1
  136.  
  137.  
  138.  
  139. /** SKELETON CODE **/
  140.  
  141. /* Simple while loop that prints numbers 10 per line.
  142. Written by:
  143. Date:
  144. */
  145. #include <stdio.h>
  146. int main (void)
  147. {
  148. // Local Declarations
  149. int num;
  150. int lineCount = 0;
  151.  
  152. // Statements
  153. printf ("Enter an integer between 1 and 100: ");
  154. scanf ("%d", &num); // Initialization
  155.  
  156. // Test number value here to make sure it is valid (Use if-else to test if it is between 1 and 100)
  157.  
  158. /** WHILE LOOP GOES HERE **/
  159.  
  160. return 0;
  161. } // main
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. Problem #4
  182.  
  183. Write a program using a do while loop that reads positive integers from the user.
  184. The user should be prompted to enter positive integer values or a 0 to quit. Data is
  185. entered one integer at a time. Zero is used to signify the end of a user’s input. The
  186. program will print the minimum, maximum and average integer of user’s input.
  187. Also, recall that division on integer values may not work well if you have a fractional
  188. average. How might you use casting here? Remember that the zero is not a member
  189. of the list, don’t use it to determine the average. Test your program with the
  190. following data:
  191.  
  192. 24, 7, 31, 64, 57, 7, 63, 31, 15, 7, 2, 4, and 6.
  193.  
  194. Your min should be 2, max should be 64 and average should be 24.46 with this test
  195. data.
  196.  
  197.  
  198.  
  199. /** SKELETON CODE **/
  200.  
  201. /*
  202. * This program demonstrates how to use a do while loop with a
  203. * quit condition predicated on user input.
  204. * Also demonstrated is the casting operator and if selection.
  205. * By: L Snedden
  206. */
  207. #include <stdio.h>
  208.  
  209. int main(void)
  210. {
  211. // Local declarations
  212. int min = 1; // Min value
  213. int max = 1; // Max value
  214. int numIn = 0; // Input number
  215. int ttlCnt = 0; // Total number of inputs
  216. int ttlIn = 0; // Total sum of numbers
  217. double avg = 0.0; // Average
  218.  
  219. /** DO WHILE LOOP GOES HERE **/
  220.  
  221. avg = (double)ttlIn / (double)ttlCnt; // Calculate Average
  222. printf("Min: %d Max: %d Avg: %.2lf\n", min, max, avg); // Print output
  223.  
  224. return 0;
  225. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement