Download presentation
Presentation is loading. Please wait.
Published bySheila Shelton Modified over 9 years ago
1
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 3 Slides By Dr. Daniyal Alghazzawi
2
CHAPTER 3 - Outline Program 3: Calculate the Area and the Circumference Introduction to Functions Program 4: Performs 3 Square Root Computations Function Prototype Some Mathematical Library Functions Program 5: Draw Houses using Functions
3
Program 3: Calculate the Area and the Circumference Calculate the Area and the Circumference for a circle. Area Circumference Implementation Problem Analysis Design / Algorithm TestingMaintenance 1. 2. 3. 4. 5. 6.
4
Program 3: Calculate the Area and the Circumference 1. Problem Constant: PI = 301159 2. Problem Input: circle’s radius 3. Problem Output: - Area - Circumference 4. Relevant Formula: - area = π × radius 2 - circumference = 2 π × radius ImplementationProblem Analysis Design / Algorithm TestingMaintenance 1. 2. 3. 4. 5. 6.
5
Program 3: Calculate the Area and the Circumference 1. Get the circle radius. 2. Calculate the area. (area = π × radius × radius) 3. Calculate the circumference. (circumference = 2 π × radius) 4. Display the area and the circumference. ImplementationProblemAnalysis Design / Algorithm Design / Algorithm TestingMaintenance 1. 2. 3. 4. 5. 6.
6
Program 3: Calculate the Area and the Circumference Implementation ProblemAnalysis Design / Algorithm TestingMaintenance 1. 2. 3. 4. 5. 6. Outline
7
Program 3: Calculate the Area and the Circumference
8
ImplementationProblemAnalysis Design / Algorithm Testing Maintenance 1. 2. 3. 4. 5. 6.
9
Introduction to Functions Primary goal of Software Engineering: Error-free code Code reuse (using Functions) You need to #include the file that has the functions. For example: The function sqrt() written in the file math.h
10
Program 4: Performs 3 Square Root Computations
11
Program 4: (OUTPUT ERROR without )
12
Program 4: (OUTPUT)
13
Function Prototype Y = sqrt(x); return value - int - double - char - void function nameargument(s) - int - double - char - void function call
14
Some Mathematical Library Functions Check TABLE 3.1 from the book.
15
Program 5: Draw Houses using Functions Problem: We need a program that can draw 2 types of houses: Type 1:Type 2: “Big House”“Small House” /\ *------* | | *------* /\ *------* | | *------*
16
Program 5: Draw Houses using Functions 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 2 nd 3 rd functions /\ *------* | | *------* | | *------*
17
Program 5: Draw Houses using Functions #include void draw_triangle(void) { printf("\n /\\ "); } void draw_square(void) { printf("\n *------*"); printf("\n | |"); 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.
18
Program 5: (OUTPUT) #include void draw_triangle(void) { printf("\n /\\ "); } void draw_square(void) { printf("\n *------*"); printf("\n | |"); 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); }
19
CHAPTER 3 - Conclusion Introduction to Functions Function Prototype Some Mathematical Library Functions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.