While Loop Design ENGI 1020 Fall 2018.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Write a function to calculate the cubic function: y = 4x 3 + 2x 2 –5x – 4 The function should return y for any given value of x. Question One #include.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 7.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
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.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Loops. About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Topic 6 Recursion.
Introduction to Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
C++ Programming: CS150 For.
Chapter 2 Assignment and Interactive Input
Andy Wang Object Oriented Programming in C++ COP 3330
Arithmetic & other operations
Chapter 2.2 Control Structures (Iteration)
Chapter 4 Loops Case Studies
Lecture 4B More Repetition Richard Gesick
Arrays & Functions Lesson xx
Introduction to Functions
1020: Introduction to Programming Mohamed Shehata November 22, 2017
Pointers & Functions.
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Let’s all Repeat Together
Loops.
CS1201: Programming Language 2
Week 4 Lecture-2 Chapter 6 (Methods).
Repetition Statements (Loops) - 2
Building Java Programs
Pointers & Functions.
CS1201: Programming Language 2
Building Java Programs
Building Java Programs
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
REPETITION Why Repetition?
Methods and Data Passing
Presentation transcript:

While Loop Design ENGI 1020 Fall 2018

Example to trace the loop void foo(){ int i=7; while (i>0){ if( i%2 != 0 ) cout<<i<<endl; i--; } cout<<"goodbye!"; What is printed on the screen? Be very detailed! Hint: 15%2=1 (the modulo operator returns the remainder)

Solution void foo(){ int i=5; while (i>0){ if( i%2 != 0 ) cout<<i<<endl; i--; } cout<<"goodbye!"; i

While Loop Design The general case: The special cases: What should be done in the body? The special cases: Under what condition should the iteration stop? How should the loop control condition be initialized? How should the loop control condition be updated? How should other variables be initialized? How should other variables be updated? What is the state when the loop exits? long factorial(int num){ long fact = 1; while (num > 1) { fact *= num; num--; } return fact; } General case

Design loops from the inside out (from the general to the specific). Note the implication here is that first design the loop, then code the loop

Simple design example We want to print the numbers from 5 to 9 on the screen Design process: We start with number 5, then print number then we increment the previous printed number, then print number then increment previous printed number, then print number then increment previous printed number, and so on until we print 9 - Obviously this is a repeated task I got it – Let’s have a variable, make a loop and inside the loop: print number then we increment the previous printed number Integer i=5 While ( i<=9 ) { print the variable increment the variable } Define a variable While ( condition ) { print the variable increment the variable }

void printNumbers(){ int i=5; while (i<=9){ cout<<i; i++; }

Exercise Write a function that adds all the numbers from 5 to 9 Modify this code void printNumbers(){ int i=5; while (i<=9){ cout<<i; i++; } int addNumbers( ){ int sum=0; int i=5; while (i<=9){ sum+=i; i++; } return sum; To write this function int addNumbers( ){

Summary int addNumbers( ){ int sum=0; int i=5; while (i<=9){ sum+=i; i++; } return sum; Identify the principles we discussed on this function you just wrote: The general case: What should be done in the body? The special cases: Under what condition should the iteration stop? How should the loop control condition be initialized? How should the loop control condition be updated? How should other variables be initialized? How should other variables be updated?

Homework Repeat the same previous example but add only the odd numbers between 5 and 9 (sum = 5+7+9) Hint: how i should be updated?

Complicated Example Write a program in C++ to find the sum of the first 10 terms of the following series: sum = (1/1!) - (2/2!) + (4/4!) - (8/8!) …………. (n/n!) where number of terms=10 while loop condition i<10 define sum, term, i=0 while loop( ) { evaluate the current term Add the current term to sum increment i } Print the Sum 1, 2, 4, 8, … is equivalent to 2(i) (that’s why we start at i=0) Also notice the sign is changing - The sign can be done using (-1) (i) The general form of a term is then: (−2) 𝑖 (2) 𝑖 ! ½ of the lecture (25 minutes have passed)

Example 2 – Cont’d double term=0.0; double sum=0.0; int i=0; We need a factorial function 𝑥! long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; } return result; define sum, term, i While ( i<10 ) { current term = (-2)(i) / factorial( (2)(i) ) Add current term to sum increment i } Print the Sum pow(-2,i)

Putting All Together long factorial (int x); main function { double sum=0.0; double term=0.0; int i=0; while (i<10 ) current term = (-2)(i) / factorial( (2)(i) ) Add current term to sum increment i } Print the Sum long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; return result;

Writing the code #include <iostream> #include <math.h> using namespace std; long factorial(int x); int main() { double sum=0.0; double term=0.0; int i=0; while (i<10) { term=pow(-2,i) / factorial(pow(2,i)); sum += term; i++; } cout << "The sum of 10 terms is: " << sum ; return 0; long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; return result; long factorial (int x); main function { double sum=0.0; double term=0.0; int i=0; while (i<10 ) current term = (-2)(i) / factorial( (2)(i) ) Add current term to sum increment i } Print the Sum long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; return result;