M4ritimeSeeker

PASS Week 3/17

Mar 14th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. PASS Week 03/17
  2.  
  3.  
  4.  
  5. Problem #1 Review of Loops
  6.  
  7. The value of pi (π) can be calculated by the following formula:
  8.  
  9. Θ = ✓6 * (1/1^2) + (1/2^2) + (1/3^2) + ... + (1/limit^2)
  10.  
  11. Write a program that uses this formula to calculate pi. Each operation in the
  12. parentheses will be repeated to the number of the limit (n). Note, the program will
  13. prompt the user for the “limit” value. Test your program once a limit of 5 terms, 10
  14. terms and a limit of 20 terms. Print the results of each and observe how this
  15. algorithm gets more accurate depending on how many terms you use. USE A FUNCTION TO CALCULATE THE FORMULA.
  16.  
  17. Sample Output:
  18. Enter the limit of terms to compute Pi.
  19. 6
  20. Pi = 2.991376
  21.  
  22. More Sample Outout:
  23. Enter the limit of terms to compute Pi.
  24. 1000
  25. Pi = 3.140638
  26.  
  27.  
  28.  
  29. /*** SKELETON CODE ***/
  30.  
  31. /***********************************************
  32. * This program computes pi using repetition *
  33. * and the terms or limits method. *
  34. * By: Larry Snedden *
  35. **********************************************/
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39.  
  40. double compute_Pi(int);
  41.  
  42. int main(void)
  43. {
  44. int limit;
  45. double Pi;
  46.  
  47. printf("Enter the limit of terms to compute Pi.\n");
  48. scanf("%d", &limit);
  49.  
  50. Pi = {FUNCTION CALL HERE} ;
  51.  
  52. printf("Pi = %lf\n", Pi);
  53. return 0;
  54. }//end main
  55.  
  56.  
  57. /********************************************
  58. * This function takes an integer for number*
  59. * of terms to compute Pi and returns *
  60. * Pi as a double. *
  61. * *****************************************/
  62.  
  63. /*** FUNCTION GOES HERE (Put Loop inside function) ***/
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. Problem #2
  80.  
  81. Construct a program that uses two nested for loops to produce 10 lines of 10
  82. integers that shows the row and column position. The first loop represents the rows
  83. and the nested loop represents each column in a row. The first number is the row
  84. and the second is the column position.
  85. Your output should look something like this:
  86.  
  87. [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9]
  88. [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9]
  89. [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9]
  90. [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9]
  91. [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,7] [4,8] [4,9]
  92. [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9]
  93. [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9]
  94. [7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9]
  95. [8,0] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9]
  96. [9,0] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9]
  97.  
  98.  
  99. /*** SKELETON CODE ***/
  100.  
  101. #include <stdio.h>
  102.  
  103. #define ROWS 10
  104. #define COLS 10
  105.  
  106. int main(void)
  107. {
  108.  
  109. return 0;
  110. }//end main
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127. Problem #3
  128.  
  129. Write a program that uses nested loops to produce the following pattern. The outer
  130. loop will produce the lines and the inner loop the column values. Think about how
  131. the numbers are descending.
  132.  
  133. (No skeleton code provided, refer to your problem #2 solution for help).
  134.  
  135. Sample Output:
  136.  
  137. 1 2 3 4 5 6 7 8 9
  138. 1 2 3 4 5 6 7 8
  139. 1 2 3 4 5 6 7
  140. 1 2 3 4 5 6
  141. 1 2 3 4 5
  142. 1 2 3 4
  143. 1 2 3
  144. 1 2
  145. 1
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. Problem #4
  178. Compose a program with a function that is called recursively to
  179. count backward from the number 10, printing each iteration to
  180. screen. The output might look something like this:
  181.  
  182. T Minus: 10
  183. T Minus: 9
  184. T Minus: 8
  185. T Minus: 7
  186. T Minus: 6
  187. T Minus: 5
  188. T Minus: 4
  189. T Minus: 3
  190. T Minus: 2
  191. T Minus: 1
  192. T Minus: 0
  193. Blast off!!!
  194.  
  195.  
  196.  
  197. /*** SKELETON CODE ***/
  198.  
  199. #include <stdio.h>
  200.  
  201. void CountDown(int);
  202.  
  203. int main(void)
  204. {
  205. int countFrom = 10;
  206. CountDown(countFrom);
  207. return 0;
  208. }//end main
  209.  
  210. void CountDown(int nbrIn)
  211. {
  212. /*** DO RECURSION HERE ***/
  213.  
  214. return;
  215. }
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236. Recursion examples (NOT PRACTICE PROBLEMS)
  237.  
  238.  
  239. /**** MYSTERY EXAMPLE WITHOUT RECURSION ****/
  240.  
  241. /***********************************************
  242. *
  243. * By: Larry Snedden *
  244. **********************************************/
  245. #include <stdio.h>
  246. #include <stdlib.h>
  247. #include <math.h>
  248.  
  249. double mysteryFunction(int);
  250.  
  251. int main(void)
  252. {
  253. int x;
  254.  
  255. printf("Enter the number to compute.\n");
  256. scanf("%d", &x);
  257.  
  258. printf("The calculation of %d is %.2lf\n", x, mysteryFunction(x));
  259.  
  260. return 0;
  261. }//end main
  262.  
  263. /********************************************
  264. * What does this function do?*
  265. * *****************************************/
  266. double mysteryFunction(int n)
  267. {
  268. double a = 1;
  269. int i = 1;
  270.  
  271. for(;i <= n; ++i) {
  272. a *= i;
  273. }
  274.  
  275. return a;
  276. }//end function
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /**** MYSTERY EXAMPLE WITH RECURSION ****/
  286.  
  287. /*
  288. Modified by: L. Snedden
  289.  
  290. */
  291. #include <stdio.h>
  292. #include <stdbool.h>
  293. long mysteryFunction (long n);
  294.  
  295. int main (void)
  296. {
  297. //Local Statements
  298. int x;
  299.  
  300. printf("Enter the number to compute.\n");
  301. scanf("%d", &x);
  302.  
  303. printf("The calculation of %d is: %ld\n", x, mysteryFunction(x));
  304.  
  305. return 0;
  306. } // end main
  307.  
  308. long mysteryFunction (long n)
  309. {
  310. //Local Statements
  311. if (n == 0)
  312. return 1;
  313. else
  314. return (n * mysteryFunction(n - 1));
  315. } // end function
  316.  
  317. /* Results:
  318. The calculation of 5 is: 120
  319. */
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326. /**** EXAMPLE OF STACK OVERFLOW ****/
  327.  
  328. //IF YOU WANT OVERFLOW TO HAPPEN FASTER, DO IT ON OSPREY. REPL.IT WILL TAKE A WHILE TO OVERFLOW
  329.  
  330. /*
  331. * There are many ways to run out of stack and heap memory.
  332. * This is just one example of recursing until the process stack
  333. * overflows. Modern operating systems now can contain this and
  334. * avoid crashing the system.
  335. * By: Larry Snedden
  336. */
  337. #include <stdio.h>
  338.  
  339. int StackOverflow(int);
  340.  
  341. int main(void)
  342. {
  343. int n = StackOverflow(n);
  344. return 0;
  345. }//end main
  346.  
  347. int StackOverflow(int n)
  348. {
  349. printf("In the %d recursing function.\n", n);
  350. StackOverflow(n + 1);
  351. return 1;//never reduces never reaches a base case
  352. }
Add Comment
Please, Sign In to add comment