Tutorial 2 Control Flow: Loops NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.

Slides:



Advertisements
Similar presentations
We can think of numbers being on a number line: These numbers are all positive numbers.
Advertisements

What is the sum of the following infinite series 1+x+x2+x3+…xn… where 0
ALGORITHMS & FLOWCHARTING II
CS1010 Programming Methodology
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
Tutorial 9 String and Structure NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.
Arithmetic Sequences Section 4.5. Preparation for Algebra ll 22.0 Students find the general term and the sums of arithmetic series and of both finite.
Simple Data Type Representation and conversion of numbers
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Notes Over 11.4 Infinite Geometric Sequences
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Tutorial 5 Arrays Basics (1D, 2D) NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.
Series Ch. 13.
13.3 – Arithmetic and Geometric Series and Their Sums Objectives: You should be able to…
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
4.7 Define & Use Sequences & Series. Vocabulary  A sequence is a function whose domain is a set of consecutive integers. If not specified, the domain.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Computer Organization and Design Information Encoding II Montek Singh Wed, Aug 29, 2012 Lecture 3.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
Counting Loops.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Figure out how to work with infinite series when i=0 vs i=1 Slide 12.
Arithmetic Sequences Sequence is a list of numbers typically with a pattern. 2, 4, 6, 8, … The first term in a sequence is denoted as a 1, the second term.
{ 12.3 Geometric Sequence and Series SWBAT evaluate a finite geometric series SWBAT evaluate infinite geometric series, if it exists.
Control flow Ruth Anderson UW CSE 160 Spring
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
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.
Control flow Ruth Anderson UW CSE 160 Winter
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Copyright © Cengage Learning. All rights reserved. Sequences and Series.
13.5 – Sums of Infinite Series Objectives: You should be able to…
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Mealy Machines Finite State Machines with Outputs given on the transitions.
Nested Loops CS303E: Elements of Computers and Programming.
Repetitive Structures
‘C’ Programming Khalid Jamal.
Chapter 6: Loops.
Chapter 4 Operations on Bits.
Python Loops and Iteration
Start reading Sec and chapter 7 on loops
11.3 – Geometric Sequences and Series
13.3 – Arithmetic and Geometric Series and Their Sums
Tutorial 8 Pointers and Strings
Lecture 07 More Repetition Richard Gesick.
Ruth Anderson UW CSE 160 Winter 2017
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
Patterns, Patterns, and more Patterns!
Sequences and Series 4.7 & 8 Standard: MM2A3d Students will explore arithmetic sequences and various ways of computing their sums. Standard: MM2A3e Students.
Loops in C.
Ruth Anderson UW CSE 140 Winter 2014
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Sequences and Series.
Suppose I want to add all the even integers from 1 to 100 (inclusive)
CS150 Introduction to Computer Science 1
Tutorial 6 Array Problem Solving
EE 194/Bio 196: Modeling biological systems
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Tutorial 2 Control Flow: Loops NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Quick Summary 2 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 3 Trace the program shown on the left for i = 0 to i = 30; What is the output if n = 321; CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 4 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 5 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 6 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 7 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 8 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 9 count 2 count 3 count 5 i n ? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q1: Program Tracing 10 We then observe that: count5: numbers divisible by 5 count3: numbers divisible by 15 count2: numbers divisible by 2 BUT not divisible by 10! CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO We observe that: count5 counts for:0,5,…,320 count3 counts for:0,15,…,315 count2 counts for:2,4,6,8,12,…,

Q2: Program Tracing 11 Given the program on the right, what do you think will happen? What will actually happen? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q2: Program Tracing 12 We observe that: The value of i will change in the following pattern: 1, 2, 3, …, …, …, All values satisfy i>0, the program will end in infinite loop. However… CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q2: Infinite Loop & Data Type Range 1.The program ends up in value after a short while. 2.Integer data type has a finite range on your computer, from to Incrementing from causes overflow and it will immediately jump to the negative extreme CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q3: Sum of Sequence and Series 14 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q3: Sum of Sequence and Series 15 Sum = … + nSum = n + (n-1) + … CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q3: Sum of Sequence and Series 16 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO while(n--){…} This structure is frequently used as a simple loop to repeat n times. Important: you can only use this method if you value of n is not used again later on.

Q3: Sum of Sequence and Series 17 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO Note: Value of “i”: 1, 3, 5, …, Value of “j”: 1, -1, 1, -1, …, Value of “i*j”: 1, -3, 5, -7, …,

Q3: Sum of Sequence and Series 18 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO 4.0*j/i: Differentiate between integer division and floating point division: 4/5 = 0; 4.0/5 = ;

Q3: Sum of Sequence and Series 19 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q4: Printing 2D Matrix 20 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q4: Printing 2D Matrix 21 ij j j j j CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q4: Printing 2D Matrix 22 %4d: The output number will take up altogether 4 spaces. If the number need more than 4 spaces, just print normally. If the number need less than 4, add blank spaces in front. Challenge: How to print inverted triangle? CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q5: Prime Number Test 23 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO

Q5: Prime Number Test 24 Exhaustively test from 2 to k Check end configuration, if “i <= k”, it means it did not pass through all tests. CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO