Advertisement
M4ritimeSeeker

PASS Week 10/20

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