Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FLOW OF CONTROL/FUNCTIONS EXAMPLE PROBLEM
- Answer the questions in the comments of this code BEFORE running it.
- Run it afterward to check your results!
- #include <stdio.h>
- //What are these two below statements called?
- int myFunction1(void);
- int myFunction2(int);
- // What order do the functions execute in this program (Including main)?
- // When does each function begin and end when the program is executed? (Extension of prev question)
- int main(void){
- int x = 7;
- int y = 0;
- myFunction1(); //Computes something
- myFunction2(x); //Computes something
- y = myFunction1() + myFunction2(x); //What does y equal?
- //What type of function is printf?
- //What type are myFunction1 and myFunction2?
- printf("%d\n", y);
- return 0;
- }
- int myFunction1(void){
- return 5;
- }
- int myFunction2(int myVar){
- return myVar + 2;
- }
- POINTER QUESTIONS
- What 3 different values/information does a variable store upon declaration?
- (For example, what 3 pieces of information does the statement "int x = 10" store?)
- Answer the questions in the comments of this code.
- Run it afterward to check your results, try changing the statements/values and see what you get!
- #include <stdio.h> /* for printf() */
- int main(void)
- {
- int i;
- int *p = &i; //What value does variable *p store?
- printf("The address of i is %p.\n", (void*) p); //What will this print? (Remember what the %p formatter does)
- return 0;
- }
- What does this program do?
- (You don't have to know the variable values, just what the program will do)
- //Function Prototype
- void exchange(int* pa, int* pb);
- int main(void){
- int a, b;
- exchange(&a, &b);
- return 0;
- }
- /* Function: exchange */
- void exchange(int* pa, int* pb) {
- int c;
- c = *pa;
- *pa = *pb;
- *pb = c;
- }
- PRACTICE PROBLEMS
- Problem #1 multi‐part problem Pass By Value
- (THERE WILL BE 3 PROGRAMS TOTAL, THEY ALL DO THE SAME THING BUT IN DIFFERENT WAYS)
- A (First program). 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. Print out the area of the square with a minimum width of 8 and
- precision of 2 decimal places to the right.
- B (Second Program). 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 PROTOTYPE in the GLOBAL AREA of the source code. Copy or
- rewrite your processes in a function definition UNDER the main function (Not INSIDE 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 (Third Program). Modify the program in problem #1 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.
- D. Review and Discuss the solutions (Pastebin of Solutions will be provided after completion)
- Problem #2 (Read this problem more than once, the wording is confusing):
- Write a program that uses a void function that receives two integer variables and a pointer to a floating‐point
- variable (use type DOUBLE for this problem) and sends back the integer and decimal parts as
- integers using pointers up to the hundredth place.
- So if a user enters in 98.65 , the function should return 98 and 65. Hint use modulus and division.
- IN YOUR MAIN FUNCTION
- declare your two integer variables and prompt for the user to input a floating point number, then call the
- function described above and output the parts from main after the function returns them using
- pointers (I.e. Give the function values and let it run, THEN output the results in main).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement