Repetition Control Structure in C++ Program

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Computer Science 1620 Programming & Problem Solving.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© 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.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Sequences Jordi Cortadella Department of Computer Science.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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,
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Overview Go over parts of quiz? Another iteration structure for loop.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Branching statements.
Introduction to Computer Programming
Repetitive Structures
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
while Repetition Structure
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
C++ Programming: CS150 For.
Engineering Problem Solving with C++, Etter/Ingber
Introduction to C++ October 2, 2017.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Summary Two basic concepts: variables and assignments Basic types:
Computing Fundamentals
Lecture Notes – Week 4 Chapter 5 (Loops).
Control Structures Part 1
Computer programming Lecture 3.
By Hector M Lugo-Cordero September 3, 2008
2.6 The if/else Selection Structure
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
REPETITION Why Repetition?
Presentation transcript:

Repetition Control Structure in C++ Program CSC1110C Introduction to Computer Programming Tutorial 3 Repetition Control Structure in C++ Program By Josh Chen

Outline Repetition Control Structure Example 1: Fibonacci Numbers Example 2: Prime Number

Repetition Control Structure (1) Repeat actions while certain condition remain true while (condition){ //actions executed repeatedly //while condition is true } //exit the loop when condition becomes false Actions might not be executed if condition is false from the beginning

Repetition Control Structure (2) do { //actions executed repeatedly //while condition is true } while (condition) //exit when condition becomes false Actions will be executed at least once

Repetition Control Structure (3) for (initialization; loop testing; increment) { //actions executed repeatedly //if loop testing passed } //exit when loop testing fail Declare loop variable in the loop initialization (not accessible outside the for loop) Avoid changing loop variable inside loop actions

Fibonacci Numbers (1) Sequence of non-negative integers The first element is 0; the second is 1 Each element equals to the sum of the preceding two elements F(0) = 0; F(1) = 1; F(2) = F(0) + F(1) = 1; F(3) = F(1) + F(2) = 2; F(4) = F(2) + F(3) = 3; F(5) = F(3) + F(4) = 5; … … … … … … F(100) = 354224848179261915075 // grow pretty fast

Fibonacci Numbers (2) fibonacci.cpp: print out the first n elements of Fibonacci sequence; n is input by user Pseudocode of fibonacci.cpp Read n if ( n is bigger than 1) initialize and print the first two Fibonacci numbers for ( i increase from 2 to n-1) calculate the ith Fibonacci number if ( the ith Fibonacci number is negative) print error message to inform overflow break the loop end if print the ith Fibonacci number end loop

Fibonacci Numbers (3) fibonacci.cpp: /* The program print out the first n elements of Fibonacci numbers. n is input by the user. */ #include <iostream> int main() { using namespace std; int n; int grandfather, father, son; cout << "Please input the number of the Fibonacci numbers (n>1): " ; cin >> n; … … …

Fibonacci Numbers (4) … … … if ( n > 1 ) { grandfather = 0; // the first element father = 1; // the second element cout << "F(0)" << " = " << 0 << endl; cout << "F(1)" << " = " << 1 << endl; for (int i = 3; i <= n ; i++) son = grandfather + father; if ( son < 0 ){ cout << "Data overflow ! " << endl; break; } cout << "F(" << i-1 << ")" << " = " << son << endl; grandfather = father; father = son; return 0; } // end of main()

Fibonacci Numbers (5) Alternative 1: int counter = 3; … … … int counter = 3; while (counter <= n){ son = grandfather + father; if ( son < 0 ){ cout << "Data overflow ! " << endl; break; } cout<<"F("<<(counter++)-1<<")"<<" = "<<son<<endl; grandfather = father; father = son;

Fibonacci Numbers (6) Alternative 2: … … … int counter = 3; do{ son = grandfather + father; if ( son < 0 ){ cout << "Data overflow ! " << endl; break; } cout <<"F("<<(counter++)-1<<")"<<" = "<<son<< endl; grandfather = father; father = son; } while( counter <= n); Error: unexpected result when n = 2 ! ( see next slide )

Fibonacci Numbers (7)

Fibonacci Numbers (8)

Fibonacci Numbers (9) F(47) = 2971215073 > 2147483647 (biggest int type data)

Prime Number (1) A positive integer not divisible without a remainder by any whole number other than itself and one Smallest prime number 2, 3, 5, 7, 11, 13, … … …, 123457, 123479, … … … Algorithm to test whether a number is a prime number: 1) 2 is a prime number 2) For a positive integer n, n > 2 if n is not divisible by any whole number between 2 and sqrt(n), it is a prime number; otherwise, it is a composite number;

Prime Number (2) Pseudocode of judeprime.cpp judgeprime.cpp: repeatedly read in a positive integer number >= 2 and tell whether it is a prime number. If it is a composite number, give a possible decomposition. Program will exit when user input a number smaller than 2. Pseudocode of judeprime.cpp Read in a integer - number while (number is bigger than 1) root equals the square root of number if ( number is 2) print out : 2 is a prime number otherwise test whether number is a prime number if (number is prime) print out: number is prime print out a possible decomposition read in the next integer end while

Prime Number (3) judgeprime.cpp: jump out of the loop … … … cout << "Please input a positive integer number bigger than 1: "; cin >> number; while (number > 1){ root = static_cast<int>(std::sqrt(static_cast<double>(number))); if( number == 2) cout << number << " is a prime number." << endl; else{ isPrime = true; for(int i = 2;i <= root; i++){ if( number % i == 0 ){ isPrime = false; factor1 = i; factor2 = number/i; break; } if ( isPrime ) else cout << number << " = " << factor1 << 'x' << factor2 <<" , is a composite number." << endl; jump out of the loop

biggest int type data (4 bytes) Prime Number (4) biggest int type data (4 bytes)

Prime Number (5) nextprime.cpp: read in a positive integer nubmer, calculate the smallest prime number bigger than the input number rewrite judge.cpp into a function: boolean testPrime(int value) //return true if value is a prime, false otherwise nextprime.cpp: … … … cout<< "Please input a positive integer number bigger than 1:"; cin >> number; nextPrime = number + 1; while( nextPrime > 0 && !testPrime( nextPrime ) ){ nextPrime ++; } ... … …

Prime Number (6)

Prime Number (7) More interesting topics on Prime Numbers: 1) Find “Twin Prime Numbers” Twin Prime – a pair of prime numbers with their difference equal to 2 such as (3, 5) and (11, 13) 2) The Largest Known Prime ( Mersenne 41; Announced May 28, 2004)