Presentation is loading. Please wait.

Presentation is loading. Please wait.

FOR LOOPS.

Similar presentations


Presentation on theme: "FOR LOOPS."— Presentation transcript:

1 FOR LOOPS

2 for (initialize counter ; test counter ; update counter )
For Loops Structure for (initialize counter ; test counter ; update counter ) statement; for (initialize counter ; test counter ; update counter ) { statement; } The for loop structure starts with the keyword for then between brackets you have your header where you initialize your counter, have your termination condition, and the update of the counter

3 Factorial with while loop
/***** factorial **************************************** num - a positve integer: number > 0 * the factorial of num ***********************************************************/ long factorial(int num){ long result= 1; int i=1; while (i <= num) { result *= i; i++; } return result; }

4 Factorial /** factorial ************************************************** * num must be a non-negative integer * the factorial of num ****************************************************************/ long factorial(int num){ long result= 1; for (int i = 1; i <= num; i++) result *=i; return result; } long factorial(int num){ long result= 1; int i=1; while (i <= num) { result *= i; i++; } return result; }

5 for (i = 1; i <= num; i++) fact *= i;

6 * * * 3 3 2 1 3 False True True True Execution Example int finalValue;
cin >> finalValue; for (int counter = 0; counter < finalValue; counter++) cout << "*"; False True True True Display Screen 3 finalValue counter 3 2 1 3 * * *

7 Common Errors How many times this loop will execute Logical Error
Print all numbers between 50 and 60 inclusive for(int i = 50 ; i < 60 ; i++) { cout<< i << endl; } Off-By-One Logical Error for(int x=0; x > -1; x++) { //do something } Infinite-Loop

8 Common Errors What is the output of the following code: Syntax Error
int n; cin>>n; for(int x=0; x<n; x++) cout<< “*”; cout<<“the loop repeated”<<x+1; Variables Scope Logical Error for(int x=0; x<3; x++) ; cout<< “*”; Null statement int new_term=0; int sum=0; for(int x=0; x<3; x++) new_term=x*x; sum+= new_term; cout<<“the sum is:”<<sum; Logical Error Body Scope

9 Example: Fibonacci Numbers
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. Complete the Pseudo code below then write it as a C++ function /** fibonacci ************************************************* n - an n >= 2 the n'th fibonacci number ****************************************************************/ int fibonacci(int n) define F0 define F1 let Flast be F1 that is 1 let F2ndlast be F0 that is 0 for i = 2 to n Fi = Flast + F2ndlast F2ndlast = Flast Flast = Fi

10 /. fibonacci. @params: n - an integer @pre: n >= 2
/** fibonacci ************************************************* * n - an n >= 2 * the n'th fibonacci number ****************************************************************/ int fibonacci(int n) { } int fLast = 1; int f2Last = 0; int fi; for (int i = 2; i <= n; i++) { fi = fLast + f2Last; f2Last = fLast; fLast = fi; } return fi;

11 Example 2: Definite integral
The definite integral of f(x) from x = a to x = b can be approximated by n trapezoids, each of width deltaX = (b-a)/n. The i'th trapezoid has a left edge height f(Xleft) and a right edge height of f(Xright). Write a function for computing the integral with arguments a, b and n, assuming the function f(x) has been declared and written (you don't need to know what f(x) is- it can be anything, e.g. sin(x) ). You may assume b > a. The area of a trapezoid is computed by ((Xleft + Xright) / 2 )* T. Give an implementation for the function using the algorithm described above. double integrate(double a, double b, int n);

12 Example of f(x) can be a sin(x)
You can assume that this is already implemented for you and you can call it directly //This is an example of what f(x) might be double f(double x) { return sin(x); }

13 Solution double integrate(double a, double b, int n) { double area = 0; double T = (b - a) / n; double xleft = a; double xright = xleft + T; for (int i = 0; i<n; i++) { area += ( (f(xleft) + f(xright)) / 2) * T; xleft = xright; xright = xleft + T; } return area;


Download ppt "FOR LOOPS."

Similar presentations


Ads by Google