Date post: | 18-Jan-2016 |
Category: |
Documents |
Upload: | blaze-sherman-garrett |
View: | 228 times |
Download: | 0 times |
Example # 1Example # 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.
• Goal: calculating the area of the room.• Input: length and width.• Process: area = length * width.• Output: area.
• Goal: calculating the area of the room.• Input: length and width.• Process: area = length * width.• Output: area.
STARTSTART
Input lengthInput length
Input widthInput width
area = length * widtharea = length * width
ENDEND
Output areaOutput area
Flowchart
• The following flowchart is in correct. Can you guess why?
STARTSTART
Input lengthInput length
Input widthInput width
area = length * widtharea = length * width
ENDEND
Output areaOutput area
• 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.
Example # 2Example # 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.
• 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.
avg = sum/3avg = sum/3
Is avg>=60
Is avg>=60
STARTSTART
Input m1,m2,m3Input m1,m2,m3
sum = m1+m2+m3sum = m1+m2+m3
ENDEND
Output “failed”Output “failed”
Output “passed”Output
“passed”
Flowchart
YES NO
Example # 3Example # 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
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);
}
Is Counter
<=5
Is Counter
<=5
STARTSTART
ENDEND
Output sum
Output sum
Input valueInput value
counter = counter + 1counter = counter + 1
sum =sum + valuesum =sum + value
counter = 1counter = 1
Sum = 0Sum = 0
YES NO
Flowchart
Questions?Questions?
• What would happen if the process counter = counter +1 did not exist?
• What would happen if the process sum =0 did not exist?
Example # 4Example # 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
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){
max = value;}counter = counter + 1;
}printf("The maximum is %d \n ",max);
}
Is Counter<=10
Is Counter<=10
STARTSTART
ENDEND
Output maxOutput max
Input valueInput value
counter = counter + 1counter = counter + 1
max = valuemax = value
counter = 1counter = 1
max = valuemax = value
Input valueInput value
Is Value>max
Is Value>max
YES
YES
NO
NO
Flowchart