Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Similar documents
Chapter 8 Algorithms 1

8 Algorithms 8.1. Foundations of Computer Science Cengage Learning

STUDENT LESSON A12 Iterations

Algorithms. Chapter 8. Objectives After studying this chapter, students should be able to:

Repetition Algorithms

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

SNS COLLEGE OF ENGINEERING,

Programming for Experimental Research. Flow Control

Logic & Algorithms Foundations of Computer Science Behrouz A. Forouzan, Brooks/Cole Thomson Learning, Pacific Grove, USA, 2003.

Reviewing all Topics this term

REPETITION CONTROL STRUCTURE LOGO

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CS112 Lecture: Repetition Statements

Information Science 1

SNS COLLEGE OF ENGINEERING

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Chapter 5: Control Structures II (Repetition)

Information Science 1

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Problem Solving and Algorithms

REPRESENTING ALGORITHMS. REPRESENTING ALGORITHMS IB DP Computer science Standard Level ICS3U

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Loops / Repetition Statements

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

PROBLEM SOLVING WITH LOOPS. Chapter 7

Theory of control structures

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

C Programming for Engineers Structured Program

Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers

Programming in C++ PART 2

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

CSCE150A. Introduction. While Loop. Compound Assignment. For Loop. Loop Design. Nested Loops. Do-While Loop. Programming Tips CSCE150A.

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 5. Repetition in Programs. Notes. Notes. Notes. Lecture 05 - Loops

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

Algorithms (continued)

Top Down Design. 2. Design Methodology

Variable A variable is a value that can change during the execution of a program.

Problem Solving with Decisions. T.Fatin Alhila

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

In this chapter you will learn:

Condition Controlled Loops. Introduction to Programming - Python

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Quiz Determine the output of the following program:

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 2 - Problem Solving

Why Is Repetition Needed?

CS112 Lecture: Loops

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

III. Check if the divisors add up to the number. Now we may consider each of these tasks separately, assuming the others will be taken care of

Computer System and programming in C

C++ Programming Language Lecture 2 Problem Analysis and Solution Representation

Algorithms, Part 2 of 3. Problem Solving. Problem Solving (con t) Topics Problem Solving Examples Pseudocode Control Structures

Outline. Program development cycle. Algorithms development and representation. Examples.

Algorithms, Part 2 of 3. Topics Problem Solving Examples Pseudocode Control Structures

Loops (while and for)

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

depicts pictorially schematic representation of an algorithm document algorithms. used in designing or documenting

Steps to program development

Chapter 1 Lab Algorithms, Errors, and Testing

IT 1033: Fundamentals of Programming Loops

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Final Examination Semester 2 / Year 2010

CS1004: Intro to CS in Java, Spring 2005

Programming Logic and Design Sixth Edition

Chapter 2: Functions and Control Structures

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Backtracking. Examples: Maze problem. The bicycle lock problem: Consider a lock with N switches, each of which can be either 0 or 1.

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

Microsoft Visual Basic 2005: Reloaded

Laboratory 5: Implementing Loops and Loop Control Strategies

CSCE 206: Structured Programming in C++

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Class 8 ALGORITHMS AND FLOWCHARTS. The City School

Developing Algorithms

BIL101E: Introduction to Computers and Information systems Lecture 8

A Quick Review of Chapter 1

The sequence of steps to be performed in order to solve a problem by the computer is known as an algorithm.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Mathematical Computing

Numerical Methods in Scientific Computation

Part III Appendices 165

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

Pseudocode syntax, descriptions and examples

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Loops / Repetition Statements

Final Examination Semester 3 / Year 2010

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Chapter 1 - An Introduction to Computers and Problem Solving

Transcription:

Algorithms 1. Objectives... 2 2. Design You Solution... 2 3. Structure of an algorithm:... 3 4. Pseudocode:... 4 5. Example... 5 6. Selection or Conditional Execution... 8 7. Looping or Iteration... 9 8. Questions/Practice:... 12 Abdelghani Bellaachia, CSCI 1121 Page: 1

1. Objectives Introduce you to the world of programming Programming constructs A good design for your program will save you an incredible amount of time testing at the computer. 2. Design You Solution The following steps are used in designing a large program or a simple function or procedure: 1. Define the problem. o o Describe the input data available (when the program starts), Describe the output (result) desired, 2. Plan a solution (design the program). This stage often uses one of two ways to describe the solution: 1. pseudocode - An English-like structured language, or Abdelghani Bellaachia, CSCI 1121 Page: 2

2. A flow chart - a pictorial representation of the step-by-step solution to the problem. 3. Code the program: Translate the logic from the flow chart/pseudocode into a programming language. 4. Test the program: Run a number of test cases through the program to demonstrate that it works. 5. Document the program: This is the time for a formal document called the user manual to be produced. Most of the program code should already contain adequate comments for documentation in the coding phase. 3. Structure of an algorithm: Your algorithm consists of the following: o A name of your program Abdelghani Bellaachia, CSCI 1121 Page: 3

o A set of inputs o A mark that indicates the beginning of the set of executable statements A set of executable statements o A mark that indicates the end of your program. 4. Pseudocode: It consists of short, English phrases used to explain specific tasks within a program's algorithm. It is a "text-based" detail (algorithmic) design tool. One programmer should be able to take another programmer's pseudocode and generate a program based on that pseudocode. Why is pseudocode necessary? The programming process is a complicated one. You must first understand the problem Abdelghani Bellaachia, CSCI 1121 Page: 4

5. Example specifications, of course, then you need to organize your thoughts before creating your program Writing pseudocode WILL save you time later during the construction & testing phase of a program's development. Write a program that obtains three numbers from the user. It will print out the sum of those numbers. o Pseudocode: Ask the user to enter the first integer. Obtain user's first integer input. Ask the user to enter a second integer. Obtain user's second integer input Ask the user to enter a second integer. Obtain user's third integer input. Compute the sum of the three user inputs. Abdelghani Bellaachia, CSCI 1121 Page: 5

Display an output prompt that explains the answer as the sum Display the result. Sequences: Straight-Line Algorithms o A sequence refers to a set of statements separated by a delimiter, i.e., semicolon. The program enters the sequence, execute from the top to the bottom each statement, until the end of the sequence Example: Ask a user to enter the price of an item being purchased at a store and calculate and display the final price including sales tax. Let us assume the sales tax is 4.5%. o Pseudocode: Ask the user for the price. Read the price into the variable x. Compute the tax value y as follows: y = 0.045 * x; Compute the final price: Abdelghani Bellaachia, CSCI 1121 Page: 6

total = x + y Display total. Abdelghani Bellaachia, CSCI 1121 Page: 7

6. Selection or Conditional Execution Selection structures are used to check conditions and specify actions based upon those conditions or to choose an action from multiple options. Examples of selections: o One-way selection: if (condition(s)) the Sequence end if; o To-way selection (or binary selection): if (condition(s)) the Sequence else Sequence end if; Example: Compute the grade letter of a given course according the following chart: Grade Grade Letter 90-100 A 80-89 B 70-79 C 65-69 D 64-below F Abdelghani Bellaachia, CSCI 1121 Page: 8

7. Looping or Iteration A common requirement in computing is to repetitively perform the same, or very similar, sequence many times. Repetition is achieved using a loop: o A statement that causes the computer to repeat the same sequence of code many times. Looping statements almost invariably include a condition, since it pays to stop repeating yourself eventually. Infinite loop: o It is a loop that never stops o It is usually a bug. However there are always exceptions. Two types of loops: o Defined loop: The number of iterations is defined ahead of time. o Undefined loop: The number of iterations is not known in advanced. Abdelghani Bellaachia, CSCI 1121 Page: 9

Examples of looping: o For loop, o While loop, o Nested loops, o Etc. Examples: o Print a number n of Hellos where n is a user input values. Algorithm 1: - Comments: this pseudocode uses the following variable: Variable n is used to store the user input. - Ask the user to input a value - Read the user input in n - Repeat n times Print ( hello ); - end repeat; Algorithm 2: - Comments: this pseudocode uses the following variables: Variable n is used to store the user input. Variable i is used to control the number - Ask the user to input a value - Read the user input in n - For i=1 to n do Print ( hello ); - end for; o Compute the average number of a set of input values entered by the user. The set of inputs is ended by 1. Algorithm: Abdelghani Bellaachia, CSCI 1121 Page: 10

Comments: We are going to use the following variables: variable total to store the sum of all user input values. Initially it is set to zero. variable count to store the total number of user inputs. Initially it is set to zero. variable average to store the average of user input values. Initially it is set to zero. Start the program: - Ask the user to input a value; - Read the user input in x; - while x is different from 1 do add the value of x to total: total=total+x; count=count+1; ask the user to input a value; read the user input in x; - end of while - Compute the average as follows: - average=total/coun t; - print average on the screen; - End of program Abdelghani Bellaachia, CSCI 1121 Page: 11

8. Questions/Practice: Write the pseudocode of a program that computes the sum of all multiples of 10 between 1 and 1000 (10, 20, ). Your pseudocode should also print each multiple of 10. Write the pseudocode of a program that computes the sum of all even numbers between 1 and 1000 (0, 2, 4, ). Write the pseudocode of a program that adds only negative numbers within a set of user input numbers. The processing stops when the user enters 999. Write the pseudocode of a program that computes the maximum number of a set of user input numbers. The processing stops when the user enters 1. Abdelghani Bellaachia, CSCI 1121 Page: 12