1 / 77

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman. CP 202 Chapter 3. 11-10-1429. CHAPTER 3 - Outline. #. Function ( noArgument , noReturnValue ) The area and the circumference of circle Function (1 Argument, noReturnValue )

sylvie
Télécharger la présentation

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving and Program Design in C (5th Edition)by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 11-10-1429

  2. CHAPTER 3 - Outline # • Function (noArgument, noReturnValue) • The area and the circumference of circle • Function (1 Argument, noReturnValue) • Function (1 Argument, 1 ReturnValue) • Function (noArgument, 1 ReturnValue) • Function (2 Arguments, noReturnValue) • Performs 3 Square Root Computations • Introduction to Functions • Function Header • Call Function • Function Library • Common Errors •  column shows the topics index.  column shows the programs index.

  3. Introduction to Functions 1 • Introduction: • Characteristics: • Break the big problem into small problems • Each small problem can be solved individually • Write a function to solve each of these small problems • Some codes to solve the small problems are the same between the problems, so you can share or reuse these functions again and again • Each function will have a separate memory, so the functions in one program do not share variables • Three terms to remember: • Function Header • Call Function • Function Library

  4. Functions – Function Header 2 • Introduction: • Each function has to be declared before using it • Two ways to declare functions: • Declare the function only: • similar to declare variables • some programmers like to declare a list of functions first in order to organize the code. Then, they put the body / content of each function later • Declare the function with the content: • similar to declare and initial variables • most of the programmers like to put the body of the function in the same time in order to save space and time • The name of the function should refer to its job

  5. Functions – Function Header 2 B1.Syntax (declare the function with the content): C1.Example: void welcome_msg (void) { printf(“Hello World”); } return_valuefunction_name(arguments) { function_body } int, double, char, void int, double, char, void Function name:welcome_msg Arguments (input): void Return (output): void • The name of the function should refer to its job

  6. Functions – Function Header 2 B2.Syntax (declare the function only): C2.Example: void welcome_msg (void); : void welcome_msg (void) { printf(“Hello World”); } return_valuefunction_name(arguments); int, double, char, void int, double, char, void Function name:welcome_msg Arguments (input): void Return (output): void Note: declaring a function only indicates that the body of the function has to be written later • Declaring functions helps organize the functions’ list

  7. Functions – Call Function 3 • Introduction: • Every place in the code you want to use a function, you need to call the function • The function may return a value (int, double, char) or not (void), so there are two ways to call a function • Syntax: • Example: • welcome_msg (); • draw_box (2, 3); • Kilo = mile_to_kilo (Mile); function_name(arguments); return_value= function_name(arguments); Function name:welcome_msg Arguments (input): void Return (output): void Function name:mile_to_kilo Arguments (input): double (Mile) Return (output): double (Kile)

  8. Functions – Function Library 4 • Introduction: • If a function is written in a different file, you need to #include the file that has the function • The existing functions will help you to write any program faster and better • Syntax: • Example: • #include <stdio.h> #include <file_name> The file stdio.h has many functions, such as printf() and scanf() • There are number of libraries existing. Review Table 3.1 from the book

  9. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 • Problem • We need a program that can draw 2 types of houses under each other • Look at the similarities between the two types, and write one function for this part and other functions for the others parts • Hints: • The big house consists of a triangle and a square • The small house consists of a triangle and a rectangle Type 1: “Big House” /\ / \ / \ *------* | | | | *------* Type 2: “Small House” /\ / \ / \ *------* | | *------*

  10. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 • Analysis • Input • Output • Display the following shapes: • Formula /\ / \ / \ *------* | | | | *------* /\ / \ / \ *------* | | *------*

  11. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 • Design (1st version) • Display the two houses under each other /\ / \ / \ *------* | | | | *------* /\ / \ / \ *------* | | *------* triangle so we can build three functions to draw the two houses square triangle rectangle • We have to use functions, so look for the similarities

  12. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 • Design (2nd version) • Build a function called draw_triangle()receive: void return: void 1.1 Display the following triangle: • Build a function called draw_square()receive: void return: void 2.1 Display the following square: /\ / \ / \ *------* | | | | *------* • exist () means a function not a variable

  13. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 • Design (2nd version) • Build a function called draw_rectangle()arguments: void return: void 3.1 Display the following rectangle: • Display a big house 4.1 Call draw_triangle() 4.2 Call draw_square() • Display a small house 5.1 Call draw_triangle() 5.2 Call draw_rectangle() *------* | | *------* • You can indicate send/receive variables when you call a function

  14. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. #include <stdio.h> // 1. Build a function called draw_triangle () void draw_triangle(void) { // 1.1 Display a triangle shape } // 2. Build a function called draw_square () void draw_square(void) { // 2.1 Display a square shape } // 3. Build a function called draw_rectangle () void draw_rectangle(void) { // 3.1 Display a rectangle shape } int main(void) { // 4. Display a big house // 4.1 call draw_triangle() // 4.2 call draw_square() // 5. Display a small house // 5.1 call draw_triangle() // 5.2 call draw_rectangle() return (0); } Hint:Put the main() function at the end. Otherwise, you need to do extra steps. You will see an example at the end.

  15. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. #include <stdio.h> // 1. Build a function called draw_triangle () void draw_triangle(void) { // 1.1 Display a triangle shape printf(" /\\ \n"); printf(" / \\ \n"); printf(" / \\ \n"); } // 2. Build a function called draw_square () void draw_square(void) { // 2.1 Display a square shape printf(" *------* \n"); printf(" | | \n"); printf(" | | \n"); printf(" *------* \n"); } // 3. Build a function called draw_rectangle () void draw_rectangle(void) { // 3.1 Display a rectangle shape printf(" *------* \n"); printf(" | | \n"); printf(" *------* \n"); } Note: Using \ in the printf()means you have a special symbol, such as \n. If you want to print the symbol \, you need to type it twice.

  16. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 int main(void) { // 4. Display a big house draw_triangle(); draw_square(); // 5. Display a small house draw_triangle(); draw_rectangle(); return (0); } 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.

  17. Function (noArgument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 1 int main(void) { // 4. Display a big house draw_triangle(); draw_square(); // 5. Display a small house draw_triangle(); draw_rectangle(); return (0); } 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.

  18. The Area and the Circumference of Circle Implementation Testing Maintenance Problem Analysis Design Outline 2 • Problem • We need a program to help the users calculate the area and the circumference for a circle after entering the radius of the circle. • Note: • π = 3.14159 • area = π × radius2 • circumference = 2π × radius Circumference Area

  19. The Area and the Circumference of Circle Implementation Testing Maintenance Problem Analysis Design Outline 2 • Analysis • Input • PI = 3.14159 • radius • Output • Area • Circumference • Formula • Area = PI × radius × radius • Circumference = 2 × PI × radius PIradius Area = PI x radius2 Area PIradius Circumference = 2 x PI x radius Circumference • PI has a value unlike radius PI is a constant and not a variable

  20. The Area and the Circumference of Circle Implementation Testing Maintenance Problem Analysis Design Outline 2 • Design • Define the constants • PI = 3.14159 • Get the circle radius  radius • Calculate the areaarea = PI ×radius ×radius • Calculate the circumferencecircum = 2 ×PI ×radius • Display the area  area • Display the circumference  circum Also, you can calculate the area then display the result immediately. Later, you calculate the circumference and display the result

  21. The Area and the Circumference of Circle Implementation Testing Maintenance Problem Analysis Design Outline 2 • Some programs prefer to indicate if a variable is input or output value

  22. The Area and the Circumference of Circle Implementation Testing Maintenance Problem Analysis Design Outline 2

  23. The Area and the Circumference of Circle Implementation Testing Maintenance Problem Analysis Design Outline 2

  24. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 • Problem • We need a program that helps the users calculate the area and the circumference for a circle after entering the radius of the circle (Similar to Program 2) • The output value of the area has to be inside a box as well as the circumference value. Build a function to draw a box around the value; the header of the function is: void display_box(double value); Area: *---------* | 78.5397 | *---------* Circumference: *---------* | 31.4159 | *---------*

  25. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 • Analysis(Similar to Program 2) • Input • PI = 3.14159 • radius • Output • Area • Circumference • Formula • Area = PI × radius × radius • Circumference = 2 × PI × radius • The input, output, and the formula of the program did not change

  26. display_box() double void Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 • Design (1st version) • Define the constants • Build a function called display_box() that displays the argument value in a boxarguments: double (value) return: void • Get the circle radius • Calculate the area • Calculate the circumference • Display the area in a box • Display the circumference in a box

  27. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 • Design (2nd version) • Define the constants: • PI = 3.14159 • Build a function called display_box() that displays the argument value in a boxarguments: double (value) return: void 2.1 Display the value of the argument inside a box  value *-------* | value | *-------* • Get the circle radius  radius • Calculate the area:area = PI ×radius ×radius • 1 variable in the display_box(), so 1 variable needs to be declared

  28. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 • Design (2nd version) • Calculate the circumference:circum = 2 ×PI ×radius • Display the area in a box 6.1Display “Area:” on the screen 6.2 Call display_box()send: area receive: - • Display the circumference in a box 7.1 Display “Circumference:” on the screen 7.2 Call display_box()send: circum receive: - • 3 variables in the main(), so 3 variables need to be declared

  29. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. #include <stdio.h> /* 1. Define the constants */ #define PI 3.14159 /* 2. a function displays the argument value in a box */ void display_box(double value) { /* 2.1 Display the value of the argument inside a box */ } int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 3. Get the circle radius */ /* 4. Calculate the area */ /* 5. Calculate the circumference */ /* 6. Display the area in a box */ /* 6.1 display “Area:” on the screen */ /* 6.2 call display_box() */ /* 7. Display the circumference in a box */ /* 7.1 display “Circumference:” on the screen */ /* 7.2 call display_box() */ return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31.

  30. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include <stdio.h> /* 1. Define the constants */ #define PI 3.14159 /* 2. a function displays the argument value in a box */ void display_box(double value) { /* 2.1 Display the value of the argument inside a box */ printf("*---------*\n"); printf("| %7.4f |\n“, value); printf("*---------*\n"); } int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 3. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius);

  31. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. /* 4. Calculate the area */ area = PI * radius * radius; /* 5. Calculate the circumference */ circum = 2 * PI * radius; /* 6. Display the area in a box */ /* 6.1 display “Area:” on the screen */ printf(“Area:\n”); /* 6.2 call display_box */ display_box(area); /* 7. Display the circumference in a box */ /* 7.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 7.2 call display_box */ display_box (circum); return(0); }

  32. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. /* 4. Calculate the area */ area = PI * radius * radius; /* 5. Calculate the circumference */ circum = 2 * PI * radius; /* 6. Display the area in a box */ /* 6.1 display “Area:” on the screen */ printf(“Area:\n”); /* 6.2 call display_box */ display_box(area); /* 7. Display the circumference in a box */ /* 7.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 7.2 call display_box */ display_box (circum); return(0); }

  33. Function (1 Argument,noReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. #include <stdio.h> /* 1. Define the constants */ #define PI 3.14159 /* 2. a function displays the argument value in a box */ void display_box(double value) { /* 2.1 Display the value of the argument inside a box */ printf("*---------*\n"); printf("| %7.4f |\n“, value); printf("*---------*\n"); } int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 3. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); /* 4. Calculate the area */ area = PI * radius * radius; /* 5. Calculate the circumference */ circum = 2 * PI * radius; /* 6. Display the area in a box */ /* 6.1 display “Area:” on the screen */ printf(“Area:\n”); /* 6.2 call display_box */ display_box(area); /* 7. Display the circumference in a box */ /* 7.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 7.2 call display_box */ display_box (circum); return(0); } Memory: main() Memory: display_box() – call 1 Memory: display_box() – call 2 Output:

  34. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Problem • We need a program to help the users calculate the area and the circumference for a circle after entering the radius of the circle (Similar to Program 2) • The output value of the area has to be inside a box as well as the circumference value. Build a function to draw a box around the value; the header of the function is: • void display_box(double value); (Similar to Program 3) • Build 2 functions to calculate the area and the circumference; the headers of the functions are: • double circle_area(double radius); • double circle_circum(double radius);

  35. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Analysis(Similar to Program 2) • Input • PI = 3.14159 • radius • Output • Area • Circumference • Formula • Area = PI × radius × radius • Circumference = 2 × PI × radius • The input, output, and the formula of the program did not change

  36. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (1st version) • Define the constants • Build a function called display_box() that displays the argument value in a boxarguments: double (value) return: void • Build a function called circle_area() that calculates the area of the circlearguments: double (radius) return: double (area) • Build a function called circle_circum() that calculates the circumference of the circlearguments: double (radius) return: double (circum)

  37. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (1st version) • Get the circle radius • Calculate the area • Calculate the circumference • Display the area in a box • Display the circumference in a box

  38. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (2nd version) • Define the constants: • PI = 3.14159 • Build a function called display_box() that displays the argument value in a boxarguments: double (value) return: void 2.1 Display the value of the argument inside a box  value *-------* | value | *-------* • Build a function called circle_area() that calculates the area of the circle arguments: double (radius) return: double (area) 3.1Calculate the area:area = PI ×radius ×radius • 1st way – Tradition way: (step-by-step) • define a new variable • store the result of the equation in the new variable • return the value of the new variable

  39. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (2nd version) • Build a function called circle_circum() that calculates the circumference of the circle arguments: double (radius) return: double (2 ×PI ×radius) • Get the circle radius  radius • Calculate the area 6.1 Call circle_area()send: radius receive: area • Calculate the circumference 7.1 Call circle_area()send: radius receive: circum • 2nd way: (save space and time) • return the result of the equation

  40. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 • Design (2nd version) • Display the area in a box 8.1 Display “Area:” on the screen 8.2 Call display_box()send: area receive: - • Display the circumference in a box 9.1 Display “Circumference:” on the screen 9.2 Calldisplay_box()send: circum receive: -

  41. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include <stdio.h> /* 1. Define the constants */ #define PI 3.14159 /* 2. a function displays the argument value in a box */ void display_box(double value) { /* 2.1 Display the value of the argument inside a box */ } /* 3. a function calculates the area of the circle */ double circle_area(double radius) { /* 3.1 Calculate the area */ } /* 4. a function calculates the circumference of the circle */ double circle_circum(double radius) { }

  42. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 intmain(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 5. Get the circle radius */ /* 6. Calculate the area */ /* 6.1 call circle_area() */ /* 7. Calculate the circumference */ /* 7.1 call circle_circum() */ /* 8. Display the area in a box */ /* 8.1 display “Area:” on the screen */ /* 8.2 call display_box() */ /* 9. Display the circumference in a box */ /* 9.1 display “Circumference:” on the screen */ /* 9.2 call display_box() */ return(0); } 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49.

  43. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. #include <stdio.h> /* 1. Define the constants */ #define PI 3.14159 /* 2. a function displays the argument value in a box */ void display_box(double value) { /* 2.1 Display the value of the argument inside a box */ printf("*---------*\n"); printf("| %7.4f |\n“, value); printf("*---------*\n"); } /* 3. a function calculates the area of the circle */ double circle_area(double radius) { double result; /* 3.1 Calculate the area */ result = PI * radius * radius; return (result); } /* 4. a function calculates the circumference of the circle */ double circle_circum(double radius) { return (2 * PI * radius); } • 1st way – Tradition way: (step-by-step) • define a new variable • store the result of the equation in the new variable • return the value of the new variable • 2nd way: (save space and time) • return the result of the equation

  44. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 5. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); /* 6. Calculate the area */ /* 6.1 call circle_area() */ area = circle_area(radius); /* 7. Calculate the circumference */ /* 7.1 call circle_circum() */ circum = circle_circum(radius); /* 8. Display the area in a box */ /* 8.1 display “Area:” on the screen */ printf(“Area:\n”); /* 8.2 call display_box */ display_box(area); /* 9. Display the circumference in a box */ /* 9.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 9.2 call display_box */ display_box (circum); return(0); }

  45. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 5. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); /* 6. Calculate the area */ /* 6.1 call circle_area() */ area = circle_area(radius); /* 7. Calculate the circumference */ /* 7.1 call circle_circum() */ circum = circle_circum(radius); /* 8. Display the area in a box */ /* 8.1 display “Area:” on the screen */ printf(“Area:\n”); /* 8.2 call display_box */ display_box(area); /* 9. Display the circumference in a box */ /* 9.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 9.2 call display_box */ display_box (circum); return(0); }

  46. Function (1 Argument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 4 Memory:main() # include <stdio.h> /* 1. Define the constants */ #define PI 3.14159 /* 2. a function displays the argument value in a box */ void display_box(double value) { /* 2.1 Display the value of the argument inside a box */ printf("*---------*\n"); printf("| %7.4f |\n“, value); printf("*---------*\n"); } /* 3. a function calculates the area of the circle */ double circle_area(double radius) { double result; /* 3.1 Calculate the area */ result = PI * radius * radius; return (result); } /* 4. a function calculates the circumference of the circle */ double circle_circum(double radius) { return (2 * PI * radius); } int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* 5. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); /* 6. Calculate the area */ /* 6.1 call circle_area() */ area = circle_area(radius); /* 7. Calculate the circumference */ /* 7.1 call circle_circum() */ circum = circle_circum(radius); /* 8. Display the area in a box */ /* 8.1 display “Area:” on the screen */ printf(“Area:\n”); /* 8.2 call display_box */ display_box(area); /* 9. Display the circumference in a box */ /* 9.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 9.2 call display_box */ display_box (circum); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. Memory: circle_area() Memory: circle_circum() Memory: display_result() – call 1 Output: Memory: display_result() – call 2

  47. Function (noArgument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 5 • Problem • We need a program to help the users calculate the area and the circumference for a circle after entering the radius of the circle (Similar to P2) • The output value of the area has to be inside a box as well as the circumference value. Build a function to draw a box around the value; the header of the function is: • void display_box(double value); (Similar to P3) • Build 2 functions to calculate the area and the circumference; the headers of the functions are: • double circle_area(double radius); • double circle_circum(double radius); (Similar to P4) • Build a function to get the radius’ value from the user: • double get_radius(void);

  48. Function (noArgument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 5 • Analysis(Similar to Program 2) • Input • PI = 3.14159 • radius • Output • Area • Circumference • Formula • Area = PI × radius × radius • Circumference = 2 × PI × radius • The input, output, and the formula of the program did not change

  49. Function (noArgument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 5 • Design (1st version) • Define the constants • Build a function called display_box() that displays the argument value in a boxarguments: double (value) return: void • Build a function called circle_area() that calculates the area of the circlearguments: double (radius) return: double (area) • Build a function called circle_circum() that calculates the circumference of the circlearguments: double (radius) return: double (circum)

  50. Function (noArgument,1 ReturnValue) Implementation Testing Maintenance Problem Analysis Design Outline 5 • Design (1st version) • Build a function called get_radius() that gets the value of the radius from the userarguments: void return: double (radius) • Get the circle radius • Calculate the area • Calculate the circumference • Display the area in a box • Display the circumference in a box

More Related