DCIT22 Lecture 1 Algorithm and Flowcharting part1 PDF
Document Details
Uploaded by GodGivenAntigorite1940
John Williams
Tags
Summary
This document is a lecture on algorithms and flowcharting, part 1. It introduces the concepts of algorithms, flowcharts, and provides examples with calculations and flowcharts for sums, products, and various geometric shapes. The document is intended for an undergraduate computer science course.
Full Transcript
Lecture 1: ALGORITHM & FLOWCHARTING (Part 1) Define the problem before you pursue a solution ~ John Williams Contents 1 Algorithm 2 Flowcharting 3 Symbols in Flowcharting ALGORITHM Procedure for solving a problem in terms of the actions to execute, and the order in whi...
Lecture 1: ALGORITHM & FLOWCHARTING (Part 1) Define the problem before you pursue a solution ~ John Williams Contents 1 Algorithm 2 Flowcharting 3 Symbols in Flowcharting ALGORITHM Procedure for solving a problem in terms of the actions to execute, and the order in which these actions execute ALGORITHM Sample Problem #1: Create an algorithm that accepts 2 numbers and compute for the sum. INPUT: 2 numbers, num1 and num2 PROCESS: addition OUTPUT: sum ALGORITHM – Sample Problem#1 INPUT: 2 numbers, num1 and num2 PROCESS: addition OUTPUT: sum 1. Accept 2 numbers and call it num1 and num2. 2. Add num1 and num2 and store it to sum. 3. Display sum. ALGORITHM Sample Problem #2: Create an algorithm that accepts 2 numbers and compute for the sum, difference, product and quotient. INPUT: 2 numbers, num1 and num2 PROCESS: addition, subtraction, multiplication, division OUTPUT: sum, difference, product, quotient ALGORITHM – Sample Problem#2 INPUT: 2 numbers, num1 and num2 PROCESS: addition, subtraction, multiplication, division OUTPUT: sum, difference, product, quotient 1. Accept 2 numbers and call it num1 and num2. 2. Add num1 and num2 and call it sum. 3. Subtract num2 from num1 and store it to difference. 4. Multiply num1 and num2 and store it to product. 5. Divide num1 by num2 and store it to quotient 6. Display sum, difference, product, quotient. FLOWCHART Graphical presentation of an algorithm, often used in the design phase of programming to work out the logical flow of a program SYMBOLS IN FLOWCHARTING FLOWCHART – Sample Problem#1 Start INPUT: 2 numbers, num1 and num2 num1, PROCESS: num2 addition OUTPUT: sum sum = num1 + 1. Accept 2 numbers and num2 call it num1 and num2. 2. Add num1 and num2 and store it to sum. sum 3. Display sum. End FLOWCHART – Sample Problem#2 INPUT: 2 numbers, num1 and Start num2 PROCESS: addition, subtraction, num1, multiplication, division sum = num1 + num2 OUTPUT: sum, difference, num2 product, quotient diff = num1 – 1. Accept 2 numbers and call it num1 and num2. num2 2. Add num1 and num2 and store it to prod = num1 * sum. num2 3. Subtract num2 from num1 and store it to quo = num1 / difference. sum, diff, num2 4. Multiply num1 and num2 and store it to prod, quo product. 5. Divide num1 by num2 and store it to quotient End 6. Display sum, difference, product, quotient. FLOWCHART – Sample Problem#3 Sample Problem #3: Start Accept radius and compute the area of circle radius INPUT: radius PROCESS: areaOfCircle = 3.1416 * radius * radius areaOfCircle = OUTPUT: areaOfCircle 3.1416 * radius * radius 1. Accept radius and call it radius. 2. Multiply radius by itself and call it radius2. areaOfCircl 3. Multiply 3.1416 by radius2 and store e it to areaOfCircle. 4. Display areaOfCircle. End SAMPLE PROBLEM #4 Sample Problem #4: Create the algorithm and flow chart that will perform the computation of area of circle, square, rectangle and triangle. Use the following formula: area of circle = πradius2, area of square = side2, area of rectangle = length * width, and area of triangle = ½ * base * height. SAMPLE PROBLEM #4 Use the following formula: area of circle = πradius2, area of square = side2, area of rectangle = length * width, and area of triangle = ½ * base * height. areaOfCircle = 3.1416 * radius * radius areaOfSquare = side * side areaOfRectangle = length * width areaOfTriangle = 0.5 * base * height SAMPLE PROBLEM#4 areaOfCircle = 3.1417 * radius * radius areaOfSquare = side * side areaOfRectangle = length * width areaOfTriangle = 0.5 * base * height INPUT: radius, side, length, width, base, height SAMPLE PROBLEM#4 areaOfCircle = 3.1417 * radius * radius areaOfSquare = side * side areaOfRectangle = length * width areaOfTriangle = 0.5 * base * height PROCESS: areaOfCircle = 3.1417 * radius * radius, areaOfSquare = side * side, areaOfRectangle = length * width, areaOfTriangle = 0.5 * base * height SAMPLE PROBLEM#4 areaOfCircle = 3.1417 * radius * radius areaOfSquare = side * side areaOfRectangle = length * width areaOfTriangle = 0.5 * base * height OUTPUT: areaOfCircle, areaOfSquare, areaOfRectangle, areaOfTriangle SAMPLE PROBLEM#4 INPUT: radius, side, length, width, base, height PROCESS: areaOfCircle = 3.1417 * radius * radius, areaOfSquare = side * side, areaOfRectangle = length * width, areaOfTriangle = 0.5 * base * height OUTPUT: areaOfCircle, areaOfSquare, areaOfRectangle, areaOfTriangle SAMPLE PROBLEM#4 INPUT: radius, side, length, width, base, height 1. Accept value of radius and call it fRadius, value of side and call it fSide, value of length and call it fLength, value of width and call it fWidth, value of base and call it fBase & value of height and call it fHeight SAMPLE PROBLEM#4 PROCESS: areaOfCircle = 3.1417 * radius * radius, areaOfSquare = side * side, areaOfRectangle = length * width, areaOfTriangle = 0.5 * base * height 2. Multiply fRadius by itself and store it to fRadius2. Then multipy fRadius2 by 3.1416 and store it to areaOfCircle. 3. Multiply fSide by itself and store it to areaOfSquare SAMPLE PROBLEM#4 PROCESS: areaOfCircle = 3.1417 * radius * radius, areaOfSquare = side * side, areaOfRectangle = length * width, areaOfTriangle = 0.5 * base * height 4. Multiply fLength by fWidth and store it to areaOfRectangle 5. Multiply 0.5, fBase and fHeight and store it to areaOfTriangle SAMPLE PROBLEM#4 OUTPUT: areaOfCircle, areaOfSquare, areaOfRectangle, areaOfTriangle 6. Display areaOfCirle, areaOfSquare, areaOfRectangle, areaOfTriangle SAMPLE PROBLEM#4 FLOWCHART Start 1. Accept value of radius and call it fRadius, value of fRadius, side and call it fSide, fSide, value of length and fLength, call it fLength, value fWidth, of width and call it fWidth, value of fBase, base and call it fHeight fBase & value of SAMPLE PROBLEM#4 Start FLOWCHART fRadius, fSide, 2. Multiply fRadius fLength, fWidth, by itself and store it fBase, fHeight to fRadius2. Then areaOfCircle = 3.1416 * multipy fRadius2 by fRadius * fRadius 3.1416 and store it toareaOfSquare = fSide * areaOfCircle. fSide areaOfRectangle = 3. Multiply fSide by fLength * fWidth itself and store it to areaOfTriangle = 0.5 * areaOfSquare fBase * fHeight 4. Multiply fLength SAMPLE PROBLEM#4 START FLOWCHART fRadius, fSide, 6. Display fLength, fWidth, fBase, fHeight areaOfCirle, areaOfCircle = 3.1416 * fRadius * areaOfSquare, fRadius areaOfRectangle, areaOfSquare = fSide * fSide areaOfTriangle areaOfRectangle = fLength * fWidth areaOfTriangle = 0.5 * fBase * fHeight areaOfCirle, areaOfSquare, areaOfRectangle, areaOfTriangle STOP SIMULATION A computer simulation model is a computer program or algorithm which simulates changes of a modeled system in response to input signals. - Springe Link https://www.slideshare.net/DIVYAJAYAN4/computer-simulationthmx-92260357 SAMPLE PROBLEM#4 START Ex. The user inputs the ff fRadius, fSide, values: fRadius = 2, fSide = 4, fLength, fWidth, fLength = 5, fWidth = 3, fBase fBase, fHeight = 4, fHeight fRadi = 6 fLeng fWidt fSide us th h areaOfCircle = 3.1416 * fRadius * fRadius areaOfSquare = fSide * fSide 2 4 5 3 areaOfRectangle = fLength * fWidth fHeig areaOfCircl areaOfTriangle = 0.5 * fBase * fBase fHeight ht e areaOfCirle, 4 6 areaOfSquare, areaOfRectangle , areaOfTriangle STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength = 5, fWidth = 3, fBase = 4, fHeight = 6 fRadius, fSide, fLength, fWidth, fRadiu fLengt fSide fWidth fBase, fHeight s h areaOfCircle = 3.1416 * fRadius 2 4 5 3 * fRadius areaOfCircl areaOfSquare = fSide * fSide fBase fHeight areaOfRectangle = fLength * e fWidth areaOfTriangle = 0.5 * fBase * 4 6 areaOfCircle = 3.1416 * fRadius * fHeight areaOfCirle, fRadius areaOfSquare, areaOfRectang areaOfCircle = 3.1416 * 2 * 2 le, areaOfTriangle areaOfCircle = 12.5664 STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength = 5, fWidth = 3, fBase = 4, fHeight = 6 fRadius, fSide, fLength, fWidth, fRadiu fLengt fSide fWidth fBase, fHeight s h 2 4 5 3 areaOfCircle = 3.1416 * fRadius * fRadius areaOfCircl fBase fHeight areaOfSquare = fSide * fSide areaOfRectangle = fLength * fWidth 12.56 e areaOfTriangle = 0.5 * fBase * 4 6 fHeight 64 areaOfCirle, areaOfSquare, areaOfRectangle , areaOfTriangle STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength = 5, fWidth = 3, fBase = 4, fHeight = 6 fRadius, fSide, fLength, fWidth, fRadiu fLengt fSide fWidth fBase, fHeight s h 2 4 5 3 areaOfCircle = 3.1416 * fRadius * fRadius areaOfCircl fBase fHeight areaOfSquare = fSide * fSide areaOfRectangle = fLength * fWidth 12.56 e 4 areaOfTriangle = 0.5 * fBase * 6 areaOfSquare = fSide * fHeight areaOfSquar 64 fSide areaOfCirle, e areaOfSquare, areaOfSquare = 4 * 4 areaOfRectangle , areaOfTriangle areaOfSquare = 16 STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength fRadius, fSide, = 5, fLength, fWidth, fWidthfRadi = 3, fBase fSide =fLeng 4, fWidt= fHeight fBase, fHeight 6 us th h 2 4 5 3 areaOfCircle = 3.1416 * fRadius * fRadius fHeig areaOfCircl fBase areaOfSquare = fSide * fSide areaOfRectangle = fLength * fWidth ht 12.566 e areaOfTriangle = 0.5 * fBase * 4 6 fHeight areaOfSqu 4 areaOfCirle, areaOfSquare, are areaOfRectangle , areaOfTriangle 16 STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength fRadius, fSide, = 5, fLength, fWidth, fWidthfRadi = 3, fBase fSide =fLeng 4, fWidt= fHeight fBase, fHeight 6 us th h 2 4 5 3 areaOfCircle = 3.1416 * fRadius * fRadius fHeig areaOfCircl fBase areaOfSquare = fSide * fSide areaOfRectangle = fLength * fWidthht e 4 = fLength areaOfRectangle areaOfTriangle = 0.5 * fBase * fHeight 6 12.5664 * fWidth areaOfRectangle = 5 * 3 areaOfRectan areaOfSqu areaOfRectangle are areaOfCirle, = 15 gle areaOfSquare, areaOfRectangle , areaOfTriangle 16 STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength fRadius, fSide, = 5, fLength, fWidth, fWidthfRadi = 3, fBase fSide =fLeng 4, fWidt= fHeight fBase, fHeight 6 us th h 2 4 5 3 areaOfCircle = 3.1416 * fRadius * fRadius fHeig areaOfCircl areaOfSquare = fSide * fSide fBase areaOfRectangle = fLength * fWidth ht e areaOfTriangle = 0.5 * fBase * fHeight 4 6 12.5664 areaOfSqu areaOfRectan areaOfCirle, areaOfSquare, are gle areaOfRectangle , areaOfTriangle 16 15 STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength = 5, fRadius, fSide, fRadi fLeng fWidt fSide = 4, fHeight = fWidth = 3, fBase fLength, fWidth, us th h fBase, fHeight 6 2 4 5 3 areaOfCircle = 3.1416 * fRadius * fHeig fBase areaOfCircle fRadius ht 4 areaOfSquare = fSide * fSide 6 areaOfRectangle = fLength * fWidth 12.5664 areaOfTriangle = 0.5 * fBase * fHeight areaOfTriangle = 0.5 * fBase * areaOfTriangle = areaOfSqu fHeight 0.5 * 4 * 6 areaOfRectan areaOfTriangle = 12 areaOfCirle, are gle areaOfSquare, areaOfRectangle 16 15 , areaOfTriangle areaOfTriang le STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength = 5, fWidth = 3, fBase = 4, fHeight = 6 fRadius, fSide, fRadius fSide fLength fWidth fBase fLength, fWidth, fBase, fHeight 2 4 5 3 4 areaOfCircle = 3.1416 * fRadius * fRadius fHeight areaOfCircle areaOfSquare areaOfSquare = fSide * fSide areaOfRectangle = fLength * fWidth areaOfTriangle = 0.5 * fBase * 6 12.5664 16 fHeight areaOfCirle, areaOfRectangle areaOfTriangle areaOfSquare, areaOfRectangle , areaOfTriangle 15 12 STOP SAMPLE PROBLEM#4 START fRadius = 2, fSide = 4, fLength = 5, fWidth = 3, fBase = 4, fHeight = 6 fRadius, fSide, fRadius fSide fLength fWidth fBase fLength, fWidth, fBase, fHeight 2 4 5 3 4 OUTPUT: areaOfCircle = 3.1416 * fRadius * fRadius Area of fHeight areaOfSquare = fSide * fSide CircleareaOfCircle = areaOfSquare areaOfRectangle = fLength * fWidth 12.5664 6 areaOfTriangle = 0.5 * fBase * 12.5664 16 fHeight Area of Square = 16 areaOfRectangle areaOfTriangle areaOfCirle, areaOfSquare, Area of Rectangle = , areaOfTriangle 15 areaOfRectangle 15 12 Area of Triangle = 12 STOP - End -