Professional Documents
Culture Documents
Top-Down Design With Functions PDF
Top-Down Design With Functions PDF
CHAPTER 3
TOP-DOWN DESIGN WITH FUNCTIONS
OUTLINE
¡ Top-Down Design
¡ Introduction to Function
¡ Function Design
¡ Library Function
TOP-DOWN DESIGN
Big Problem
3
TOP-DOWN DESIGN
¡ Making a movie:
4
FUNCTIONS
5
ADVANTAGES OF USING FUNCTION SUBPROGRAMS
main()
¡ The empty parentheses after the function name indicate that this function requires
no arguments
¡ For example:
int main(void){
draw_circle(); // Draw a circle
draw_triangle(); // Draw a triangle
draw_intersect(); // Draw intersecting lines
return (0);
} Person
7
FUNCTION PROTOTYPES & DEFINITIONS
¡ Arguments
¡ Parameters
¡ Return parameter
argument 9
EXAMPLE
11
CREATING VOID FUNCTIONS
¡ void is used as a function return type, indicating that it doesn’t return a value:
void printInfo(char name[], double r){
printf("Volume of %s is %.1f cubic kilometers\n",name,volumeOfSphere(r));
}
int main(){
printInfo("Moon", 1737);
printInfo("Mars", 3393);
printInfo("Earth", 6378.8);
printInfo("Sun", 696340);
return 0;
}
12
LIBRARY FUNCTIONS
¡ Formatted input/output:
¡ printf() is used to print the character, string, float, integer, octal and hexadecimal values onto the
output screen
¡ scanf() is used to read a character, string, numeric data from keyboard.
¡ getchar() reads character from keyboard
¡ putchar() writes a character to screen
¡ gets() reads line from keyboard
¡ puts() writes line to o/p screen
¡ …
14
STDIO.H
¡ Operations on files:
¡ fopen() opens a file
¡ fclose() closes an opened file
¡ getw() reads an integer from file
¡ putw() writes an integer to file
¡ fgetc() reads a character from file
¡ fgets() reads string from a file, one line at a time
¡ fputs() writes string to a file
¡ feof() finds end of file
¡ …
15
MATH.H
¡ Trigonometric functions:
¡ cos(): Returns the cosine of a radian angle x
¡ sin(): Returns the sine of a radian angle x
¡ tan(): Returns the tangent of a radian angle x
¡ acos(): Returns the arc cosine of x in radians
¡ asin(): Returns the arc sine of x in radians
¡ atan(): Returns the arc tangent of x in radians
16
MATH.H
¡ Hyperbolic functions:
¡ acosh(): Returns the area hyperbolic cosine of x
¡ asinh(): Returns the area hyperbolic sine of x
¡ atanh(): Returns the area hyperbolic tangent of x
¡ sinh(): Returns the hyperbolic sine of x
¡ cosh(): Returns the hyperbolic cosine of x
¡ tanh(): Returns the hyperbolic tangent of x
17
MATH.H
18
MATH.H
¡ Round functions:
¡ ceil(): Returns the smallest integer value greater than or
equal to x
¡ round(): returns the nearest integer
¡ floor(): Returns the largest integer value less than or equal
to x
¡ trunc(): Returns nearest integral value that is not larger in
magnitude than x
19
MATH.H
¡ Other functions:
¡ double fabs(x): Returns the absolute value
¡ double fmax(x, y): Returns the larger of its arguments
¡ double fmin(x, y): Returns the smaller of its arguments
¡ double sqrt(x); Returns the square root of x.
¡ double cbrt(x); Returns the cubic root of x.
20
CTYPE.H
21
STDLIB.H
22
STRING.H
23
TIME.H
¡ Time manipulation:
¡ clock(): Clock program
¡ difftime(): Return difference between two times
¡ mktime(): Convert tm structure to time_t
¡ time(): Get current time
¡ Conversion:
¡ asctime(): Convert tm structure to string
¡ ctime(): Convert time_t value to string
¡ gmtime(): Convert time_t to tm as UTC time
¡ localtime(): Convert time_t to tm as local time
¡ strftime(): Format time as string
24
LIMITS.H
¡ Write this expression as a C assignment statement using functions exp, log, and pow:
26
EXERCISE 2
1
𝑛! ≈ 𝑛! 𝑒 "! 2𝑛 + 𝜋
3
¡ Write a C program, to calculate and display the result of 25! Should look something
like this:
int main(){
int n;
printf("Enter an integer to find its factorial\n");
scanf("%d", &n);
float f = factorial(n);
printf("%d! equals approximately %f", n, f);
return 0;
}
28
EXERCISE 4
¡ A new strain of flu begins with a single case in a China. Three days later a second case is reported, and
in the following days the reported cases are as shown in the table below:
Day # 0 3 5 6 7 8 9 10 11
Total cases 1 2 3 4 5 7 9 11 15
¡ A math professor observes that the number of cases seems to be increasing by the following formula:
40000
𝐶𝑎𝑠𝑒𝑠 𝑥 =
1 + 39999(𝑒 !".$%&'( )
¡ Write a function that implements this model. Test your function by entering a day number and then
calculates the number of cases predicted
29
SUMMARY
¡ Top-Down Design
¡ Introduction to Function
¡ Function Design
¡ Library Function
30