Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PASS Week 03/17
- Problem #1 Review of Loops
- The value of pi (π) can be calculated by the following formula:
- Θ = ✓6 * (1/1^2) + (1/2^2) + (1/3^2) + ... + (1/limit^2)
- Write a program that uses this formula to calculate pi. Each operation in the
- parentheses will be repeated to the number of the limit (n). Note, the program will
- prompt the user for the “limit” value. Test your program once a limit of 5 terms, 10
- terms and a limit of 20 terms. Print the results of each and observe how this
- algorithm gets more accurate depending on how many terms you use. USE A FUNCTION TO CALCULATE THE FORMULA.
- Sample Output:
- Enter the limit of terms to compute Pi.
- 6
- Pi = 2.991376
- More Sample Outout:
- Enter the limit of terms to compute Pi.
- 1000
- Pi = 3.140638
- /*** SKELETON CODE ***/
- /***********************************************
- * This program computes pi using repetition *
- * and the terms or limits method. *
- * By: Larry Snedden *
- **********************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double compute_Pi(int);
- int main(void)
- {
- int limit;
- double Pi;
- printf("Enter the limit of terms to compute Pi.\n");
- scanf("%d", &limit);
- Pi = {FUNCTION CALL HERE} ;
- printf("Pi = %lf\n", Pi);
- return 0;
- }//end main
- /********************************************
- * This function takes an integer for number*
- * of terms to compute Pi and returns *
- * Pi as a double. *
- * *****************************************/
- /*** FUNCTION GOES HERE (Put Loop inside function) ***/
- Problem #2
- Construct a program that uses two nested for loops to produce 10 lines of 10
- integers that shows the row and column position. The first loop represents the rows
- and the nested loop represents each column in a row. The first number is the row
- and the second is the column position.
- Your output should look something like this:
- [0,0] [0,1] [0,2] [0,3] [0,4] [0,5] [0,6] [0,7] [0,8] [0,9]
- [1,0] [1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,7] [1,8] [1,9]
- [2,0] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9]
- [3,0] [3,1] [3,2] [3,3] [3,4] [3,5] [3,6] [3,7] [3,8] [3,9]
- [4,0] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,7] [4,8] [4,9]
- [5,0] [5,1] [5,2] [5,3] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9]
- [6,0] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9]
- [7,0] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9]
- [8,0] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9]
- [9,0] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9]
- /*** SKELETON CODE ***/
- #include <stdio.h>
- #define ROWS 10
- #define COLS 10
- int main(void)
- {
- return 0;
- }//end main
- Problem #3
- Write a program that uses nested loops to produce the following pattern. The outer
- loop will produce the lines and the inner loop the column values. Think about how
- the numbers are descending.
- (No skeleton code provided, refer to your problem #2 solution for help).
- Sample Output:
- 1 2 3 4 5 6 7 8 9
- 1 2 3 4 5 6 7 8
- 1 2 3 4 5 6 7
- 1 2 3 4 5 6
- 1 2 3 4 5
- 1 2 3 4
- 1 2 3
- 1 2
- 1
- Problem #4
- Compose a program with a function that is called recursively to
- count backward from the number 10, printing each iteration to
- screen. The output might look something like this:
- T Minus: 10
- T Minus: 9
- T Minus: 8
- T Minus: 7
- T Minus: 6
- T Minus: 5
- T Minus: 4
- T Minus: 3
- T Minus: 2
- T Minus: 1
- T Minus: 0
- Blast off!!!
- /*** SKELETON CODE ***/
- #include <stdio.h>
- void CountDown(int);
- int main(void)
- {
- int countFrom = 10;
- CountDown(countFrom);
- return 0;
- }//end main
- void CountDown(int nbrIn)
- {
- /*** DO RECURSION HERE ***/
- return;
- }
- Recursion examples (NOT PRACTICE PROBLEMS)
- /**** MYSTERY EXAMPLE WITHOUT RECURSION ****/
- /***********************************************
- *
- * By: Larry Snedden *
- **********************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- double mysteryFunction(int);
- int main(void)
- {
- int x;
- printf("Enter the number to compute.\n");
- scanf("%d", &x);
- printf("The calculation of %d is %.2lf\n", x, mysteryFunction(x));
- return 0;
- }//end main
- /********************************************
- * What does this function do?*
- * *****************************************/
- double mysteryFunction(int n)
- {
- double a = 1;
- int i = 1;
- for(;i <= n; ++i) {
- a *= i;
- }
- return a;
- }//end function
- /**** MYSTERY EXAMPLE WITH RECURSION ****/
- /*
- Modified by: L. Snedden
- */
- #include <stdio.h>
- #include <stdbool.h>
- long mysteryFunction (long n);
- int main (void)
- {
- //Local Statements
- int x;
- printf("Enter the number to compute.\n");
- scanf("%d", &x);
- printf("The calculation of %d is: %ld\n", x, mysteryFunction(x));
- return 0;
- } // end main
- long mysteryFunction (long n)
- {
- //Local Statements
- if (n == 0)
- return 1;
- else
- return (n * mysteryFunction(n - 1));
- } // end function
- /* Results:
- The calculation of 5 is: 120
- */
- /**** EXAMPLE OF STACK OVERFLOW ****/
- //IF YOU WANT OVERFLOW TO HAPPEN FASTER, DO IT ON OSPREY. REPL.IT WILL TAKE A WHILE TO OVERFLOW
- /*
- * There are many ways to run out of stack and heap memory.
- * This is just one example of recursing until the process stack
- * overflows. Modern operating systems now can contain this and
- * avoid crashing the system.
- * By: Larry Snedden
- */
- #include <stdio.h>
- int StackOverflow(int);
- int main(void)
- {
- int n = StackOverflow(n);
- return 0;
- }//end main
- int StackOverflow(int n)
- {
- printf("In the %d recursing function.\n", n);
- StackOverflow(n + 1);
- return 1;//never reduces never reaches a base case
- }
Add Comment
Please, Sign In to add comment