Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example # 1 Draw a flowchart for calculating the area of a room:

Similar presentations


Presentation on theme: "Example # 1 Draw a flowchart for calculating the area of a room:"— Presentation transcript:

1 Example # 1 Draw a flowchart for calculating the area of a room:
In order to calculate the area of the room, we must know firstly the length and the width of the room. Then we calculate the area by multiplying the length by width.

2 Goal: calculating the area of the room.
Input: length and width. Process: area = length * width. Output: area.

3 C code: don’t worry about it now
#include<stdio.h> void main() { int length, width, area; scanf("%d",&lendth); scanf("%d", & width); area = length * width; printf("%d",area); } Flowchart START Input length Input width area = length * width Output area END

4 The following flowchart is in correct. Can you guess why?
START Input length Input width area = length * width END Output area

5 It is wrong because we cannot calculate the area of the room before knowing the length and width of the room. So, we should input the length and width of the room then calculate area.

6 Example # 2 Draw a flowchart which reflects the process of deciding whether a student has passed or failed . the course based on the average three exams.

7 Goal: decide whether the student pass or failed the course.
Input: m1, m2, m3 /*m mass mark */ Arithmetic Process: sum = m1 + m2 + m3. average = sum/3. Logical process: is the average greater or equal to 60. Output: passed or failed.

8 C code:don’t worry about it now
#include<stdio.h> void main() { int m1, m2, m3; Int sum, avg; scanf("%d%d%d", &m1,&m2,&m3); sum = m1 + m2 + m3; avg = sum/3; if(avg >=60) { Printf("passed"); } else Printf("faild"); Flowchart START Input m1,m2,m3 sum = m1+m2+m3 avg = sum/3 YES Is avg>=60 NO Output “passed” Output “failed” END

9 Example # 3 Draw a flowchart which reflects the process of calculating the sum of 5 values: Goal: calculate the sum of 5 values. Input: value Process: adding the values to each other. Output: the sum

10 C code: don’t worry about it now
Flowchart C code: don’t worry about it now #include<stdio.h> void main() { int value, sum, counter; sum = 0; counter = 1; while(counter<=5) { scanf("%d",& value); sum = sum + value; counter = counter + 1; } printf("%d",sum); START Sum = 0 counter = 1 Is Counter<=5 YES NO Input value sum =sum + value counter = counter + 1 Output sum END

11 Questions? What would happen if the process counter = counter +1 did not exist? What would happen if the process sum =0 did not exist?

12 Example # 4 Draw a flowchart for finding the maximum value of 10 input values. Goal: finding the maximum value. Input: value Process: comparing to find the maximum value. Output: the maximum value

13 Flowchart START Input value max = value counter = 1 Is Counter<=10
C code:don’t worry about it now. #include<stdio.h> void main() { int value, max, counter; scanf("%d", & value); max = value; counter = 1; while(counter<=10) { scanf("%d", &value); if(value>max) } counter = counter + 1; printf("The maximum is %d \n ",max); Flowchart START Input value max = value counter = 1 Is Counter<=10 NO YES Input value Is Value>max NO YES max = value counter = counter + 1 Output max END


Download ppt "Example # 1 Draw a flowchart for calculating the area of a room:"

Similar presentations


Ads by Google