Introduction to Algorithms - 2

Slides:



Advertisements
Similar presentations
Representing an algorithm using Flowcharts
Advertisements

Exercise (1).
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
1 Flowchart. 2 Flowchart: It is a diagram consists of symbolic block that represents the algorithm step by step Symbols of flowchart: 1. Terminal (start,
PSEUDOCODE & FLOW CHART
ALGORITHMS & FLOWCHARTING II
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
ALGORITHMS AND FLOWCHARTS
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Flowcharts.
PSEUDOCODE C Programming Technique – Firdaus-Harun.com.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Basic Control Structures
Count and add list of numbers From user input and from file.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
LESSON 1 Introduction to Programming Language. Computer  Comprised of various devices that are referred to as HARDWARE.  The computer programs that.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Mean Mean  = Sum of all the values The number of values Eg. Find the mean of the following values: 2, 5, 4, 7, 5, 7, 8, 4, 5, 6, 12, 2, 4, 5, 7, 5 Mean.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Programming – Algorithms (Flowcharts)
Algorithm & Flowchart.
CS1010 Programming Methodology
IGCSE 1 Cambridge Algorithms and flowcharts Unit 7 Computer Science
REPETITION CONTROL STRUCTURE
while Repetition Structure
for Repetition Structures
Computer Programming Flowchart.
CHAPTER 5A Loop Structure
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
ALGORITHMS & FLOWCHARTING II
Repetition-Counter control Loop
Computer Science 101 While Statement.
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
Siti Nurbaya Ismail Senior Lecturer
Logical Operators and While Loops
ALGORITHMS AND FLOWCHARTS
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structure Senior Lecturer
PROBLEM SOLVING CSC 111.
Algorithms & Pseudocode
Chapter 8 The Loops By: Mr. Baha Hanene.
ALGORITHMS AND FLOWCHARTS
Control Structures Repetition
Introduction to Algorithms and Programming
Faculty of Computer Science & Information System
CS150 Introduction to Computer Science 1
Introduction to Algorithms - 1
Introduction to Programming – 4 Operators
Let’s all Repeat Together
Flowchart.
CHAPTER 4 Iterative Structure.
Flowcharts and Pseudo Code
Logical Operators and While Loops
The structure of programming
Control Structures Selection
Introduction to Algorithms - 2
Presentation transcript:

Introduction to Algorithms - 2 Dr. Khizar Hayat Associate Prof. of Computer Science

What can a computer do An algorithm can be written using six basic computer operations: A computer can receive (input) information, e.g. read name get x A computer can display (output) information, e.g. write “the average is “, avg display num print “Hello!” A computer can perform arithmetic operations, e.g. set total total + numb A computer can assign value to a variable, e.g. set counter 0 set total  price+tax

What can a computer do A computer can compare two pieces of information to select one of two actions, e.g. if number < 0 then add 1 to neg_number else add 1 to pos_number end if A computer can repeat a group of actions, e.g. repeat untill total=50 read num write num set totaltotal+1 End repeat OR while total ≤50 End while

Algorithm to add two numbers begin get num1 get num2 set sum  num1 + num2 display sum end Start Read Num1 Read Num2 Sum Num1 + Num2 What about: The difference of two numbers? The sum and difference of two numbers? Display Sum Stop

Algorithm to input two numbers and print the smaller number Start begin get num1 get num2 if num1 < num2 then display num1 as smaller else display num2 as smaller end if end Read Num1 Read Num2 Num1 < Num2? No What about: The largest of two numbers? The largest of three numbers? The median of three numbers? Yes Display Num1 as min Display Num2 as min Stop

Algorithm to sum five numbers input by the user Start count 0 begin set count0 set sum0 repeat until count=5 get num set countcount+1 set sumsum+num end repeat display sum as the total end sum 0 Read Num count count + 1 sum sum + Num count = 5? No What about: Sum of N numbers? Average of N numbers? Entering numbers till ‘-1’ is entered? The sum of first 20 numbers? Yes Display sum as total Stop

Algorithm to compute the GCD of two numbers begin get numA get numB if numA > numB then set maqsnumA set m_AlaihnumB else set maqsnumB set m_alaihnumA end if set baqimaqs mod m_alaih while baqi ≠ 0 do set maqsm_alaih set m_alaihbaqi end while display m_alaih as GCD end Display M_Alaih as GCD Stop Maqs NumA M_Alaih NumB Start Read NumA Read NumB NumA > NumB? Yes No Maqs NumB M_Alaih NumA Baqi = 0? Maqs M_Alaih M_Alaih baqi Baqi Maqs mod M_Alaih Euclid’s Algorithm

Exercises Draw a flowchart that reads names of 50 students in the class and display it. Page 16-17 of notes Page 21 of notes Any sum in Omani currency?