Algorithms and Problem Solving

Slides:



Advertisements
Similar presentations
Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes.
Advertisements

Exercise (1).
PROBLEM SOLVING TECHNIQUES
Chapter 2- Visual Basic Schneider1 Chapter 2 Problem Solving.
Java Planning our Programs Flowcharts Arithmetic Operators.
CS1010 Programming Methodology
LECTURE 1 CMSC 201. Overview Goal: Problem solving and algorithm development. Learn to program in Python. Algorithm - a set of unambiguous and ordered.
INTRODUCTION TO PROGRAMMING
1 More on Decisions Overview l The selection Operator l Grouping of Statements l Multiple branches (if else if) l Switch Statement.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
1 Repetition structures Overview while statement for statement do while statement.
1 Pertemuan 01 Algorithm Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
Unit 171 Algorithms and Problem Solving - II Algorithm Efficiency Primality Testing Improved Primality Testing Sieve of Eratosthenes Primality Testing.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection Statement 4.6 if else Selection Statement 4.7 while.
1 Algorithms and Problem Solving. 2 Outline  Problem Solving  Problem Solving Strategy  Algorithms  Sequential Statements  Examples.
Review Algorithm Analysis Problem Solving Space Complexity
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Chapter 1 Pseudocode & Flowcharts
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
ALGORITHMS AND FLOWCHARTS
The University of Texas – Pan American
Chapter 2: Problem Solving
Introduction to Programming Lecture Number:. What is Programming Programming is to instruct the computer on what it has to do in a language that the computer.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Algorithmic Problem Solving CMSC 201 Adapted from slides by Marie desJardins (Spring 2015 Prof Chang version)
Describe the Program Development Cycle. Program Development Cycle The program development cycle is a series of steps programmers use to build computer.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
Software Life Cycle What Requirements Gathering, Problem definition
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
1 Program Planning and Design Important stages before actual program is written.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
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.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
FUNDAMENTALS OF ALGORITHMS MCS - 2 LECTURE # 2. MODEL OF COMPUTATION REPRESENTATION OF ALGORITHMS.
Chapter 7 Problem Solving with Loops
The Hashemite University Computer Engineering Department
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Flow Charts And Pseudo Codes Grade 12. An algorithm is a complete step-by- step procedure for solving a problem or accomplishing a task.
CSE 110: Programming Language I Matin Saad Abdullah UB 404.
 Problem Analysis  Coding  Debugging  Testing.
CS1010 Programming Methodology
PROGRAM CONTROL STRUCTURE
Lecture 3 of Computer Science II
Lecture 2 Introduction to Programming
ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Algorithm Algorithm is a step-by-step procedure or formula or set of instruction for solving a problem Its written in English language or natural language.
Repetition-Counter control Loop
Introduction to Computer Programming
Chapter 4 – Control Structures Part 1
ALGORITHMS AND FLOWCHARTS
Unit# 9: Computer Program Development
Algorithms & Pseudocode
Java Programming Loops
1) C program development 2) Selection structure
ALGORITHMS AND FLOWCHARTS
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Java Programming Loops
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
WJEC GCSE Computer Science
Presentation transcript:

Algorithms and Problem Solving Introduction What is an Algorithm? Algorithm Properties Example Exercises Unit 16

What is an Algorithm? What is an Algorithm? An algorithm is a precisely defined and ordered sequence of instructions that is guaranteed to solve a specific problem. The algorithm must be general, that is, it should solve the problem for all possible input sets to the problem. If there is a particular output for an input, we say that the algorithm can be applied to this input and process it to give the corresponding output. The word “algorithm” is derived from the name of Muhammad ibn Musa-al-Khwarizmi, a Muslim mathematician whose works introduced algebraic concepts. Unit 16

Properties of Algorithms An algorithm should have the following properties: No ambiguity in any instruction No ambiguity which instruction is executed next Finite number of instructions Execution must halt and produce a result No ambiguity in any instruction: The instructions in an algorithm must be precisely defined, so that any person or machine implementing the instructions must have no difficulty in implementing them. For example, an instruction: “Adjust the temperature to suitable level” is ambiguous and not precisely defined. Unit 16

Properties of an Algorithm – Contd. No ambiguity which instruction is executed next: The order in which instructions in algorithm must be executed should also be well defined and not ambiguous. Again, we can say that the ordering should be such that any person or machine implementing the algorithm must have no difficulty in implementing them. Finite number of instructions: The number of instructions in an algorithm must be finite so that implementation by hand or by machine takes a finite amount of time. Execution must halt and produce a result: Finally, an algorithm must halt and produce an output. Procedures that loop on forever on their inputs are not algorithms in the strict sense of the term. Unit 16

Representation of Algorithms Pseudocode (Pseudo = not real; false, Code = Program fragment) is a generic way of describing an algorithm using the conventions of programming languages, without using language-specific syntax. Pseudocode generally does not actually use the syntax of any particular language There is no systematic standard form, although any particular writer will generally borrow the appearance of a particular language. Details not relevant to the algorithm (such as memory management code) are usually omitted. The programming language will be augmented with natural language where convenient (for example, for trivial operations such as swapping two variables). Unit 16

Flow Charts Another way to represent algorithms is by using flowcharts. Flowcharts can be thought of as a graphical form of pseudocode. A flowchart (also spelled flow-chart and flow chart) is a schematic representation of a process. Generally the start point, end points, inputs, outputs, possible paths and the decisions that lead to these possible paths are included. Various flowchart boxes are shown below: Decision Begin/End Data Initialization Action Connector Unit 16

Example 1 Develop an algorithm to find the average of a list of student scores. Solution: The problem statement doesn’t specify the number of students, so we assume that a sentinel value of -1 for score indicates the end-of-input. The algorithm is straightforward. We keep inputting the score of each student. For each valid score entered we increment the variable count indicating the number of students. For each valid score entered, we add the score to another variable called sum. If a score is entered with a value of -1, it indicates the end of input so we stop inputting further scores. The average is equal to total/count. But what happens if the user didn’t enter any valid score. For example his first entered value is -1? In that case if calculate total/count, it will lead to a division by zero error. So we say that if count is equal to zero, then the average is also zero. Unit 16

The Pseudocode version The algorithm is straightforward: 1. Initialize score, total, count and average to zero. 2. Input student score 3. If score = -1 then go to step 7. 4. Add score to total 5. Increment count by 1. 6. Go to step 2. 7. If count ≠ 0 average = total/count. 8. Output average. Unit 16

Example 1 – Contd. The algorithm appearing in the previous slide was in pseudo-code that was informal. A flowchart version is shown below: Start count = 0 ? Yes score = 0, total = 0, count = 0, average = 0 No Input score Average = total/count score = -1 ? Yes Output average No score = score + total End count = count + 1 Unit 16

Example 1 - Conversion to Java Program Below we show the Java program corresponding to Example 1. Observe the modifications made to the pseudocode. import java.io.*; public class Sentinel { public static void main(String[] args) throws java.io.IOException BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); double total = 0, avg = 0; int score = 0, count = 0; System.out.print("Enter the score (or -1 to Quit): "); score = Integer.parseInt(stdin.readLine()); while (score != -1) total += score; count += 1; score=Integer.parseInt(stdin.readLine()); } if (count != 0) avg = total/count; System.out.println("Average Grade = "+avg); Unit 16

Exercises Develop an algorithm to check if a given integer is a perfect square or not. Give both the pseudocode version and the flowchart version. Convert your pseudocode into a Java program. 2. Develop an algorithm to display all prime numbers from 2 to 100. Give both the pseudocode version and the flowchart version. Convert your pseudocode into a Java program. 3. Develop an algorithm to find the maximum and minimum value from a list of integers. Give both the pseudocode version and the flowchart version. Convert your pseudocode into a Java program. Unit 16