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)