Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Computing Science and Programming I
Loops (Part 1) Computer Science Erwin High School Fall 2014.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
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.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
Introduction to Problem SolvingS1.2.1 Bina © 1998 Liran & Ofir Introduction to Problem Solving Programming in C.
Programming Training Main Points: - Working with Functions in Python
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Programming Training Main Points: - Lists / Arrays in Python. - Fundamental algorithms on Arrays.
Programming Training Main Points: - Python Statements - Problems with selections.
Prime numbers Jordi Cortadella Department of Computer Science.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
Programming for Engineers in Python Sawa 2015 Lecture 2: Lists and Loops 1.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
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.
Programming Training Main Points: - Sorting and searching algorithms on Arrays. - Problem Solving.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Programming Training kiddo Main Points: - Python Statements - Problems with selections.
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 3. Time Complexity Calculations.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 9 For Loops.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Programming Training Main Points: - More Fundamental algorithms on Arrays. - Reading / Writing from files - Problem Solving.
 See pages 65 – 67 in your book  While loops are all around us ◦ Shampoo  Rinse, Lather, Repeat  While (Condition Is True) : ◦ Execute a block of.
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
Main Points: - Python Statements - Problems with selections.
Recall: d is a divisor of n  n % d == 0
Chapter 5: Control Structure
Repetition: the for loop
Main Points: - More Fundamental Algorithms on Arrays.
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
Chapter 4 Control structures and Loops
Control Structure Senior Lecturer
Control Structure Senior Lecturer
Determinate Loops with the
CS1100 Computational Engineering
CS1100 Computational Engineering
Programming Training Main Points:
Programming Training Main Points: - Working with Functions in Python
Programming Training Main Points: - Problems with repetitions.
Programming Training Main Points: - Working with strings.
Repetition: the for loop
The Triangle Inequality
Matlab Basics.
REPETITION Why Repetition?
Presentation transcript:

Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.

C Selection. if test : line1 line2 … else : line1 line2... # endif If Block Else Block Block Start

Test if three float form a triangle. Given three real numbers a, b and c then write a Python program to test whether they form a triangle. Inputs: a,b,c – float Output: the answer either yes or not. Some points: 1. negative numbers do not form triangle 2. a, b, c are triangle sides then a+b>c and a+c>b and b+c>a

Form a right-angled triangle?

C Repetitions. C repetitions execute repeatedly a statement while a condition holds or for a number of times. while test : line1 line2 … # endwhile While loop repeats the block while test holds. If test is initially false the the block is not exeecuted. Block to repeat

C Repetitions. C repetitions execute repeatedly a statement while a condition holds or for a number of times. for elem in iterator : line1 line2 … # endfor For loop repeats statements for all elems in iterator. iterator is a set of elements that can be traversed. Block to repeat

Iterators zIterators can be given by: yrange(n)  all values between 0, 1,…, n-1 yrange(a,b)  all values between a, a+1,…,b- 1 yrange(a, b, step)  all values between a, a+step, a+2step, … zLists, strings can also be iterators

Examples of for repetition for letter in str : print(letter) for elem in list : sum = sum + elem for i in range(10) : print(i, i**2, i**3)

Examples of for repetition Suppose that there is a list a = [a[i], i=0,…,n-1]. for i in range(n) : # compute the element a[i] #end for

Find pow so that d^pow | n. Given an int number n and d one of its divisors then find what the of is in n. Some Hints: 1)d is a divisor of n when n % d == 0 2)Examples: 1)n = 100 and d = 2  pow = 2 2)n = 81 and d = 2  pow = 0 3)n = 81 and d = 3  pow = 4

Find pow so that d^pow | n. Counting problems use a counting counter cont: - initial value is 0. - count increases when the event occurs. Any time when a divisor is found we remove it from the number. Example: n=72 and k=2. pow: 0 | 1 | 2 | 3 n : 72 | 36 | 18 | 9 d : 2 | 2 | 2 | The repetition takes place while n is divisible by d: n % d ==0

Prime number decomposition If a number n is integer find the prime number decomposition. We need to find all the prime divisors together with their powers. Examples: 36 = 2**2 * 3**2 54 = 2**1 * 3** = 2**10 Hint: for a prime divisor p use the previous schme to find its power possible divisors 2,3,4,5,…, n

Prime number decomposition Repeat for d=2,3,4,5,…? - if d is a divisor of n then - find the power of d in n. - write d and pow Example: n=72 n : 72 | 9 | 1 | d : 2 | 3 | 4 | pow : 3 | 2 | Repetition ends when n<d.

Find the digits of an integer. If n is a long number then last= n % 10 is the last digit. n = n/10 is the new number from which we remove last. If we repeat removing the last digit then we can find: - all the digits of the number. - number of digits. Example: n= last:1| 4 | 9 | 2 | … n: | | | 4567 | … We repeat while the number is not fully done.

To do List 1.Read more about while and for from the e-tutorial. 2.Solve the HW problems.