Sunteți pe pagina 1din 16

General formula for calculating

number of operations of loop


statement (simple tactics)
ASSUME I IS INCREMENTED BY ONE
For loop statement

Case 1: when i starts from zero and goes to i<=n


(n + 2) – 0 for testing condition
No. Operation *(n + 1) – 0 for loop body
(n + 1) – 0 for increment
General formula :
(n + 2) – ( initial value of I ) for testing condition
No. Operation *(n + 1) – ( initial value of I) for loop
body
(n + 1) – ( ­initial value of I ) for increment
Continue…

Example
Int sum=0;
For (int i=1; i<=n; i++)
{ 
Sum=sum+I;
Count<<I;
}
(n + 2) – 1 for testing condition i.e. i<=n // n +1
3*(n +1) –1 for loop body // 3*(n )
Continue …

Case 2: : when i starts from zero and goes to i<n


(n + 1) – 0 for condition testing
No. Operation *(n ) – 0 for loop body
(n ) – 0 for increment
 
General formula :
(n + 1) – ( initial value of i) for testing condition
No. Operation *(n ) –( initial value of i ) for loop body
(n ) – ( initial value of i ) for increment
Continue…

Example
Int sum=0;
For (int i=2; i<n; i++)
{ 
Sum=sum+i;
Count<<i;
}
(n + 1) – 2 for testing condition i.e. i<=n // n -1
3*(n ) –2 for loop body // 3n - 2
While statement

Case 1: when I starts from zero and goes to i<=n


(n + 2) – 0 for testing condition
No. Operation *(n + 1) – 0 for loop body
General formula :
(n + 2) – ( initial value of I) for testing condition
No. Operation *(n + 1) – ( initial value of I ) for loop
body
Continue …

Example
int i=1;
While (i<=n)
{
X++;
I++;
}
(n + 2) – (1 ) for testing condition //n + 1
2 *(n + 1) – ( 1 ) for loop body // 2n + 1
Continue …

Case 2: : when i starts from zero and goes to i<n


(n + 1) – 0 for condition testing
No. Operation *(n ) – 0 for loop body
 
General formula :
(n + 1) – ( initial value of i ) for testing condition
No. Operation *(n ) – ( initial value of i) for loop
body
Continue …

Example
int i=1;
While (i<n)
{
X++;
I++;
}
(n + 1) – (1 ) for testing condition //n
2 *(n ) – ( 1 ) for loop body // 2n – 1
Do …. While statement

Case 1: when I starts from zero and goes to i<=n


(n + 1) – 0 for testing condition
No. Operation *(n + 1) – 0 for loop body

General formula :
(n + 1) – ( initial value of i ) for testing condition
No. Operation *(n + 1 ) – ( initial value of i ) for loop body
 
Restriction :– initial value of i must be less than n
If initial value of i is greater than n there will be only (one
operation)*(No. operation in the loop ) for loop body and also one
operation for testing condition
Continue ….

Example
int i=0;
do
{
X++;
I++;
} While (i<=n);
 
(n + 1) – (0 ) for testing condition //n + 1
2 *(n + 1) – ( 0) for loop body // 2n +2
Continue …

Case 2: : when I starts from zero and goes to i<n


(n ) – 0 for testing condition
No. Operation *(n ) – 0 for loop body
 
General formula :
(n ) – ( initial value of I) for testing condition
No. Operation *(n ) – ( initial value of I) for loop body

Restriction :– initial value of I must be less than n


If initial value of i is greater than n there will be only (one
operation)*(No. operation in the loop ) for loop body and also one
operation for testing condition
Continue ….

Example
int i=1;
do
{
X++;
I++;
} While (i<n);
 
(n ) – (1 ) for testing condition //n -1
2 *(n ) – ( 1) for loop body // 2n -1
Exercises

Fin the complexity equation for the following algorithm


1.
Int sum=3;
For (int i=1; i<=n; i++)
{
 
Sum=sum+I;
Count<<I;
}
Continue ….

2.
Void myfun()
{
 
Int x=0;
Int i=8;
Int j=5;
Cout<<” Enter an integer ”;
Cin>>n;
While (i<n)
{
X++;
I++;
}
 
While(j<n)
{
J++;
}
 
}
Continue ….

3.
Int sum( int n)
{
Int psum=0;
For(int i=1; i<=n; i++)
{
Psum+=(i*i*i);
If(psum%2)
{
Psum++;
i--;
}
Else
Psum--;
}
Return psum;
 
}

S-ar putea să vă placă și