SFTW120-LectureNotes-04

SFTW120-LectureNotes-04 - SFTW120 Programming Science...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 SFTW120 Programming Science Lecture Notes 4: C: Program Design and Functions (Textbook Chapter 3) © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 4 Designing programs s Applying the software development method to C programming: b Documenting results from analysis and design as program outlines s Example: b Problem: given the radius of a circle, calculate and display its area and circumference b Analysis: identify inputs, outputs, relationships (formulas) s Input: radius s Output: area, circumference s Formulas: area = π r 2 , circumference = 2 π r
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 SFTW120 - Programming Science 3 Lecture Notes 4 Example of program development b Design: identify the steps necessary to solve the problem 1. Get the circle’s radius 2. Calculate the area 3. Calculate the circumference 4. Display the area and circumference This can be further refined: 2.1 (refinement of step 2): assign PI * radius * radius to area 3.1 (refinement of step 3): assign 2 * PI * radius to circum SFTW120 - Programming Science 4 Lecture Notes 4 Documenting analysis and design Documenting the results of analysis and design as a program outline: /* * Calculates and displays the area and circumference of a * circle */ #define PI 3.14159 int main(void) { double radius; /* input - radius of a circle */ double area; /* output - area of a circle */ double circum; /* output - circumference */ (continued…)
Background image of page 2
3 SFTW120 - Programming Science 5 Lecture Notes 4 Documenting analysis and design (…continued) /* Get the circle radius */ /* Calculate the area */ /* Assign PI * radius * radius to area. */ /* Calculate the circumference */ /* Assign 2 * PI * radius to circum. */ /* Display the area and circumference */ } s Next step: the program outline needs to be extended to become the whole program ( implementation) b Write C code for each algorithm step SFTW120 - Programming Science 6 Lecture Notes 4 Extending a program outline to a program /* * Calculates and displays the area and circumference of a * circle */ #include <stdio.h> #define PI 3.14159 int main(void) { double radius; /* input - radius of a circle */ double area; /* output - area of a circle */ double circum; /* output - circumference */ (continued…)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 SFTW120 - Programming Science 7 Lecture Notes 4 Extending a program outline to a program (…continued) /* Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* Calculate the area */ area = PI * radius * radius; /* Calculate the circumference */ circum = 2 * PI * radius; /* Display the area and circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return (0); } SFTW120 - Programming Science 8 Lecture Notes 4 Testing the finished program s After compiling the program, run it, provide some sample input, and manually check if the produced output is correct s Example: b Sample input circle radius = 5 b Expected output (calculated manually): s Area = π r 2 = π × 5 2 = π × 25 = 78.53975 s Circumference = 2
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

SFTW120-LectureNotes-04 - SFTW120 Programming Science...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online