Problem Solving and Program Design in C 5

Problem Solving and Program Design in C (5 th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 ( )ﺍﻹﺻﺪﺍﺭ ﺍﻟﺜﺎﻧﻲ Slides By Dr. Daniyal Alghazzawi

CHAPTER 3 - Outline Introduction to functions Program 3: Functions without arguments nor results Program 4: Calculate the Area and the Circumference Program 5: Functions with single argument Program 6: Functions with single argument and a result Program 7: Functions with single result only Program 8: Functions with multiple arguments Function Prototype Existing Libraries

Introduction to Functions 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. 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. Building a group of functions called Library.

Introduction to Functions • For example, two programs to help American and British People to do the following functions: Convert the numbers to Arabic. Use kilometers for measurement. Use Saudi Riyal for currency. Problem #1 USA to SA argument int English_to_Arabic (int) double Miles_to_Kilo (double) Problem #2 UK to SA int English_to_Arabic (int) double Miles_to_Kilo (double) double Dollar_to_SR (double) function name double Euro_to_SR (double)

Program 3: Function w/o Arguments nor Results Problem: We need a program that can draw 2 types of houses: Type 1: “Big House” / / *------* | | *------* Type 2: “Small House” / / *------* | | *------*

Program 3: Function w/o Argument & Return value Analysis: We need to build 3 functions: The 1 st function is called draw_triangle() to draw: / / The 2 nd function is called draw_square() to draw: *------* | | *------* The 3 rd function is called draw_rectangle() to draw: *------* | | *------* To draw the “Big House”, call the 1 st 2 nd functions To draw the “Small House”, call the 1 st 3 rd functions

Program 3: Function w/o Argument & Return value #include <stdio. h> void draw_triangle(void) { printf(" /\ n"); printf(" / \ n"); } void draw_square(void) { printf(" *------* printf(" | | printf(" *------* } n"); void draw_rectangle(void) { printf(" *------* n"); printf(" | | n"); printf(" *------* n"); } int main(void) { // Draw the big house draw_triangle(); draw_square(); // Draw the small house draw_triangle(); draw_rectangle(); return (0); } Using in the code means you have a message, such as n. If you want to print the symbol , you need to type it twice. Hint: Put the main() function at the end. Otherwise, you need to do extra steps. You will see an example at the end.

Program 3: (OUTPUT) #include <stdio. h> void draw_triangle(void) { printf(" /\ n"); printf(" / \ n"); } void draw_square(void) { printf(" *------* printf(" | | printf(" *------* } n"); void draw_rectangle(void) { printf(" *------* n"); printf(" | | n"); printf(" *------* n"); } int main(void) { // Draw the big house draw_triangle(); draw_square(); // Draw the small house draw_triangle(); draw_rectangle(); return (0); }

Program 4: Calculate the Area and the Circumference 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance Calculate the Area and the Circumference for a circle. Area Circumference

Program 4: Calculate the Area and the Circumference 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 1. Problem Constant: π = PI = 3. 14159 2. Problem Input: circle’s radius 3. Problem Output: - Area - Circumference 4. Relevant Formula: - area = π × radius 2 - circumference = 2π × radius

Program 4: Calculate the Area and the Circumference 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance 1. Get the circle radius. 2. Calculate the area. (area = π × radius) 3. Calculate the circumference. (circumference = 2π × radius) 4. Display the area and the circumference.

Program 4: Calculate the Area and the Circumference 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance Outline 1.

Program 4: Calculate the Area and the Circumference

Program 4: Calculate the Area and the Circumference 1. 2. 3. 4. 5. 6. Problem Analysis Design / Algorithm Implementation Testing Maintenance

Program 5: (Program 4 cont. ) Function with 1 Argument Problem: We need to display the values of the Area and Circumference in Program 3 in a box. For example: Area: *-----* | 78. 5397 | *-----* Circumference: *-----* | 31. 4159 | *-----*

Program 5: Function with 1 Argument void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } Analysis: We need to build a function that takes the result as an argument and display it in a box. double display_result() Outline: Add the following function: void

Program 5: Function with 1 Argument void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } /* Display the result in a box */ printf(“Area: n”); display_result(area); printf(“Circumference: n”); display_result(circum);

Program 5: (OUTPUT) Memory: main() radius area circum (double) 5. 0 void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } 78. 5397 31. 4159 Memory: display_result() – call 1 X (double) 78. 5397 Memory: display_result() – call 2 X (double) 31. 4159 Output: /* Display the result in a box */ printf(“Area: n”); display_result(area); printf(“Circumference: n”); display_result(circum);

Program 6: (Program 5 cont. ) Function with Argument&Return Value Problem: We need to build a library (more than one function) focuses on Circles. A function to calculate the Area A function to calculate the Circumference Analysis: we need to build 2 functions that take the radius of a circle and return the area or the circumference: double circle_area() double circle_circum() double

Program 6: (Program 5 cont. ) Function with Argument&Return Value double circle_area(double radius) { double result; result = PI * radius; return (result); } Outline: We need to build 2 functions that take the radius of a circle and return the area or the circumference: double circle_circum(double radius) { return (2 * PI * radius); }

Memory: main() void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } double circle_area(double rad) { double result; result = PI * rad; return (result); } double circle_circum(double radius) { return (2 * PI * radius); } radius area circum (double) 5. 0 78. 5397 31. 4159 Memory: circle_area() rad result (double) 5. 0 78. 5397 Memory: circle_circum() radius (double) int main(void) 5. 0 Memory: display_result() ……… Output: /* Calculate the Area and the Circumference */ area = circle_area(radius); circum = circle_circum(radius); /* Display the result in a box */ printf(“Area: n”); display_result(area); printf(“Circumference: n”); display_result(circum);

void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } double circle_area(double rad) { double result; result = PI * rad; return (result); } double circle_circum(double radius) { return (2 * PI * radius); } int main(void) /* Calculate the Area and the Circumference */ area = circle_area(radius); circum = circle_circum(radius); /* Display the result in a box */ printf(“Area: n”); display_result(area); printf(“Circumference: n”); display_result(circum);

Program 7: (Program 6 cont. ) Function with Single Result Only double get_radius(void) { double radius; printf(“Enter radius> “); scanf(“%lf”, &radius); return (radius); } Problem: We need to get the value of the radius using a function. Analysis: we need to build a function that: Ask the user to enter the radius value Return the radius value to the main function void Outline: get_radius() double

void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } double circle_area(double rad) { return (PI * rad); } double circle_circum(double radius) { return (2 * PI * radius); } double get_radius(void) { double radius; printf(“Enter radius> “); scanf(“%lf”, &radius); return (radius); } int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* Get the circle radius */ radius = get_radius(); /* Calculate the Area and the Circumference */ area = circle_area(radius); circum = circle_circum(radius); /* Display the result in a box */ printf(“Area: n”); display_result(area); printf(“Circumference: n”); display_result(circum); return(0); }

void display_result(double x) { printf("*-----*n"); printf("| %7. 4 f |n“, x); printf("*-----*n"); } double circle_area(double rad) { return (PI * rad); } double circle_circum(double radius) { return (2 * PI * radius); } double get_radius(void) { double radius; printf(“Enter radius> “); scanf(“%lf”, &radius); return (radius); } int main(void) { double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */ /* Get the circle radius */ radius = get_radius(); /* Calculate the Area and the Circumference */ area = circle_area(radius); circum = circle_circum(radius); /* Display the result in a box */ printf(“Area: n”); display_result(area); printf(“Circumference: n”); display_result(circum); return(0); }

Program 8: Function with Multiple Arguments • Problem: We need to build an small program that can find (sum, different, multiply, and divide) of any two numbers. • Analysis: Program Input: 2 numbers. Program Output: Number 1 + Number 2 Number 1 - Number 2 Number 1 * Number 2 Number 1 / Number 2 int simple_cal() void

Program 8: Function with Multiple Arguments #include <stdio. h> void simple_cal(int num 1, int { printf("%d + %d = %dn", printf("%d - %d = %dn", printf("%d * %d = %dn", printf("%d / %d = %dn", } int main(void) { int num 1, num 2; num 2) num 1, num 2, num 1+num 2); num 1 -num 2); num 1*num 2); num 1/num 2); // input: the two numbers /* get the two numbers from the user */ printf("Enter Number 1 > "); scanf("%d", &num 1); printf("Enter Number 2 > "); scanf("%d", &num 2); /* call the function that do the simple caluculation */ simple_cal(num 1, num 2); return (0); }

Program 8: (OUTPUT) #include <stdio. h> void simple_cal(int num 1, int { printf("%d + %d = %dn", printf("%d - %d = %dn", printf("%d * %d = %dn", printf("%d / %d = %dn", } int main(void) { int num 1, num 2; num 2) num 1, num 2, num 1+num 2); num 1 -num 2); num 1*num 2); num 1/num 2); // input: the two numbers /* get the two numbers from the user */ printf("Enter Number 1 > "); scanf("%d", &num 1); printf("Enter Number 2 > "); scanf("%d", &num 2); /* call the function that do the simple caluculation */ simple_cal(num 1, num 2); return (0); }

Function Prototype Y = sqrt(x); function name return value - int - double - char - void argument(s) - int - double - char - void

Existing Libraries If a function is witten in a different file, you need to #include the file that has the function. For example: The function sqrt() written in the file math. h double sqrt() double There are number of libraries existing. (Review Table 3. 1 from the book) The existing functions will help you to write any program

Program 9: Performs 3 Square Root Computations

Program 9: (OUTPUT)

Program 9: (OUTPUT ERROR without <math. h>)

CHAPTER 3 - Conclusion Functions and Prototype Program 3: Functions without arguments nor results Program 5: Functions with single argument Program 6: Functions with single argument and a result Program 7: Functions with single result only Program 8: Functions with multiple arguments Existing Libraries
- Slides: 34