Скачать презентацию Problem Solving and Program Design in C 5 Скачать презентацию Problem Solving and Program Design in C 5

9eeb46f2c791b4ea767b596d1c85ca44.ppt

  • Количество слайдов: 65

Problem Solving and Program Design in C (5 th Edition) by Jeri R. Hanly Problem Solving and Program Design in C (5 th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS 202 12 -10 -1429

# CHAPTER 3 - Functions 1. Introduction to Functions a) b) c) 2. Function # CHAPTER 3 - Functions 1. Introduction to Functions a) b) c) 2. Function Header Call Function Library Common Errors in Functions 1. 2. 3. 4. 5. 6. 7. Function (no. Argument, no. Return. Value) The area and the circumference of circle Function (no. Argument, 1 Return. Value) Function (1 Argument, no. Return. Value) Function (2 Arguments, no. Return. Value) Performs 3 Square Root Computations column shows the topics index. column shows the programs index. -2 -

1 Functions A. Introduction B. Break the big problem into small problems Each small 1 Functions A. Introduction B. Break the big problem into small problems Each small problem can be solved individually Write a function to solve each of these small problems Functions can be shared or reused many times Each function will have a separate memory, so the functions in one program do not share variables Subtopics Function Header Call Function Library -3 -

1 a Functions – Function Header A. Introduction Each function has to be declared 1 a Functions – Function Header A. 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 -4 -

1 a Functions – Function Header B 1. Syntax (declare the function with content) 1 a Functions – Function Header B 1. Syntax (declare the function with content) int, double, char, void return_value function_name(arguments) { function_body } int, double, char, void C 1. Example void welcome_msg (void) { printf(“Hello World”); } Function name: welcome_msg Arguments (inputs): void Return (output): void -5 -

1 a Functions – Function Header B 2. Syntax (declare the function without content) 1 a Functions – Function Header B 2. Syntax (declare the function without content) int, double, char, void return_value function_name(arguments); int, char, double, void C 2. Example void welcome_msg (void); : : void welcome_msg (void) { printf(“Hello World”); } 1. declaring a function indicates that the body of the function has to be written later 2. Declaring all the functions once makes the code readable -6 -

1 b Functions – Call Function A. Introduction B. Every place in the code 1 b Functions – Call Function A. Introduction B. 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 for functions without a return value function_name (arguments); for functions with a return value return_value = function_name (arguments); C. Example welcome_msg(); Kilo = mile_to_kilo(70); -7 -

1 c Functions – Function Library A. Introduction B. If a function is written 1 c Functions – Function Library A. Introduction B. If a function is written in a different file, you need to #include the file that has the function They are many libraries existing to help you write a program faster and better. (Review Table 3. 1 from the book) Syntax #include C. Example #include The file stdio. h has many functions, such as printf() and scanf() -8 -

1 Functions C. Conclusion 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 1 Functions C. Conclusion 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. #include int msg 1(int x); void msg 2(void) { printf("Hi. . . n"); } int main (void) { int ret. Val; msg 2(); ret. Val = msg 1(10); printf("%dn", ret. Val); return(0); } int msg 1(int x) return(x+5); } Library called stdio. h Declaring Function without content (1 argument & 1 return value) Declaring Function with content (No argument & no return value) Call function (in the existing file) Call function (in stdio. h file) { Content for the declared function -9 -

P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) We need a program that can draw 2 types of houses under each other as shown in the following picture Look at the similarities between the two types and write one function for this part and 2 other functions for the others Hints: The big house consists of a triangle and a square The small house consists of a triangle and a rectangle -10 When you asked to use function, you may need to look at the similarities

P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) Input Output Display the following shapes: / / *------* | | *------* triangle square triangle we need to build three functions to draw the two houses rectangle Formula We have to use functions, so look at the similarities -11 -

P 1 Problem Design Analysis Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Design Analysis Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) 1 void draw_triangle (void) 1. Display the following triangle: 4 int main (void) 1. / / 2 void draw_square (void) 1. Display the following square: *------* | | *------* 3 2. Display a big house 1. 1 call draw_triangle() 1. 2 call draw_square() Display a small house 2. 1 call draw_triangle() 2. 2 call draw_rectangle() void draw_rectangle (void) 1. Display the following rectangle: *------* | | *------* The algorithm of each function has to stand alone. FORGET the others -12 -

P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. #include void draw_triangle(void) { /* 1. Display a triangle shape */ } void draw_square(void) { /* 2. Display a square shape */ } void draw_rectangle(void) { /* 3. Display a rectangle shape */ } int main(void) { /* 1. Display a big house */ /* 2. Display a small house */ return (0); } Keep main() at the end to avoid declaring the functions at the beginning -13 -

P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) 1. #include P 1 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. ▼ void draw_triangle(void) { /* 1. Display a triangle shape */ printf(" /\ n"); printf(" / \ n"); printf(“ / \ n"); } void draw_square(void) { /* 1. Display a square shape */ printf(" *------* n"); printf(" | | n"); printf(" *------* n"); } To display the symbol ‘’, you need to type it twice as shown in line #7 -14 -

P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) ▲ 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. void draw_rectangle(void) { /* 1. Display a rectangle shape */ printf(" *------* n"); printf(" | | n"); printf(" *------* n"); } int main(void) { /* 1. Display a big house */ draw_triangle(); draw_square(); /* 2. Display a small house */ draw_triangle(); draw_rectangle(); return (0); } Keep main() at the end to avoid declaring the functions at the beginning -15 -

P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. P 1 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, no. Return. Value) -16 -

P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles We need a program to help users calculate the area and the circumference of a circle The users should enter the value of the radius Note: π = 3. 14159 area = π × radius 2 circumference = 2π × radius Circumference Area The symbol π is called Pi, and Pi has a constant value, which is 3. 14159 -17 -

P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles Input Formula Pi = 3. 14159 radius Pi radius Area = Pi × radius Circumference = 2 × Pi × radius Area=Pi x radius 2 Cirum=2 x π x radius 2 Output Area Circumference Pi has a constant value, so it needs to be defined with the constants Area Circum -18 -

P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles Get the radius value radius 2. Calculate the area = Pi × radius 3. Calculate the circumference circum = 2 × Pi × radius 4. Display the area 5. Display the circumference circum 1. 1) 2) 3) 4) Pi is defined in step 1 and used in step 3 & 4 radius is defined in step 2 and used in step 3 & 4 area is defined in step 3 and used in step 5 circum is defined in step 4 and used in step 6 After getting a value of a variable, you can use it in equation or output -19 -

P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. #include #define Pi 3. 14159 int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference /* 1. Get the circle radius */ /* 2. Calculate the area */ /* 3. Calculate the circumference */ /* 4. Display the area */ /* 5. Display the circumference */ return(0); } -20 -

P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles P 2 Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #define Pi 3. 14159 int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* 2&3. Calculate the area & circumference */ area = Pi * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference */ printf("The area is %. 4 fn", area); printf("The circumference is %. 4 fn", circum); return(0); } -21 -

Problem Analysis Design Outline Implementation Testing Maintenance Test Case 2 Test Case 1 Area Problem Analysis Design Outline Implementation Testing Maintenance Test Case 2 Test Case 1 Area & Circumference of Circles Test Case 3 P 2 22

P Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles The P Problem Analysis Design Outline Implementation Testing Maintenance Area & Circumference of Circles The next three programs will use the idea of Program #2 and move parts of the codes into functions: Program #3 will move the INPUT into a new function (No Argument & One Return Value) Program #4 will move the FORMULA into a new function (One Argument & One Return Value) Program #5 will move the OUTPUT into a new function (One Argument & No Return Value) -23 -

P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. Value) 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) Build a function that gets the radius value from the user and returns the value to the caller; the headers of the functions is: double get_radius(void); -24 -

P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. Value) Input Formula Pi = 3. 14159 radius Pi radius Area = Pi × radius Circumference = 2 × Pi × radius Area=Pi x radius 2 Cirum=2 x π x radius 2 Output Area Circumference Pi has a constant value, so it needs to be defined with the constants Area Circum -25 -

P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. Value) 1 double get_radius (void) 1. 2. 2 Get the radius value radius Return (radius) int main (void) 1. 2. 3. 4. 5. Get the radius value 1. 1 radius = get_radius() Calculate the area = Pi × radius Calculate the circumference circum = 2 × Pi × radius Display the area Display the circumference circum The algorithm of each function has to stand alone. FORGET the others -26 -

P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #define Pi 3. 14159 double get_radius(void) { double radius; /* 1. Get the radius value */ return (radius); } int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference /* 1. Get the circle radius */ /* 2&3. Calculate the area & circumference */ /* 4&5. Display the area & circumference */ return(0); } -27 -

P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. ▼ #include #define Pi 3. 14159 double get_radius(void) { double radius; /* 1. Get the radius value */ printf("Enter radius> "); scanf("%lf", &radius); /* 2. Return (radius) */ return(radius); } int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference -28 -

P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. P 3 Problem Analysis Design Outline Implementation Testing Maintenance Function (no. Argument, 1 Return. Value) ▲ 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. } /* 1. Get the circle radius */ radius = get_radius(); /* 2&3. Calculate the area & circumference */ area = Pi * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference */ printf("The area is %. 4 fn", area); printf("The circumference is %. 4 fn", circum); return(0); -29 -

Problem Analysis Design Outline Implementation Testing Maintenance Test Case 2 Test Case 1 Function Problem Analysis Design Outline Implementation Testing Maintenance Test Case 2 Test Case 1 Function (no. Argument, 1 Return. Value) Test Case 3 P 3 30

P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. Value) 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) 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); -31 -

P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. Value) Input Formula Pi = 3. 14159 radius Pi radius Area = Pi × radius Circumference = 2 × Pi × radius Area=Pi x radius 2 Cirum=2 x π x radius 2 Output Area Circumference Pi has a constant value, so it needs to be defined with the constants Area Circum -32 -

P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. Value) 1 double circle_area (double radius) 1. 2. 2 or 1. Return (Pi × radius) double circle_circum (double radius) 1. 2. 3 Calculate the area = Pi × radius Return (area) Calculate the circumference circum = 2 × Pi × radius Return (circum) or 1. Return (2 × Pi × radius) int main (void) 1. 2. 3. 4. 5. Get the radius value radius Calculate the area = circle_area (radius) Calculate the circumference circum = circle_circum (radius) Display the area Display the circumference circum You can save Time & Space when you write the algorithms -33 -

P 4 Problem Analysis Design Outline Testing Implementation Maintenance Function (1 Argument, 1 Return. P 4 Problem Analysis Design Outline Testing Implementation Maintenance Function (1 Argument, 1 Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #define Pi 3. 14159 double circle_area(double radius) double area; /* 1. Calculate the area */ return (area); } 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 /* 1. Get the circle radius */ /* 2&3. Calculate the area & circumference */ /* 4&5. Display the area & circumference */ return(0); } -34 -

P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. ▼ #include #define Pi 3. 14159 double circle_area(double radius) { double result; /* 1. Calculate the area */ result = Pi * radius; return (result); 1 st way (step-by-step) : 1. Declare a new variable 2. Store the result in the new variable 3. Return the value of the new variable } double circle_circum(double radius) { 2 nd way (save space and time) : return (2 * Pi * radius); 1. return the result of the equation } -35 -

P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. P 4 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, 1 Return. Value) ▲ 19. int main(void) 20. { 21. double radius; // input – radius of a circle 22. double area; // output – area of a circle 23. double circum; // output – circumference 24. 25. /* 1. Get the circle radius */ 26. printf("Enter radius> "); 27. scanf("%lf", &radius); 28. 29. /* 2&3. Calculate the area & circumference */ 30. area = circle_area (radius); 31. circum = circle_circum (radius); 32. 33. /* 4&5. Display the area & circumference */ 34. printf("The area is %. 4 fn", area); 35. printf("The circumference is %. 4 fn", circum); 36. 37. return(0); 38. } -36 -

Problem Analysis Design Outline Implementation Testing Maintenance Test Case 2 Test Case 1 Function Problem Analysis Design Outline Implementation Testing Maintenance Test Case 2 Test Case 1 Function (1 Argument, 1 Return. Value) Test Case 3 P 4 37

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) 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); -38 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) Input Formula Pi = 3. 14159 radius Pi radius Area = Pi × radius Circumference = 2 × Pi × radius Area=Pi x radius 2 Cirum=2 x π x radius 2 Output Area Circumference Pi has a constant value, so it needs to be defined with the constants Area Circum -39 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) 1 void display_box (double value) 1. 2 Display the value of the argument inside a box value *-------* | value | *-------* int main (void) 1. 2. 3. 4. 5. Get the radius value radius Calculate the area = Pi × radius Calculate the circumference circum = 2 × Pi × radius Display the area in a box 4. 1 display_box (area) Display the circumference in a box 5. 1 display_box (circum) The algorithm of each function has to stand alone. FORGET the others -40 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) 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. #include #define Pi 3. 14159 void display_box(double value) { /* 1. Display the value of the argument inside a box */ } int main(void) { double radius; double area; double circum; /* /* /* 1. 2. 3. 4. 5. // input – radius of a circle // output – area of a circle // output – circumference Get the circle radius */ Calculate the area */ Calculate the circumference */ Display the area in a box */ Display the circumference in a box */ return(0); } -41 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. ▼ #include #define Pi 3. 14159 void display_box(double value) { /* 1. Display the value of the argument inside a box */ printf("*----*n"); printf("| %f |n", value); printf("*----*n"); } int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); -42 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) ▲ 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. } /* 2&3. Calculate the area & circumference */ area = Pi * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference in a box */ display_box(area); display_box (circum); return(0); -43 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) You need to change the display format… Wh. Y? Because it doesn’t look good -44 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. ▼ #include #define Pi 3. 14159 void display_box(double value) { /* 1. Display the value of the argument inside a box */ printf("*-----*n"); printf("| %7. 4 f |n", value); printf("*-----*n"); } int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); -45 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) ▲ 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. } /* 2&3. Calculate the area & circumference */ area = Pi * radius; circum = 2 * Pi * radius; /* 4. Display the area in a box */ printf("Area: n"); display_box(area); /* 5. Display the circumference in a box */ printf("Circumference: n"); display_box (circum); return(0); -46 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) -47 -

P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. P 5 Problem Analysis Design Outline Implementation Testing Maintenance Function (1 Argument, no. Return. Value) 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. #include #define Pi 3. 14159 Memory: main() void display_box(double value) { /* 1. Display the value of the argument. . . */ printf("*-----*n"); printf("| %7. 4 f |n", value); printf("*-----*n"); } int main(void) { double radius; double area; double circum; // input – radius of a circle // output – area of a circle // output – circumference /* 2&3. Calculate the area & circumference */ area = Pi * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference */ printf(“Area: n”); display_box(area); printf(“Circumference: n”); display_box (circum); } area circum (double) 5. 0 78. 53975 31. 4159 Memory: display_box() – call 1 value /* 1. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); return(0); radius (double) 78. 53975 Memory: display_box() – call 2 value (double) 31. 4159 Output: -48 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) We need a program that takes 2 numbers from a user The two numbers will be sent to a function called simple_cal() that calculates and displays the sum & different & multiplying & and dividing of the two numbers int simple_cal() void -49 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) Input Formula number 1 number 2 No need because they are simple operators Output the sum of the two numbers the different between the two numbers multiplying of two numbers dividing the two numbers -50 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) void simple_math (int x, int y) 1. 2. 3. 4. Display the sum of the two numbers x + y Display the different between the two numbers x – y Display multiplying the two numbers x * y Display dividing the two numbers x / y int main (void) 1. 2. 3. Get the first number from the user number 1 Get the second number from the user number 2 Calculate and display the results of the four simple operators 3. 1 simple_math (number 1, number 2) -51 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include void simple_math(int x, int y) { /* 1. Display the sum of the two numbers */ /* 2. Display the different between the two numbers */ /* 3. Display multiplying the two numbers */ /* 4. Display dividing the two numbers */ } int main(void) { int number 1, number 2; // input: the two numbers /* 1. Get the first number from the user */ /* 2. Get the second number from the user */ /* 3. Calculate & Display the results */ return (0); } -52 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include void simple_math(int x, int y) { /* 1&2&3&4. Display the +, -, *, / of the two numbers */ printf("%d + %d = %dn", x, y, x+y); printf("%d - %d = %dn", x, y, x-y); printf("%d * %d = %dn", x, y, x*y); printf("%d / %d = %fn", x, y, x/y); } int main(void) { int number 1, number 2; // input: the two numbers /* 1&2. Get the two numbers from the user */ printf("Enter Number 1 > "); scanf("%d", &number 1); printf("Enter Number 2 > "); scanf("%d", &number 2); /* 3. Calculate & Display the results */ simple_math(number 1, number 2); return (0); } -53 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) The division operator doesn’t work; you need to unify the data-types -54 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include void simple_math(int x, int y) { /* 1&2&3&4. Display the +, -, *, / of the two numbers */ printf("%d + %d = %dn", x, y, x+y); printf("%d - %d = %dn", x, y, x-y); printf("%d * %d = %dn", x, y, x*y); printf("%d / %d = %. 2 fn", x, y, (double)x/(double)y); } int main(void) { int number 1, number 2; // input: the two numbers /* 1&2. Get the two numbers from the user */ printf("Enter Number 1 > "); scanf("%d", &number 1); printf("Enter Number 2 > "); scanf("%d", &number 2); /* 3. Calculate & Display the results */ simple_math(number 1, number 2); return (0); } -55 -

P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. P 6 Problem Analysis Design Outline Implementation Testing Maintenance Function (2 Arguments, no. Return. Value) -56 -

P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations Write a program that finds the square root of two numbers and the square root of the sum of the two numbers Hint: there is a library called math. h has a function called sqrt() that receives a value and returns the square root of this value. The header of sqrt() is: double sqrt(double); If you are using an external function, you need to include the library -57 -

P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations Input Output number 1 number 2 the square root of number 1 the square root of number 2 the square root of (number 1 + number 2) Formula No need -58 -

P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations 1 include 2 int main (void) 1. 2. 3. 4. 5. 6. 7. 8. Get the first number from the user first Get the second number from the user second Calculate the square root of the first number 3. 1 first_sqrt = sqrt (first) Calculate the square root of the second number 4. 1 second_sqrt = sqrt (second) Calculate the square root of the (first+second) number 5. 1 sum_sqrt = sqrt (first + second) Display the square root of the 1 st number first_sqrt Display the square root of the 2 nd number second_sqrt Display the square root of the sum_sqrt You may merge the Formula & Output together to same Time & Space -59 -

P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. #include #include // definitions of printf(), scanf() // definitions of sqrt() int main(void) { double first, second, first_sqrt, second_sqrt, sum_sqrt; // // input - two data values output - square root of first output - square root of second output - square root of sum /* 1&2. Get the first & second numbers from the user */ /* 3&4&5. Calculate the square root of them */ /* 6&7&8. Display the result */ return (0); } -60 -

P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #include // definitions of printf(), scanf() // definitions of sqrt() int main(void) { double first, second; double first_sqrt, second_sqrt, sum_sqrt; // input // output /* 1&2. Get the first & second numbers from the user */ printf("Enter the first number> "); scanf("%lf", &first); printf("Enter the second number> "); scanf("%lf", &second); /* 3&4&5. Calculate the square root of them */ first_sqrt = sqrt(first); second_sqrt = sqrt(second); sum_sqrt = sqrt(first + second); /* 6&7&8. Display the result */ printf("The square root of 1 st number is %. 2 fn", first_sqrt); printf("The square root of 2 nd number is %. 2 fn", second_sqrt); printf("The square root of the sum is %. 2 fn", sum_sqrt); return (0); } -61 -

P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations P 7 Problem Analysis Design Outline Implementation Testing Maintenance Performs 3 Square Root Computations -62 -

2 Common Errors in Functions 5 If you placed the main function before the 2 Common Errors in Functions 5 If you placed the main function before the other functions, you need to declare each of the other functions before the main #include void math(int x) { printf("X = %dn", x); } int main(void) { math(5); } or #include // function declaration void math(int x); int main(void) { math(5); } void math(int x) { printf("X = %dn", x); } -63 -

hw Evaluation Homework 1. 2. Track the memory in Program 4 ? Your bank hw Evaluation Homework 1. 2. Track the memory in Program 4 ? Your bank requires you to do some currency conversions. Write a function that takes the dollars and convert it to riyals then print the result in the main function. (Note: 1 Dollars = 3. 75 Riyals) (Apply the Software Development Method to solve this problem) 3. Build a library called math_shapes. h that contains of the 2 functions described in Program 4: 1. circle_area() 2. circle_area() and re-write the Program 4 by including the new library and removing the 2 functions from the code? (Show the code of the two files and the output in ONE page only) DON’T forget to display your name and ID in the output -64 -

# CHAPTER 3 - Functions 1. Introduction to Functions a) b) c) 2. Function # CHAPTER 3 - Functions 1. Introduction to Functions a) b) c) 2. Function Header Call Function Library Common Errors in Functions 1. 2. 3. 4. 5. 6. 7. Function (no. Argument, no. Return. Value) The area and the circumference of circle Function (no. Argument, 1 Return. Value) Function (1 Argument, no. Return. Value) Function (2 Arguments, no. Return. Value) Performs 3 Square Root Computations column shows the topics index. column shows the programs index. -65 -