M4ritimeSeeker

PASS Week 8: Math, Random Numbers

Mar 8th, 2022 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. Pastebin for KAHOOT quiz answers: https://pastebin.com/dWChiCgq
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. PROBLEM #1: Nested Loop Review
  16.  
  17. Construct a program that uses two nested for loops to produce 10 lines of 10
  18. integers that shows the row and column position. The first loop represents the rows
  19. and the nested loop represents each column in a row. The first number is the row
  20. and the second is the column position. Your output should look something like this:
  21.  
  22. [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9]
  23. [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9]
  24. [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9]
  25. [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9]
  26. [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,7] [4,8] [4,9]
  27. [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9]
  28. [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9]
  29. [7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9]
  30. [8,0] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9]
  31. [9,0] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9]
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. PROBLEM #2: Calculating Area
  55.  
  56. Write a program that prompts the user to enter in a value for the
  57. base of a square. Compute the area using the pow() function in the
  58. math.h library. Print out the area of the square with a minimum
  59. width of 8 and precision of 2 decimal places to the right.
  60.  
  61. NOTE TO Repl.it users... note the compiler commands in the Console window
  62. uses -lm for the math linker. When executing your code on the Osprey server,
  63. you will have to use -lm in your gcc command if you use math functions.
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. PROBLEM #2.5: Calculating Area of Circle
  77.  
  78. Similar to problem #2, write a program that calculates area. This time,
  79. prompt the user for the diameter of a circle, and calculate the area of
  80. the circle.
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. PROBLEM #3: Random Number Generator
  98.  
  99. Write a program that generates a random number and prints it to the screen.
  100. Run the program 2-3 times and observe the results.
  101. Now add the srand() call with the time(NULL) argument to add
  102. randomness to the numbers. YOU ONLY NEED TO CALL srand() ONCE in the program
  103. before the rand() calls.
  104.  
  105. (Not required to complete this problem) If you want to know more on WHY random numbers need a seed, here is a link:
  106. https://stats.stackexchange.com/questions/354373/what-exactly-is-a-seed-in-a-random-number-generator
  107. Fair warning, there is some HEAVY math involved.
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. PROBLEM #4: Random Number Generator Continued
  130.  
  131. Create a program that will generate a random set of numbers between 10 and
  132. 20 inclusively. Print the number(s) to the screen to confirm the range works.
  133.  
  134. HERE IS A GUIDE TO MAKING A RANGE:
  135. yourRandomNumber = rand() % (max_number + 1 - minimum_number) + minimum_number
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. PROBLEM #5: Coin Flipping and Probability
  160.  
  161. Write a program that simulates flipping a coin 1000 times
  162. and report the AMOUNT OF TIMES it hit heads or tails. Use a for
  163. loop to control the random number generation and two variables
  164. to store the heads vs tails counts.
  165.  
  166. Challenge: How could you cheat to make the heads have a 2/3 probability?
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
Advertisement
Add Comment
Please, Sign In to add comment