ALGORITHMS & FLOWCHARTING II

Slides:



Advertisements
Similar presentations
ALGORITHMS AND FLOWCHARTS
Advertisements

Flow Chart.
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
PROBLEM SOLVING TECHNIQUES
CS1010 Programming Methodology
Algorithm and Flowchart Questions
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Flowcharts Remember that a solution to a problem is called an algorithm. Algorithms are often a series of steps required to solve the problem. A flowchart.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
ALGORITHMS AND FLOWCHARTS
Chapter 2 - Algorithms and Design
Algorithms and Flowcharts for Programming CFD
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
ANALYSIS AND ALGORITHM DESIGN - IV. Repeat statements/looping/counting/iterations It is often necessary to repeat certain parts of a program a number.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
Basic Control Structures
Recursive Algorithms &
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Introduction to compilation process March 24. The following slides will show you step by step instruction how to get ready and build C language programs.
Introducing block scheme programming March 17. Algorithm / Flow chart An algorithm or a flowchart is a step-by-step procedure for solving a particular.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
INVITATION TO Computer Science 1 11 Chapter 2 The Algorithmic Foundations of Computer Science.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Flowcharts C++ Lab. Algorithm An informal definition of an algorithm is: a step-by-step method for solving a problem or doing a task. Input data A step-by-step.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Introduction to Flowcharts
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
Program Design & Development EE 201 C7-1 Spring
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Program design Program Design Process has 2 phases:
Problem Solving & Computer Programming
Starter: Program Flowchart & Trace Table
GC101 Introduction to computers and programs
Lesson 2 Flowcharting.
Unit 3: ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
PROGRAM CONTROL STRUCTURE
Algorithms and Flowcharts
CHAPTER 5A Loop Structure
Java for Beginners.
Chapter 5: Control Structure
ALGORITHMS & FLOWCHARTING II
Repetition-Counter control Loop
( Iteration / Repetition / Looping )
Siti Nurbaya Ismail Senior Lecturer
ALGORITHMS AND FLOWCHARTS
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Java for Beginners University Greenwich Computing At School DASCO
ALGORITHMS AND FLOWCHARTS
Algorithm Discovery and Design
Building Java Programs
Chapter 5: Control Structure
Flowchart.
Lecture 7 Algorithm Design & Implementation. All problems can be solved by employing any one of the following building blocks or their combinations 1.
Introduction to Programming
Little Man Computer (continued).
Introduction to Algorithms - 2
Introduction to Algorithms - 2
Presentation transcript:

ALGORITHMS & FLOWCHARTING II

LOOPS Computers are particularly well suited to applications in which operations are repeated many times. If the same task is repeated over and over again a loop can be used to reduce program size and complexity

Example 8: Write an algorithm and draw a flowchart to calculate 24 . Step 1: Base  2 Step 2: Product  Base Step 3: Product  Product * Base Step 4: Product  Product * Base Step 5: Product  Product * Base Step 6: Print Product

Flowchart Base2 START Product  Base Product  Product * Base Print STOP Product  Product * Base Base2

Question: What happens if you want to calculate 2 to the power of 1000? Answer: Use a LOOP (repeated execution of the same set of instructions)

Example 9: Write an algorithm and draw a flowchart to calculate 24 using a loop approach? Verify your result by a trace table.

Algorithm: Step 1: Base  2 Step 2: Power  4 Step 3: Product  Base Step 4: Counter  1 Step 5: While Counter < Power Repeat Step 5 through step 7 Step 6: Product  Product * Base Step 7: Counter  Counter +1 Step 8: Print Product

Product  Product * Base START Product  Base Counter  1 Print Product STOP Y is Counter < Power Product  Product * Base Counter  Counter + 1 N Base  2 Power 4

TRACING BASE POWER PRODUCT COUNTER COUNTER < POWER Step 1: Base  2 STEP 4: 2 4 2 1 T STEP 5: 2 4 2 1 T STEP 6: 2 4 2x2=4 1 T STEP 7: 2 4 4 1+1=2 T STEP 5: 2 4 4 2 T STEP 6: 2 4 4x2=8 2 T STEP 7: 2 4 8 2+1=3 T STEP 5: 2 4 8 3 T STEP 6: 2 4 8x2=16 3 T STEP 7: 2 4 16 3+1=4 F STEP 5: 2 4 16 4 F STEP 8: print 16. Step 1: Base  2 Step 2: Power  4 Step 3: Product  Base Step 4: Counter  1 Step 5: While Counter < Power Repeat Step 5 through step 7 Step 6: Product  Product * Base Step 7: Counter  Counter +1 Step 8: Print Product

Example 10: Write down an algorithm and draw a flowchart to find and print the largest of three numbers. Read numbers one by one. Verify your result by a trace table. (Use 5, 7, 3 as the numbers read)

Algorithm Step 1: Input N1 Step 2: Max  N1 Step 3: Input N2 Step 4: If (N2>Max) then Max = N2 endif Step 5: Input N3 Step 6: If (N3>Max) then Max = N3 Step 7: Print “The largest number is:”,Max

Flowchart & Tracing N1 N2 N3 Max N2>Max N3>Max Step1: 5 ? ? ? ? ? Step 2: 5 ? ? 5 ? ? Step 3: 5 7 ? 5 T ? Step 4: 5 7 ? 7 T ? Step 5: 5 7 3 7 F F Step 6: 5 7 3 7 F F Step 8: Print  Largest Number is 7

Example 11: Write down an algorithm and draw a flowchart to find and print the largest of N (N can be any number) numbers. Read numbers one by one. Verify your result by a trace table. (Assume N to be 5 and the following set to be the numbers {1 4 2 6 8 })

Algorithm: Step 1: Input N Step 2: Input Current Step 3: Max  Current Step 4: Counter 1 Step 5: While (Counter < N) Repeat steps 5 through 8 Step 6: Counter  Counter + 1 Step 7: Input Next Step 8: If (Next > Max) then Max  Next endif Step 9: Print Max

START Input Max  Current Counter 1 N Y Print Max STOP Next >Max N, Current Max  Current Print Max STOP Y Counter < N N Counter 1 Counter  Counter +1 Next Next >Max Max  Next

Tracing N Current Max Next Next > Max 8 output Counter Counter < N Next Next > Max Step 1 Step 2 Step 3 Step 4 Step 5 Step 6 Step 7 Step 8 Step 9 5 1 4 6 8 8 output 2 3 T F

Prob. 1. Write an algorithm and draw a flowchart to print the square of all numbers from LOW to HIGH. Test with LOW=1 and HIGH=10. Prob. 2. Write an algorithm and draw a flowchart to print the SUM of numbers from LOW to HIGH. Test with LOW=3 and HIGH=9. Prob. 3. Write an algorithm and draw a flowchart to print all numbers between LOW and HIGH that are divisible by NUMBER. Prob. 4. Write an algorithm and draw a flowchart to print all the prime numbers between LOW and HIGH. Test with LOW=1 and HIGH=100.

Prob. 5. Write an algorithm and draw a flowchart to count and print all numbers from LOW to HIGH by steps of STEP. Test with LOW=0 and HIGH=100. Prob. 6. Write an algorithm and draw a flowchart to count and print all numbers from HIGH to LOW by steps of STEP. Test with HIGH=100 and LOW=0. Prob. 7. Write an algorithm and draw a flowchart to print the multiplication table for 6's. i.e. ---- 1  6 = 6 ---- 2  6 = 12 … ---- 12  6 = 72

Prob. 8. Write an algorithm and draw a flowchart to print the complete multiplication table for 1's. through 12's. ---- 1  1 = 1, 1  2 = 2, … 1  12 = 12 ---- 2  1 = 2, 2  2 = 4, … 2  12 = 24 … … ---- 12  1 = 12, 12  2 = 24, …12  12 = 144 Prob. 9. Write an algorithm and draw a flowchart to arrange N values read from the input in ascending order. Prob. 10. Write an algorithm and draw a flowchart that will find and print the product of 3 numbers.

Prob. 11. Write an algorithm and draw a flowchart that will find and print The factorial of NUMBER is FACTORIAL. Test the flowchart for NUMBER=5. Prob. 12. Write an algorithm and draw a flowchart that will find and print the number of vowels in a given set of characters and print there number of occurrences.