Computer Programming (1) Tabuk University: Computer Programming (1) Tabuk University
Flowchart symbols: Flowchart symbols Start / Stop Process Input / output Condition Connector Direction Arrows
Simple Flowcharts (1) Summation of two numbers: Simple Flowcharts (1) Summation of two numbers Example: Draw a flowchart for a program that sums two numbers. Solution Step 1 : Identify input First number Second number Step 2 : identify output The summation of the two numbers Step 3: Construct the algorithm Input first number (A) Input second number (B) Sum A and B and store the result in (C) Print C Start Read A, B C = A + B Print C Stop
Simple Flowcharts (2) Average of three numbers: Simple Flowcharts (2) Average of three numbers Example: Draw a flowchart for a program that finds the average of three numbers Solution Step 1 : Identify input First number Second number Third number Step 2 : identify output The average of the three numbers Step 3: Construct the algorithm Input first number (A) Input second number (B) Input Third number (C) Sum A and B and C then divide the result by 3 and store the result in (D) Print D Start Read A, B, C D = (A + B + C) / 3 Print D Stop
Simple Flowcharts (3) Area of rectangle: Simple Flowcharts (3) Area of rectangle Example: Draw a flowchart for a program that finds the area of a rectangle. Solution Step 1 : Identify input Length of rectangle Width of rectangle Step 2 : identify output The area of the rectangle Step 3: Construct the algorithm Input length of rectangle (L) Input width of rectangle (W) Multiply L by W then store the result in (A) Print A Start Read L, W A = L * W Print A Stop
Simple Flowcharts (4) Volume of a sphere: Simple Flowcharts (4) Volume of a sphere Example: Draw a flowchart for a program that finds the volume of a sphere. Note that the volume of sphere can be computed using the following formula : Solution Step 1 : Identify input Radius of sphere Step 2 : identify output The volume of the sphere Step 3: Construct the algorithm Input radius of the sphere (r) Compute the volume of the sphere using the formula given above and store the result in (V) Print V Start Read r V=4/3 * r * r * r * 3.14 Print V Stop
Branching Flowcharts (1) Print pass if mark >=60: Branching Flowcharts (1) Print pass if mark >=60 Example: Draw a flowchart for a program that prints the word PASS if a student mark is greater or equal to 60. Solution Step 1 : Identify input Student mark (M) Step 2 : identify output If the student mark above or equal to 50 the output will be PASS , otherwise no output will be printed. Step 3: Construct the algorithm Input student mark (M) If M >=50 print PASS Start Read M Print “PASS” Stop M>=60 Yes No
Branching Flowcharts (2) Print pass if mark >=60 otherwise print fail: Branching Flowcharts (2) Print pass if mark >=60 otherwise print fail Example: Draw a flowchart for a program that prints the word PASS if a student mark is greater or equal to 60, otherwise it should print FAIL. Solution Step 1 : Identify input Student mark (M) Step 2 : identify output If the student mark above or equal to 50 the output will be PASS , otherwise print FAIL. Step 3: Construct the algorithm Input student mark (M) If M >=50 print PASS otherwise print FAIL Start Read M Print “PASS” Stop M>=60 Print “FAIL” Yes No
Branching Flowcharts (3) Find Largest among two numbers: Branching Flowcharts (3) Find Largest among two numbers Example: Draw a flowchart for a program that takes two numbers and decides which one of them is the greatest. Assume the two numbers are not the same. Solution Step 1 : Identify input First number (N1) Second Number (N2) Step 2 : identify output If the first number is greater than the second number then the program prints “First is greater” , otherwise it prints “Second is greater”. Step 3: Construct the algorithm Input two numbers (N1 , N2) If N1 > N2 print First is greater otherwise print Second is greater Start Read N1 , N2 Print “First is greater” Stop N1 > N2 Print “Second is greater” Yes No
Branching Flowcharts (4) Classify weather conditions according to temperature: Branching Flowcharts (4) Classify weather conditions according to temperature Example: Draw a flowchart for a program that prints the classification of the weather according to the following temperature classification: Below 15 : Cold Between 15 and 28 : moderate Above 28 : Hot Solution Step 1 : Identify input Temperature (T) Step 2 : identify output Depending on the value of T the output will be cold, moderate ,or hot Step 3: Construct the algorithm Input temperature (T) If T < 15 then print “Cold” else step c If T < 28 then print “Moderate” else step d Print “Hot” Print “Moderate” Yes Start Read T Print “Cold” Stop T < 15 Print “Hot” Yes No T < 28 No
Branching Flowcharts (5) Finds largest among three numbers – 1st solution: Step 2 : identify output The largest among A , B , and C Step 3: Construct the algorithm Input three numbers (A , B , C) If A > B then step c else step e If A > C then print A else print C stop If B > C then print B else print C stop Branching Flowcharts (5) Finds largest among three numbers – 1 st solution Example: Draw a flowchart for a program that prints the maximum number among three numbers. Solution Step 1 : Identify input Three Numbers ( A , B , C ) Start A , B , C Stop A > B Yes No Print B Yes Print C B > C No Print A Yes Print C A > C No
Branching Flowcharts (6) Finds largest among three numbers – 2nd solution: Step 2 : identify output The largest among A , B , and C Step 3: Construct the algorithm Input three numbers (A , B , C) If B > C then step c else step e If B > A then print B else print A stop If C > A then print C else print A stop Branching Flowcharts (6) Finds largest among three numbers – 2 nd solution Example: Draw a flowchart for a program that prints the maximum number among three numbers. Solution Step 1 : Identify input Three Numbers ( A , B , C ) Start A , B , C Stop B > C Yes No Print C Yes Print A C > A No Print B Yes Print A B > A No
Branching Flowcharts (7) Finds largest among three numbers – 3rd solution: Step 2 : identify output The largest among A , B , and C Step 3: Construct the algorithm Input three numbers (A , B , C) If B > C then step c else step e If B > A then max = B else print max = A stop If C > A then max = C else max = A stop Branching Flowcharts (7) Finds largest among three numbers – 3 rd solution Example: Draw a flowchart for a program that prints the maximum number among three numbers. Solution Step 1 : Identify input Three Numbers ( A , B , C ) Stop Start A , B , C B > C Yes No max = C Yes max = A C > A No max = B Yes max = A B > A No Print max
Branching Flowcharts (8) Finds largest among three numbers – 4th solution: Branching Flowcharts (8) Finds largest among three numbers – 4 th solution Example: Draw a flowchart for a program that prints the maximum number among three numbers. Solution Step 1 : Identify input Three Numbers ( A , B , C ) Step 2 : identify output The largest among A , B , and C Step 3: Construct the algorithm Input three numbers (A , B , C) Set max = A If B > max then max =B If C > max then max = C stop B > max Yes max = B No Start A , B , C Stop Print max C > max Yes max = C No max = a
Branching Flowcharts (9) Finds largest among fourth numbers : The flowchart below is the solution of the question on the left. If the question requires finding the max among 100 numbers then how long the resulted flowchart will be???!!!! There should be another approach to solve it! Branching Flowcharts (9) Finds largest among fourth numbers Question Draw a flowchart for a program that prints the maximum number among four numbers. Start A , B , C , D Stop A > B Yes No Print A A > C Yes No B > C Yes No A > D Yes No C > D Yes No B > D Yes No C > D Yes No Print D Print C Print D Print B Print D Print C Print D
Looping Flowcharts (1) Print the word Tabuk 100 times: Looping Flowcharts (1) Print the word Tabuk 100 times Example: Draw a flowchart for a program that prints the word TABUK 100 times. Solution Step 1 : Identify input No input required (input inside the algorithm) Step 2 : identify output Printing the word TABUK 100 times Step 3: Construct the algorithm Set C = 1 If C<= 100 then step c else step f Print “TABUK” Set C = C + 1 Go to step b stop Start C = 1 Print “TABUK” Stop C<=100 Yes No C = C +1
Looping Flowcharts (2) Print numbers from 1 to 100: Looping Flowcharts (2) Print numbers from 1 to 100 Example: Draw a flowchart for a program that prints the numbers from 1 to 100 Solution Step 1 : Identify input No input required (input inside the algorithm) Step 2 : identify output Printing numbers from 1 to 100 Step 3: Construct the algorithm Set C = 1 If C<= 100 then step c else step f Print C Set C = C + 1 Go to step b stop Start C = 1 Print C Stop C<=100 Yes No C = C +1
Looping Flowcharts (3) prints numbers from 1 to N: Looping Flowcharts (3) prints numbers from 1 to N Example: Draw a flowchart for a program that prints the numbers from 1 to N Solution Step 1 : Identify input The limit to print to (N) Step 2 : identify output Printing numbers from 1 to N Step 3: Construct the algorithm Set C = 1 If C<= N then step c else step f Print C Set C = C + 1 Go to step b stop Start C = 1 Print C Stop C<=N Yes No C = C +1 Input N
Looping Flowcharts (4) Prints the word Tabuk N times: Looping Flowcharts (4) Prints the word Tabuk N times Example: Draw a flowchart for a program that prints the word TABUK N times Solution Step 1 : Identify input The limit to print to (N) Step 2 : identify output Printing the word TABUK N times Step 3: Construct the algorithm Set C = 1 If C<= N then step c else step f Print “TABUK” Set C = C + 1 Go to step b stop Start C = 1 Print “TABUK” Stop C<=N Yes No C = C +1 Input N
Looping Flowcharts (5) Sums numbers from 1 to N: Looping Flowcharts (5) Sums numbers from 1 to N Example: Draw a flowchart for a program that sums the numbers from 1 to N Solution Step 1 : Identify input The limit to print to (N) Step 2 : identify output Sum of numbers from 1 to N (S) Step 3: Construct the algorithm Set C = 1 Set S = 0 If C<= N then step d else step g Set S = S + C Set C = C + 1 Go to step c Print S stop Start C = 1 S = 0 Print S Stop C<=N Yes No C = C +1 Input N S = S + C
Looping Flowcharts (6) Sums N numbers: Looping Flowcharts (6) Sums N numbers Example: Draw a flowchart for a program that reads N numbers and prints their summation. Solution Step 1 : Identify input The number of numbers to read (N) The numbers to read (M) Step 2 : identify output Sum of N numbers (S) Step 3: Construct the algorithm Set C = 1 Set S = 0 If C<= N then step d else step h Read M Set S = S + M Set C = C + 1 Go to step c Print S stop Start C = 1 S = 0 Print S Stop C<=N Yes No C = C +1 Input N S = S + M Read M
Looping Flowcharts (7) Finds the average of N numbers: Looping Flowcharts (7) Finds the average of N numbers Example: Draw a flowchart for a program that reads N numbers and prints their average. Solution Step 1 : Identify input The number of numbers to read (N) The numbers to read (M) Step 2 : identify output average of N numbers (AVG) Step 3: Construct the algorithm Set C = 1 Set S = 0 If C<= N then step d else step h Read M Set S = S + M Set C = C + 1 Go to step c AVG = S / N Print AVG stop Start C = 1 S = 0 Print S Stop C<=N Yes No C = C +1 Input N S = S + M Read M AVG= S / N
Looping Flowcharts (8) Count pass marks: Looping Flowcharts (8) Count pass marks Example: Draw a flowchart for a program that reads N marks and prints number of marks above or equal to 60. Solution Step 1 : Identify input The number of numbers to read (N) The numbers to read (M) Step 2 : identify output Number of marks above or equal to 60 (Count) Step 3: Construct the algorithm Set C = 1 Set Count = 0 If C<= N then step d else step i Read M If M >=60 then step f else step g Set Count = Count + 1 Set C = C + 1 Go to step c Print S stop Count= Count + 1 Start C = 1 Count = 0 Print Count Stop C<=N Yes No C = C +1 Input N Read M M>=60 Yes No