Connexions

You are here: Home » Content » C++ Iterations

Recently Viewed

This feature requires Javascript to be enabled.

C++ Iterations

Module by: uday kumar. E-mail the author

CS101: Lecture 6

Iterative solutions

August 13, 2010

Lecture by: Prof. D B Phatak

Scribes by: Devanshu Mathur

Lets recall, In a while statement the condition as ‘0’ is false and any non zero number is true.

For any iterative solution we should take care of– Designing the repetitive block– Designing the terminating condition– Designing initialization

While keeps executing the repetitive block till condition is true. So for termination, something must happen inside block which changes condition.

So we decide to input 0 to end the program. Now the code can be written aswhile (x !=0) {

cin >> x ;

if (x > max) { max = x;}

}

Identify block: Input value, compare, reset or don’t do anything inside the block

Identify termination condition: Condition is put (x is not equal to zero)

Initialisation: several values are undefined for the first time. ‘x’ should be initialised. (because ‘x’ may use some other value of a previous program if not initialised within your program). ‘max’ should be initialised too. (you should always check program manually for 1st time run for avoiding compiling errors.)

The initialisation can take place before the repetitive block.

#include<iostream>

using namespace std;

int main(){

int x, max;

x = -9999; max =x; //initialisation

while(x!=0){ //condition

cout<<"enter the next positive integer (0 to end): "; //repetitive block starts

cin>>x;

if(x > max) {max = x;}

} //repetitive block ends

cout<<endl << “maximum is ” << max << endl;

return 0;

}

We know negative numbers will not be inputted, so we assigned a random negative number to the value of ‘x’ and ‘max’.

Things to ponder: Why did we choose random values of x, max.A better way of doing it may be read first input before the iterative loop and initialise it to max. Iteration can take care of input from 2nd input onwards.int x, max;cout<<"enter the first number: ";cin >> x; max =x;

while(x!=0){

cout<<"enter the next positive integer (0 to end): ";

cin>>x;

if(x > max) {max = x;}}cout<<endl << “maximum is ” << max << endl;

One more modification can be following. Here zero does not get compared with max and the loop terminates on finding a zero a input directly.int x, max;

cout<<"enter the first number: ";

max =x;

while(x!=0){

if(x > max) {max = x;}

cout<<"enter the next positive integer (0 to end): ";

cin>>x;

}

cout<<endl << “maximum is ” << max << endl;

Newton raphson method:

It helps us to find a root of a function by the following algorithm:Start with some initial guess of the root, say xi

– Evaluate f(xi ), if it is close to 0, exit iteration

– If not

• find the next approximation, say xi+1 which can be found by xi+1 = (xi- f(xi)/f’ (xi))

• Repeat this processThis method works fine if:

– f(x) and f '(x) can be easily calculated.

– and a good initial guess is available, which is 1 in most of the cases.

Thing to note: We’ll have to use floating point nos. We cannot get exact zero as the value of f(x), so we must stop near to zero. Therefore tolerance is defined. Eg: 0.0001 which is good enough for our purpose.

This implies termination criteria: |f(x)|<= .0001

Intialisation: x=1;

Therefore the program will look like,

#include<iostream>

using namespace std;

int main() {

float k; float x=1; //initialisation

cout << “Give number whose square root is to be found ”;

cin >> k;

while (x*x – k > 0.0001 || k - x*x > 0.0001){ //condition

x = (x + k/x)/2 ;

}

cout << endl<< “Value of the root is ” << x << endl;

return 0;

}

Things to ponder: A useful modification to the program can be, making the user define accuracy.

For statement: IntroductionFor statement is used when we have to run the iterations ‘n’ times, given the value of ‘n’.So all we need is a counting mechanism for the iterations.We can do this by using ‘while’ too, by defining a variable ‘count’ and while ‘count’ is less than ‘n’ run the loop and in iteration block do increment n by one everytime (n++;)

‘for’ is just a concise statement to give initial value, checking condition and increment/decrement of ‘count’ in a single statement. It is a simplified and neat mechanism.

Example:for ( count = 1; count <=5; count ++){

// statements within the iterative block

}

Content actions

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks