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

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

description

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 ) - PowerPoint PPT Presentation

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

Page 1: 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 202Chapter 3

11-10-1429

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

© F

CIT

@K

AU

CHAPTER 3 - Outline1. Introduction to Functions

2. Function Header

3. Call Function

4. Function Library

5. Common Errors

2

#

1. Function (noArgument, noReturnValue)

2. The area and the circumference of circle

3. Function (1 Argument, noReturnValue)

4. Function (1 Argument, 1 ReturnValue)

5. Function (noArgument, 1 ReturnValue)

6. Function (2 Arguments, noReturnValue)

7. Performs 3 Square Root Computations

column shows the topics index. column shows the programs index.

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

© F

CIT

@K

AU

Introduction to FunctionsA. 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

1

-3-

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

© F

CIT

@K

AU

Functions – Function HeaderA. Introduction:

Each function has to be declared before using it Two ways to declare functions:

1.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

2.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

2

-4-The name of the function should refer to its job

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

© F

CIT

@K

AU

B1.Syntax (declare the function with the content):

C1.Example:void welcome_msg (void)

{

printf(“Hello World”);

}

return_value function_name(arguments){

function_body}

Functions – Function Header 2

int, double,

char, void

int, double,char, void

Function name: welcome_msgArguments (input): voidReturn (output): void

-5-The name of the function should refer to its job

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

© F

CIT

@K

AU

B2.Syntax (declare the function only):

C2.Example:void welcome_msg (void);

:

void welcome_msg (void)

{

printf(“Hello World”);

}

return_value function_name(arguments);

Functions – Function Header 2

int, double,

char, void

int, double,

char, void

Function name: welcome_msgArguments (input): voidReturn (output): void

Declaring functions helps organize the functions’ list

Note: declaring a function only indicates that the body of the function has to be written later

-6-

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

© F

CIT

@K

AU

Functions – Call FunctionA. 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

B. Syntax:

C. Example: welcome_msg (); draw_box (2, 3); Kilo = mile_to_kilo (Mile);

3

function_name (arguments);

return_value = function_name (arguments);

-7-

Function name: mile_to_kiloArguments (input): double (Mile)Return (output): double (Kile)

Function name: welcome_msgArguments (input): voidReturn (output): void

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

© F

CIT

@K

AU

Functions – Function LibraryA. 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

B. Syntax:

C. Example: #include <stdio.h>

4

#include <file_name>

-8-

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

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)

A.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”

/\ / \ / \ *------* | | *------*

-9-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)

B.Analysis Input Output

Display the following shapes:

Formula

-10-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

/\ / \ / \ *------* | | | | *------* /\ / \ / \ *------* | | *------*

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)

C.Design (1st version)1. Display the two houses under each other

-11-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

/\ / \ / \ *------* | | | | *------* /\ / \ / \ *------* | | *------*

We have to use functions, so look for the similarities

triangle

square

triangle

rectangle

so we can build three functions to draw the two houses

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)

C.Design (2nd version)1. Build a function called draw_triangle()

receive: voidreturn: void

1.1 Display the following triangle:

2. Build a function called draw_square()receive: voidreturn: void

2.1 Display the following square:

-12-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

/\ / \ / \

*------* | | | | *------*

exist () means a function not a variable

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)

C.Design (2nd version)3. Build a function called draw_rectangle()

arguments: voidreturn: void

3.1 Display the following rectangle:

4. Display a big house4.1 Call draw_triangle() 4.2 Call draw_square()

5. Display a small house5.1 Call draw_triangle()5.2 Call draw_rectangle()

-13-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

*------* | | *------*

You can indicate send/receive variables when you call a function

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)#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.

-14-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

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.

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)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.

-15-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)30.31.32.33.34.35.36.37.38.39.40.41.42.

int main(void){ // 4. Display a big house draw_triangle(); draw_square();

// 5. Display a small house draw_triangle(); draw_rectangle();

return (0);}

-16-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,noReturnValue)

-17-

ImplementationProblem Analysis Design

1

Outline Testing Maintenance

30.31.32.33.34.35.36.37.38.39.40.41.42.

int main(void){ // 4. Display a big house draw_triangle(); draw_square();

// 5. Display a small house draw_triangle(); draw_rectangle();

return (0);}

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

© F

CIT

@K

AU

The Area and the Circumference of Circle

A.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

-18-

Circumference Area

ImplementationProblem Analysis Design

2

Outline Testing Maintenance

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

© F

CIT

@K

AU

The Area and the Circumference of Circle

B.Analysis Input

PI = 3.14159 radius

Output Area Circumference

Formula Area = PI × radius × radius Circumference = 2 × PI × radius

-19-

ImplementationProblem Analysis Design

2

Outline Testing Maintenance

Area = PI x radius2PIradius Area

Circumference = 2 x PI x radius CircumferencePI

radius

PI has a value unlike radius PI is a constant and not a variable

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

© F

CIT

@K

AU

The Area and the Circumference of Circle

C.Design1. Define the constants

PI = 3.14159

2. Get the circle radius radius

3. Calculate the areaarea = PI × radius × radius

4. Calculate the circumferencecircum = 2 × PI × radius

5. Display the area area

6. Display the circumference circum

-20-

ImplementationProblem Analysis Design

2

Outline Testing Maintenance

Also, you can calculate the area then display the

result immediately. Later, you calculate the circumference

and display the result

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

© F

CIT

@K

AU

The Area and the Circumference of Circle

-21-

ImplementationProblem Analysis Design

2

Outline Testing Maintenance

Some programs prefer to indicate if a variable is input or output value

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

© F

CIT

@K

AU

The Area and the Circumference of Circle

-22-

ImplementationProblem Analysis Design

2

Outline Testing Maintenance

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

© F

CIT

@K

AU

The Area and the Circumference of Circle

-23-

ImplementationProblem Analysis Design

2

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)A.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);

-24-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

Area:*---------*| 78.5397 |*---------*

Circumference:*---------*| 31.4159 |*---------*

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)B.Analysis (Similar to Program 2)

Input PI = 3.14159 radius

Output Area Circumference

Formula Area = PI × radius × radius Circumference = 2 × PI × radius

-25-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

The input, output, and the formula of the program did not change

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)C.Design (1st version)

1. Define the constants2. Build a function called display_box() that displays the

argument value in a boxarguments: double (value)return: void

3. Get the circle radius

4. Calculate the area

5. Calculate the circumference

6. Display the area in a box

7. Display the circumference in a box

-26-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

display_box()double

void

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)C.Design (2nd version)

1. Define the constants: PI = 3.14159

2. Build a function called display_box() that displays the argument value in a box

arguments: double (value)return: void

2.1 Display the value of the argument inside a box value *-------* | value | *-------*

3. Get the circle radius radius

4. Calculate the area:area = PI × radius × radius

-27-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

1 variable in the display_box(), so 1 variable needs to be declared

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)C.Design (2nd version)

5. Calculate the circumference:circum = 2 × PI × radius

6. Display the area in a box6.1 Display “Area:” on the screen6.2 Call display_box()

send: areareceive: -

7. Display the circumference in a box7.1 Display “Circumference:” on the screen7.2 Call display_box()

send: circumreceive: -

-28-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

3 variables in the main(), so 3 variables need to be declared

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)

-29-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

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 <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.32.33.34.35.

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)

-30-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

#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);

1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)

-31-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

/* 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);}

24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)

-32-

ImplementationProblem Analysis Design

3

Outline Testing Maintenance

/* 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);}

24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.

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

© F

CIT

@K

AU

Function (1 Argument,noReturnValue)

-33-

ImplementationProblem Analysis Design

3

Outline

#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);}

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.

radius

(double)

area

(double)

circum

(double)

5.0 78.53975 31.4159

Memory: main()

Output:

X

(double)

31.4159

Memory: display_box() – call 2

X

(double)

78.53975

Memory: display_box() – call 1

Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)A.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);

-34-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)B.Analysis (Similar to Program 2)

Input PI = 3.14159 radius

Output Area Circumference

Formula Area = PI × radius × radius Circumference = 2 × PI × radius

-35-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

The input, output, and the formula of the program did not change

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)C.Design (1st version)

1. Define the constants2. Build a function called display_box() that displays the

argument value in a boxarguments: double (value)return: void

3. Build a function called circle_area() that calculates the area of the circle

arguments: double (radius)return: double (area)

4. Build a function called circle_circum() that calculates the circumference of the circle

arguments: double (radius)return: double (circum)

-36-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)C.Design (1st version)

5. Get the circle radius

6. Calculate the area

7. Calculate the circumference

8. Display the area in a box

9. Display the circumference in a box

-37-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)C.Design (2nd version)

1. Define the constants: PI = 3.14159

2. Build a function called display_box() that displays the argument value in a box

arguments: double (value)return: void

2.1 Display the value of the argument inside a box value *-------* | value | *-------*

3. Build a function called circle_area() that calculates the area of the circle

arguments: double (radius)return: double (area)

3.1 Calculate the area: area = PI × radius × radius -38-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)C.Design (2nd version)

4. Build a function called circle_circum() that calculates the circumference of the circle

arguments: double (radius)return: double (2 × PI × radius)

5. Get the circle radius radius6. Calculate the area

6.1 Call circle_area()send: radiusreceive: area

7. Calculate the circumference7.1 Call circle_area()

send: radiusreceive: circum

-39-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

2nd way: (save space and time)• return the result of the equation

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)C.Design (2nd version)

8. Display the area in a box8.1 Display “Area:” on the screen8.2 Call display_box()

send: areareceive: -

9. Display the circumference in a box9.1 Display “Circumference:” on the screen

9.2 Call display_box()send: circumreceive: -

-40-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)

-41-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

#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){

}

1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)

-42-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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 */

/* 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.

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

© F

CIT

@K

AU

#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);}

Function (1 Argument,1 ReturnValue)

-43-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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.

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

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

© F

CIT

@K

AU

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);}

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.

Function (1 Argument,1 ReturnValue)

-44-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

© F

CIT

@K

AU

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);}

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.

Function (1 Argument,1 ReturnValue)

-45-

ImplementationProblem Analysis Design

4

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (1 Argument,1 ReturnValue)

-46-

ImplementationProblem Analysis Design

4

Outline

# 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.

radius

(double)

5.0

Memory: circle_circum()

rad

(double)

result

(double)

5.0 78.5397

Memory: circle_area()

X

(double)

31.4159

Memory: display_result() – call 2

X

(double)

78.5397

Memory: display_result() – call 1

radius

(double)

area

(double)

circum

(double)

5.0 78.5397 31.4159

Memory: main()

Output:

Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)A.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); -47-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)B.Analysis (Similar to Program 2)

Input PI = 3.14159 radius

Output Area Circumference

Formula Area = PI × radius × radius Circumference = 2 × PI × radius

-48-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

The input, output, and the formula of the program did not change

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)C.Design (1st version)

1. Define the constants2. Build a function called display_box() that displays the

argument value in a boxarguments: double (value)return: void

3. Build a function called circle_area() that calculates the area of the circle

arguments: double (radius)return: double (area)

4. Build a function called circle_circum() that calculates the circumference of the circle

arguments: double (radius)return: double (circum)

-49-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)C.Design (1st version)

5. Build a function called get_radius() that gets the value of the radius from the user

arguments: voidreturn: double (radius)

6. Get the circle radius

7. Calculate the area

8. Calculate the circumference

9. Display the area in a box

10.Display the circumference in a box

-50-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)C.Design (2nd version)

1. Define the constants: PI = 3.14159

2. Build a function called display_box() that displays the argument value in a box

arguments: double (value)return: void

2.1 Display the value of the argument inside a box value *-------* | value | *-------*

3. Build a function called circle_area() that calculates the area of the circle

arguments: double (radius)return: double (PI × radius × radius)

-51-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)C.Design (2nd version)

4. Build a function called circle_circum() that calculates the circumference of the circle

arguments: double (radius)return: double (2 × PI × radius)

5. Build a function called get_radius() that gets the value of the radius from the user

arguments: voidreturn: double (radius)

5.1 Get the circle radius radius6. Get the circle radius

6.1 Call get_radius()send: voidreceive: radius

7. Calculate the area7.1 Call circle_area()

send: radiusreceive: area

-52-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)C.Design (2nd version)

8. Calculate the circumference8.1 Call circle_area()

send: radiusreceive: circum

9. Display the area in a box9.1 Display “Area:” on the screen9.2 Call display_box()

send: areareceive: -

10.Display the circumference in a box10.1 Display “Circumference:” on the screen

10.2 Call display_box() send: circum receive: -

-53-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

# 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){

}

/* 4. a function calculates the circumference of the circle */double circle_circum(double radius){

}

/* 5. a function gets the value of the radius from the user */double circle_circum(void){ /* 5.1 Get the circle radius */}

Function (noArgument,1 ReturnValue)

-54-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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.

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

© F

CIT

@K

AU

int main(void){ double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */

/* 6. Get the circle radius */ /* 6.1 call get_radius() */

/* 7. Calculate the area */ /* 7.1 call circle_area() */

/* 8. Calculate the circumference */ /* 8.1 call circle_circum() */

/* 9. Display the area in a box */ /* 9.1 display “Area:” on the screen */

/* 9.2 call display_box() */

/* 10. Display the circumference in a box */ /* 10.1 display “Circumference:” on the screen */

/* 10.2 call display_box() */

return(0);}

Function (noArgument,1 ReturnValue)

-55-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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.

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

© F

CIT

@K

AU

#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){ return (PI * radius * radius);}

/* 4. a function calculates the circumference of the circle */double circle_circum(double radius){ return (2 * PI * radius);}

/* 5. a function gets the value of the radius from the user */double get_radius(void){ double radius; /* 5.1 Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); return (radius);}

Function (noArgument,1 ReturnValue)

-56-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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.

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

© F

CIT

@K

AU

int main(void){ double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */

/* 6. Get the circle radius */ /* 6.1 call get_radius() */ radius = get_radius();

/* 7. Calculate the area */ /* 7.1 call circle_area() */ area = circle_area(radius);

/* 8. Calculate the circumference */ /* 8.1 call circle_circum() */ circum = circle_circum(radius);

/* 9. Display the area in a box */ /* 9.1 display “Area:” on the screen */ printf(“Area:\n”); /* 9.2 call display_box */ display_box(area);

/* 10. Display the circumference in a box */ /* 10.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 10.2 call display_box */ display_box (circum); return(0);}

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.61.62.63.64.65.66.

Function (noArgument,1 ReturnValue)

-57-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (noArgument,1 ReturnValue)

-58-

ImplementationProblem Analysis Design

5

Outline Testing Maintenance

int main(void){ double radius; /* input – radius of a circle */ double area; /* output – area of a circle */ double circum; /* output – circumference */

/* 6. Get the circle radius */ /* 6.1 call get_radius() */ radius = get_radius();

/* 7. Calculate the area */ /* 7.1 call circle_area() */ area = circle_area(radius);

/* 8. Calculate the circumference */ /* 8.1 call circle_circum() */ circum = circle_circum(radius);

/* 9. Display the area in a box */ /* 9.1 display “Area:” on the screen */ printf(“Area:\n”); /* 9.2 call display_box */ display_box(area);

/* 10. Display the circumference in a box */ /* 10.1 display “Circumference:” on the screen */ printf(“Circumference:\n”); /* 10.2 call display_box */ display_box (circum); return(0);}

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.61.62.63.64.65.66.

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

© F

CIT

@K

AU

Function (2 Arguments,noReturnValue)

A.Problem We need a program that takes 2 numbers from a

user The two numbers will be sent to a function

called simple_cal() that displays (sum, different, multiplying, and dividing) of the two number

-59-

ImplementationProblem Analysis Design

6

Outline Testing Maintenance

simple_cal()int

voidint

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

© F

CIT

@K

AU

Function (2 Arguments,noReturnValue)

B.Analysis Input

number 1 number 2

Output the sum of the two numbers the different between the two numbers multiplying of two numbers dividing the two numbers

Formula No need because they are simple operators

-60-

ImplementationProblem Analysis Design

6

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (2 Arguments,noReturnValue)

C.Design1. Build a function called simple_cal() that displays the

results of the four simple operators between two numbersarguments: int (num1), int (num2)return: void

1.1 Display the sum of the two numbers num1 + num21.2 Display the different between the two numbers num1 - num21.3 Display multiplying the two numbers num1 * num21.4 Display dividing the two numbers num1 / num2

2. Get the first number from the user number1

3. Get the second number from the user number2

4. Display the results of the four simple operators4.1 Call display_box()

send: number1, number2receive: -

-61-

ImplementationProblem Analysis Design

6

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (2 Arguments,noReturnValue)1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.

#include <stdio.h>

/* 1. a function displays the results of the four simple operators */void simple_cal(int num1, int num2){

}

int main(void){ int number1, number2; /* input: the two numbers */

/* 2. Get the first number from the user */

/* 3. Get the second number from the user */

/* 4. Display the results of the four simple operators */

return (0);}

-62-

ImplementationProblem Analysis Design

6

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (2 Arguments,noReturnValue)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.

#include <stdio.h>

/* 1. a function displays the results of the four simple operators */void simple_cal(int num1, int num2){ printf("%d + %d = %d\n", num1, num2, num1+num2); printf("%d - %d = %d\n", num1, num2, num1-num2); printf("%d * %d = %d\n", num1, num2, num1*num2); printf("%d / %d = %d\n", num1, num2, num1/num2);}

int main(void){ int number1, number2; /* input: the two numbers */

/* 2. Get the first number from the user */ printf("Enter Number 1 > "); scanf("%d", &number1);

/* 3. Get the second number from the user */ printf("Enter Number 2 > "); scanf("%d", &number2);

/* 4. Display the results of the four simple operators */ simple_cal(number1, number2);

return (0);}

-63-

ImplementationProblem Analysis Design

6

Outline Testing Maintenance

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

© F

CIT

@K

AU

Function (2 Arguments,noReturnValue)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.

#include <stdio.h>

/* 1. a function displays the results of the four simple operators */void simple_cal(int num1, int num2){ printf("%d + %d = %d\n", num1, num2, num1+num2); printf("%d - %d = %d\n", num1, num2, num1-num2); printf("%d * %d = %d\n", num1, num2, num1*num2); printf("%d / %d = %d\n", num1, num2, num1/num2);}

int main(void){ int number1, number2; /* input: the two numbers */

/* 2. Get the first number from the user */ printf("Enter Number 1 > "); scanf("%d", &number1);

/* 3. Get the second number from the user */ printf("Enter Number 2 > "); scanf("%d", &number2);

/* 4. Display the results of the four simple operators */ simple_cal(number1, number2);

return (0);}

-64-

ImplementationProblem Analysis Design

6

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

A.Problem Write a program that finds the square root of two numbers

and the square root of the sum of the two numbers Note: there is a library called math.h has a

function called sqrt() that receives a value and return the square root of this value. The header of sqrt() is: double sqrt(double);

-65-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

If you are using an external function, you need to include the library

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

B.Analysis Input

number 1 number 2

Output the square root of number 1 the square root of number 2 the square root of (number 1 + number 2)

Formula No need

-66-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

C.Design (1st version)1. Get the 1st number from the user

2. Calculate the square root of the 1st number

4. Display the square root of the 1st number

5. Get the second number from the user

6. Calculate the square root of the 2nd number

7. Display the square root of the 2nd number

8. Calculate the square root of the sum of the two numbers

9. Display the square root of the sum

-67-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

C.Design (2nd version)1. Include the library math.h in order to use sqrt()

2. Get the 1st number from the user first

3. Calculate the square root of the 1st number3.1 Call sqrt()

send: firstreceive: first_sqrt

4. Display the square root of the 1st number first_sqrt

5. Get the second number from the user second

6. Calculate the square root of the 2nd number6.1 Call sqrt()

send: secondreceive: second_sqrt

7. Display the square root of the 2nd number second_sqrt-68-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

C.Design (2nd version)8. Calculate the square root of the sum of the two numbers

8.1 Call sqrt()send: first+secondreceive: sum_sqrt

9. Display the square root of the sum sum_sqrt

-69-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

-70-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

-71-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

-72-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

-73-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Performs 3 Square Root Computations

-74-

ImplementationProblem Analysis Design

7

Outline Testing Maintenance

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

© F

CIT

@K

AU

Functions – Common Errors If you placed the main function before the other

functions, you need to declare each of the other functions before the main

-75-

#include <stdio.h>

void simple_cal(int num1, int num2){}

int main(void){ simple_cal(number1, number2);}

#include <stdio.h>

void simple_cal(int num1, int num2); // function declaration

int main(void){ simple_cal(number1, number2);}

void simple_cal(int num1, int num2){}

or

5

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

© F

CIT

@K

AU

Homework1. Track the memory in Program 5 ?2. 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)

-76-

DON’T forget to display your name and ID in the output

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

© F

CIT

@K

AU

CHAPTER 3 - Outline1. Introduction to Functions

2. Function Header

3. Call Function

4. Function Library

5. Common Errors

77

#

1. Function (noArgument, noReturnValue)

2. The area and the circumference of circle

3. Function (1 Argument, noReturnValue)

4. Function (1 Argument, 1 ReturnValue)

5. Function (noArgument, 1 ReturnValue)

6. Function (2 Arguments, noReturnValue)

7. Performs 3 Square Root Computations

column shows the topics index. column shows the programs index.