Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REVIEW OF FUNCTIONS
- Review the following code and understand the function, the flow of control and when it returns to the caller.
- /* This program demonstrates that one function can be
- called multiple times.
- Written by:
- Date:
- */
- #include <stdio.h>
- // Function Declarations
- void printOne (int x);
- int main (void)
- {
- // Local Declarations
- int a;
- // Statements
- a = 5; // First call
- printOne (a);
- a = 33;
- printOne (a); // Second call
- // Done. Return to operating system.
- return 0;
- } // End main
- /* =================== printOne ===================
- Print one integer value.
- Pre-call: x contains number to be printed
- Post-call: value in x printed
- */
- void printOne (int x)
- {
- // Statements
- printf("%d\n", x);
- return;
- } // End printOne
- Problem #1 A thru C
- A. Write a program that prompts the user to enter in a value for the base and side of a
- square. Create a variable to hold the area of the square. Compute the area using the
- pow() function in the math.h library. Print out the area of the square with a
- minimum width of 8 and precision of 2 decimal places to the right.
- B. Now compose a function to perform the calculation of the area of a square. First
- identify the data the function will need to take as parameters and then identify the
- value if any that would be returned from the function. Now create the function
- declaration in the global are of the source code. Copy or rewrite your processes in a
- function definition under the main function. Test your function by
- running the program. Does it work with floating point values? What changes if any
- do you need to make to take floating point parameters and return a floating point
- value?
- C. Modify the program in part #A by adding functions to prompt the user and to
- get the user input and to provide output to the user by taking arguments. Your main
- function should only contain functions calls and a return statement.
- NOTE: If you compile this on a Linux machine you must link the math library using the “‐lm”
- argument Example: gcc source.c ‐lm
- Problem #2
- Write a program that uses functions to compute the perimeter and area of a right triangle
- when given the length of the two sides (a and b). Hint: Use three functions, one for
- calculating c, a second for the area, and a third for the perimeter.
- c^2 = a^2 + b^2
- area = .5 * (a * b)
- perimeter = a + b + c
- RANDOM NUMBERS
- Problem #3
- Write a program that generates a random number using a call to the random number
- function and prints it to the screen. Call the random number functions several times in the program and see what happens!
- Run the program 2‐3 times and observe the results.
- Now add the srand() call with the time() argument to add randomness to the numbers. You
- ONLY need to call srand() ONCE in the program before the rand() calls.
- Problem #4
- Create a program that will generate a random set of numbers between 10 and 20 inclusively.
- Example:
- First, set the range: range = (max value - min value) + 1;
- Second, shift to the right: rand() % range + 1; //this starts the counting at 1 instead of zero.
- LOGICAL OPERATOR REVIEw
- Examine the following code by compiling it and running it.
- /* Demonstrate the results of logical operators.
- Written by:
- Date:
- */
- #include <stdio.h>
- #include <stdbool.h>
- int main (void)
- {
- // Local Declarations
- bool a = true;
- bool b = true;
- bool c = false;
- // Statements
- printf(" %2d AND %2d: %2d\n", a, b, a && b);
- printf(" %2d AND %2d: %2d\n", a, c, a && c);
- printf(" %2d AND %2d: %2d\n", c, a, c && a);
- printf(" %2d OR %2d: %2d\n", a, c, a || c);
- printf(" %2d OR %2d: %2d\n", c, a, c || a);
- printf(" %2d OR %2d: %2d\n", c, c, c || c);
- printf("NOT %2d AND NOT %2d: %2d\n", a, c, !a && !c);
- printf("NOT %2d AND %2d: %2d\n", a, c, !a && c);
- printf(" %2d AND NOT %2d: %2d\n", a, c, a && !c);
- return 0;
- } // End main
- /* Results:
- 1 AND 1: ?
- 1 AND 0: ?
- 0 AND 1: ?
- 1 OR 0: ?
- 0 OR 1: ?
- 0 OR 0: ?
- NOT 1 AND NOT 0: ?
- NOT 1 AND 0: ?
- 1 AND NOT 0: ?
- */
- Examine the following code by compiling it and running it.
- /* Demonstrates the results of relational operators.
- Written by:
- Date:
- */
- #include <stdio.h>
- int main (void)
- {
- // Local Declarations
- int a = 5;
- int b = -3;
- // Statements
- printf(" %2d < %2d is %2d\n", a, b, a < b);
- printf(" %2d == %2d is %2d\n", a, b, a == b);
- printf(" %2d != %2d is %2d\n", a, b, a != b);
- printf(" %2d > %2d is %2d\n", a, b, a > b);
- printf(" %2d <= %2d is %2d\n", a, b, a <= b);
- printf(" %2d >= %2d is %2d\n", a, b, a >= b);
- return 0;
- } // main
- /* Results:
- 5 < -3 is ?
- 5 == -3 is ?
- 5 != -3 is ?
- 5 > -3 is ?
- 5 <= -3 is ?
- 5 >= -3 is ?
- */
Add Comment
Please, Sign In to add comment