This material is provided to the professors and students using Computer Science: A Structured Approach Using C, Second Edition. All reprints in any form must cite this copyright. Copyright(c) 2001 by Brooks/Cole A division of Thomson Learning Chapter 4 Functions Review Questions 1. a. True 2. a. True 3. a. True 4. b. False 5. b. False 6. c. Factoring 7. d. The function definition contains executable statements that perform the function’s task. 8. d. Title 9. a. Empty parameter lists are declared with the keyword void. 10. e. Local variable names can be the same as the function’s parameter names. 11. d. Indirection operator (*) 12. d. labs 13. d. (rand () % 21) + 30 14. a. Structure charts are a replacement for flowcharts. Exercises 15. This function either must be declared to return an integer or must not return the value of the local variable z. 16. The parameter y needs a type associated with it, for instance: Line 1: int fun (int x, int y) 17. Function sun is defined inside function fun. 18. In the parameter list, a comma is not allowed between the type and the name. Line 1: void fun (int x) 19. a. int sun (int x, int y); b. int sun (int x, int y); c. void sun (void); (single void parameter) d. void sun (int x, float y); 20. a. fun (); (return type not used in function call) b. fun (); (parameter type not used in function call) c. fun (x, y); (no types used in function call) d. OK (provided that function takes no parameters and returns nothing) 21. a. 9.5 b. 2.4 c. 3.4 d. 7.0 e. 7.0 22. a. 9.0 b. -3.0 c. -4.0 d. 10.0 e. -2.0 f. -3.0 23. a. 3.5, 3.5, 3.8, 3.2, 3.5 b. 3.5, 3.45, 3.76, 3.23, 3.46 c. 3.5, 3.45, 3.76, 3.234, 3.457 Page 4.1 24. 25. 26. 27. a. between 0 and 9 b. between 0 and 3 c. between 0 and 51 d. between 1 and 10 e. 1 or 2 f. between -5 and 46 –2 2 23 119 12599 30237 24079 See Figure 4.1. Output: 5 0 12 1 10 5 25 Figure 4.1 Solution for Chapter 4, Exercise 27. Problems 28. /* This function prints a name block Pre Nothing Post Name block printed */ void printName (void) { /* Statements */ printf ("\n\n"); printf ("******************************\n"); printf ("* *\n"); printf ("* Your Name Here *\n"); printf ("* *\n"); printf ("******************************\n"); printf ("\n\n"); } return ; /* printName */ Call : printName (); 29. /* This program generates a random number. The range is 1 through 6. Written by: Date: */ #include <stdio.h> #include <stdlib.h> #include <time.h> Page 4.2 int main (void) { /* Local Definitions */ int a; /* Statements */ srand (time (NULL)); /* range is 1 through 6 */ a = rand() % 6 + 1; printf ("\nThe random number is %d\n", a); } return 0; /* main */ 30. /* This program generates a random number in the range 1, 4, 7, 10, 13, 16. Written by: Date: */ #include <stdio.h> #include <stdlib.h> #include <time.h> int main (void) { /* Local Definitions */ int a; /* Statements */ srand (time (NULL)); a = (rand() % 6) * 3 + 1; printf ("\nThe random number is %d\n", a); 31. 32. return 0; /* main */ Pass by value means that a copy of the data is sent to the called function. The original data in the calling function can not be changed. b. Pass by reference means that an alias of the original data is sent to the function so that the called function has access to the actual data in the calling function. C does not use pass by reference. c. Pass by address means that the address of the data is sent to the called function so that the original data in the calling function may be accessed via the address. This is how C simulates call by reference. See Figure 4.2. } a. Figure 4.2 Solution for Chapter 4, Problem 32 Page 4.3 33. "A function should do only one thing" means that the number of objects it handles must be one. Object refers to anything that exists separately from other elements of the function. 34. * Sample of top-down development using stubs. Written by: Date: */ #include <stdio.h> /* Prototype Declarations */ int initialize (void); int process (void); int endOfJob (void); int main (void) { /* Statements */ printf ("\nBegin program \n\n"); initialize (); process (); endOfJob (); } return 0; /* main */ /* =============== initialize =================*/ int initialize (void) { /* Statements */ printf ("\nIn initialize: \n"); return 0; } /* initialize */ /* =================== process =================== */ int process (void) { /* Statements */ printf ("\nIn process: \n"); return 0; } /* process */ /* =================== endOfjob =================== */ int endOfJob (void) { /* Statements printf ("\nIn endOfJob: \n"); return 0; } /* endOfJob */ 35. /* This program converts inches into centimeters. Written by: Date: */ #include <stdio.h> /* Prototype Declarations */ Page 4.4 float convertInch (float inches); int main { /* Local float float (void) Definitions */ inches; centis; /* Statements */ printf ("Please enter number of inches: "); scanf ("%f", &inches); centis = convertInch (inches); } printf ("\f inches equals %f centimeters\n", inches, centis); return 0; /* main */ /* ============== convertInch ================== This function converts inches to centimeters. Pre inches is the number of inches Post number of centimeters returned */ float convertInch (float inches) { /* Statements */ return (inches * 2.54); } /* convertInch */ 36. /* This program uses three functions to read three integers, print them in the order entered, and print them in the reverse order. Written by: Date: */ #include <stdio.h> /* Prototype Declarations */ void getData (int *first, int *second, int *third); void printForward (int first, int second, int third); void printReversed (int first, int second, int third); int main (void) { /* Local Definitions */ int x; int y; int z; /* Statements */ Page 4.5 getData (&x, &y, &z); printForward ( x, y, z); printReversed ( x, y, z); } return 0; /* main */ /* =================== getData =================== Reads data from keyboard. Pre first, second, and third are addresses Post data read into parameter variables */ void getData (int *first, int *second, int *third) { /* Statements */ printf ("Enter three integers: "); scanf ("%d %d %d", first, second, third); return; } /* getData */ /* ================= printForward ==================*/ void printForward (int a, int b, int c) { /* Statements */ printf ("\nNumbers in order: %d, %d, and %d\n", a, b, c); return; } /* printForward */ /* ================== printReversed Print integers reversed. Pre a, b, and c contain data Post data printed */ void printReversed (int a, int b, { /* Statements */ printf ("\nNumbers reversed: %d, c, b, a); return; } /* printReversed */ ================ to be printed int c) %d, and %d\n", 37. /* This program calculates the sum, difference, product, quotient, and remainder of the two numbers read from the keyboard. Written by: Date: */ #include <stdio.h> /* Prototype Declarations */ int add (int a, int b); int subt (int a, int b); int mult (int a, int b); void divide (int a, int b, int *quot, int *rem); int main (void) Page 4.6 { /* Local Definitions */ int a; int b; int sum; int diff; int product; int quotient; int remainder; /* Statements */ /* Prompt user for input and get data */ printf ("\nPlease enter two integer numbers: "); /* Read numbers into a and b */ scanf ("%d %d", &a, &b); /* Make the calculations */ sum = add (a, b); diff = subt (a, b); product = mult (a, b); divide (a, b, &quotient, &remainder); printf printf printf printf } ("\n\n%d + %d = %d\n", a, b, sum); ("%d - %d = %d\n", a, b, diff); ("%d * %d = %d\n", a, b, product); ("%d / %d = %d with %d left over.\n", a, b, quotient, remainder); return 0; /* main */ /* =================== add ================== This function adds two integers & returns their sum. Pre Parameters a and b Post Returns (a + b) */ int add (int a, int b) { return (a + b); } /* add */ /* ==================== subt ===================== This function receives two numbers and returns their difference. Pre Parameters a and b Post Returns (a - b) */ int subt (int a, int b) { return (a - b); } /* subt */ /* ==================== Receives two numbers Pre Parameters Post Returns (a */ mult ===================== & returns their product. a and b * b) Page 4.7 int mult (int a, int b) { return (a * b); } /* mult */ /* ===================== divide ===================== This function receives two numbers and returns their quotient and modulus using pass by address. Pre Parameters a and b Post quotient stored in quot; modulus stored in rem */ void divide (int a, int b, int *quot, int *rem) { /* Statements */ *quot = a / b; *rem = a % b; } return; /* divide */ 38. /* This program adds the least significant three digits (hundreds, tens, and ones). Written by: Date: */ #include <stdio.h> /* Prototype Declarations */ int addThreeDigits (int); int firstDigit (int); int secondDigit (int); int thirdDigit (int); int main (void) { /* Local Definitions */ int number; int sum; /* Statements */ /* Prompt user for input and get data */ printf ("\Enter a number with at least 3 digits: "); /* Read number */ scanf ("%d", &number); /* Calculate the sum of three digits */ sum = addThreeDigits (number); printf ("\nThe sum of 3 digits is %d\n", sum); } return 0; /* main */ Page 4.8 /* ================== addThreeDigits ================== Adds three digits returned from each subfunction. Pre Given x Post Returns the sum of rightmost 3 digits */ int addThreeDigits (int x) { /* Local Definitions */ int result; /* Statements */ result = firstDigit (x) + secondDigit (x) + thirdDigit (x); return result; } /* addThreeDigits */ /* ==================== firstDigit ==================== Extract the least significant digit of an integer. Pre a contains an integer Post Returns least significant digit */ int firstDigit (int a) { /* Statements */ return (a % 10); } /* firstDigit */ /* ================ secondDigit =================== Extract the second least significant digit of an integer. Pre a contains an integer Post Returns digit in 10s position */ int secondDigit (int a) { /* Local Definitions */ int result; /* Statements */ result = (a / 10) % 10; return result; } /* secondDigit */ /* ================= thirdDigit ======================= Extract the third least significant digit of an integer. Pre a contains an integer Post Returns digit in 100s position */ int thirdDigit (int a) { /* Local Definitions */ int result; /* Statements */ Page 4.9 } result = (a / 100) % 10; return result; /* thirdDigit */ 39. /* ================= roundTwo ============== This function rounds a floating-point number. to two decimal places. Pre floating-point number Post the rounded number */ long double roundTwo (long double x) { /* Local Definitions */ long double result; long double temp1; long int temp2; /* Statements */ temp1 = x * 1000 + 5; temp2 = temp1 / 10; result = temp2 / 100.00; return result; } /* roundTwo */ 40. /* This program reads a floating-point number and prints the ceiling, floor, and rounded value. Written by: Date: */ #include <stdio.h> #include <math.h> #include <stdlib.h> /* Prototype Declarations */ long double roundTwo (long double); int main (void) { /* Local Definitions */ long double num; long double ceiled; long double floored; long double rounded; /* Statements */ printf ("Enter a number: "); scanf ("%Lf", &num); ceiled = ceil (num); floored = floor (num); rounded = roundTwo (num); printf ("ceiling of %11.6Lf is %11.6Lf\n", num, ceiled); printf ("floor of %11.6Lf is %11.6Lf\n", num, floored); printf ("round of %11.6Lf is %11.6Lf\n", num, rounded); Page 4.10 } return 0; /* main */ /* ================ roundTwo ================= This function rounds a floating-point number. to two decimal places. Pre floating-point number Post the rounded number */ long double roundTwo (long double x) { /* Local Definitions */ long double result; long double temp1; long int temp2; /* Statements */ temp1 = x * 1000 + 5; temp2 = temp1 / 10; result = temp2 / 100.00; } return result; /* roundTwo */ 41. /* ================= triangle =================== This function calculates the perimeter and area of a right triangle. Pre a & b are lengths for sides of the triangle peri & area contain address of answer fields Post peri contains perimeter of triangle area contains the area of triangle */ void triangle (int a, int b, float *peri, float *area) { /* Local Definitions */ float c; /* Statements */ *area = (a * b) * 0.5; c = pow (a, 2.0) + pow (b, 2.0); *peri = a + b + pow (c, 0.5); } return; /* triangle */ Page 4.11