You are on page 1of 27

CE355 Practical -1 ID: 20CE120

1.1 FIBONACCI
1.1.1 FibonaCCi iterative
Code
#include<iostream>

using namespace std;

int counter=0;
int fibonacci(int n)
{
int f0 = 0,f1 = 1,f;
if(n==0)
{
return 0;
}
if(n==1)
{
return 1;
}

else
{
for(int i=2;i<=n;i++)
{

f = f0 +f1;
f0 = f1;
f1 = f;
counter+=3;
}
return f;
}
}
int main()
{
int n;
cout<<"enter the number of element :";
cin >> n;
cout<<"fibonaki : "<<fibonacci(n)<<endl;
cout<<"total no. of count :"<<counter<<endl;
}
CE355 Practical -1 ID: 20CE120

Output

1.1.2 Fibonacci recursive


Code

#include <iostream>

using namespace std;

int counter=0;
int fibonacci(int n)
{

if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
CE355 Practical -1 ID: 20CE120

counter++;
return fibonacci(n-1) + fibonacci(n-2);
}
}
int main() {
// Write C++ code here
int n;
cout<<"enter the no. of elements :";
cin >> n;
cout<<"fibonacci : "<<fibonacci(n)<<endl;
cout<<"total no. of count :"<<counter;

return 0;
}
output
CE355 Practical -1 ID: 20CE120

Table and Chart


1.1.1 Fibonacci iterative
Table

n Cnt _for_iterative
3 6
5 12
10 27
12 33
15 42

Chart
cnt_for_interation
45
40 f(x) = 3 x − 3
35
30
25
20
15
10
5
0
2 4 6 8 10 12 14 16

1.1.2 Fibonacci recursive


Table

n cnt_for_recursive
3 2
4 4
5 7
6 12
7 20
CE355 Practical -1 ID: 20CE120

char
t cnt_for_recursion
25

20 f(x) = 0.386473060513728 exp( 0.57037824746562 x )

15

10

0
2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5
CE355 Practical -1 ID: 20CE120

1.2 FACTORIAL
1.2.1 Factorial iterative
Code
#include <iostream>

using namespace std;

int count=0;
int ans =1;
int fac(int n)
{

if(n==0)
{
return ans;
}
else if(n==1)
{
return ans;
}
else
{
for(int i=2;i<=n;i++)
{
count++;
ans*=i;
}
return ans;
}
}
int main() {
// Write C++ code here
int n;
cout<<"enter the number of elements :";
CE355 Practical -1 ID: 20CE120

cin >> n;
cout<<"factorial :"<<fac(n)<<endl;
cout<<"total no. of count :"<<count;

return 0;
}
Output

1.2.2 Factorial recursive


Code
#include <iostream>

using namespace std;

int count=0;
int ans =1;

int fac(int n)
{
CE355 Practical -1 ID: 20CE120

if(n==0)
{
return ans;
}
else if(n==1)
{
return ans;
}
else
{
count++;
return n*fac(n-1);
}
}
int main() {
// Write C++ code here
int n;
cout<<"enter the number of elements :";
cin >> n;
cout<<"factorial :"<<fac(n)<<endl;
cout<<"total no. of count :"<<count;

return 0;
}

Output
CE355 Practical -1 ID: 20CE120

Table and Chart


1.2.1 Factorial iterative
Table
CE355 Practical -1 ID: 20CE120

n Cnt_for_iterative
3 2
4 3
5 4
6 5
7 6

Chart
cnt_for_iterative
7

6
f(x) = x − 1
5

0
2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5

1.2.2 Factorial recursive


Table

n Cnt_for_recursive
3 2
4 3
5 4
6 5
7 6

Chart
CE355 Practical -1 ID: 20CE120

cnt_for_recursive
7

6
f(x) = x − 1
5

0
2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5

1.3.1 linear search


Code #include<iostream>
CE355 Practical -1 ID: 20CE120

using namespace std;

void linear(int arr[],int Size,int Search)


{
int flag = 0;
int counter = 0;
for(int i=0;i<Size;i++)
{

if(arr[i] == Search)
{
flag++;
break;
}
counter+=3;
}
counter+=1;
if(flag!=0)
{
cout<< "Item found"<<endl;
}
else
{
cout<< "Item not found"<<endl;
}
cout << "no. of counter = " << counter<<endl;

}
int main()
{
int n,A;
cout << "enter size of array :"<<endl;
cin >> n;
int Array[n];
cout<<"enter the elements :"<<endl;
CE355 Practical -1 ID: 20CE120

for(int i=0;i<n;i++)
{
cin>> Array[i];
}
cout<< "enter the search the elements :"<<endl;
cin >> A;
linear(Array,n,A);
return 0;
}
Output Best_case
CE355 Practical -1 ID: 20CE120

Average_case
CE355 Practical -1 ID: 20CE120

Worst_ case
CE355 Practical -1 ID: 20CE120
CE355 Practical -1 ID: 20CE120

Table and Chart


Best_case
Table

Chart
count_of_best_case
1.2

1
f(x) = 1

0.8

0.6

0.4

0.2

0
2 4 6 8 10 12 14 16

Average_case
Table
CE355 Practical -1 ID: 20CE120

Chart
count_of_average_case
25

20 f(x) = 1.43827160493827 x + 0.0308641975308674

15

10

0
2 4 6 8 10 12 14 16

Worst_case
Table

Chart
count_of_worst_case
50
45
f(x) = 3 x + 1
40
35
30
25
20
15
10
5
0
2 4 6 8 10 12 14 16
CE355 Practical -1 ID: 20CE120

1.3.2 Binary Search


Code
#include<iostream>

using namespace std;

int counter=0;
int binary(int arr[],int key,int low,int high)
{
int mid;
counter++;
if(low>high) return -1;
CE355 Practical -1 ID: 20CE120

mid = low+high/2;
counter++;
if(key == arr[mid])
{
return mid;
}
else if(key<arr[mid])
counter++;
return binary(arr,key,low,mid-1);
counter++;
binary(arr,key,mid+1,high);
}

int main()
{
int n,A;
cout << "enter size of array :"<<endl;
cin >> n;
int Array[n];
cout<<"enter the elements :"<<endl;
for(int i=0;i<n;i++)
{
cin>> Array[i];
}
cout<< "enter the search the elements :"<<endl;
cin >> A;
int position = -1;
int Low = 0;
int High = n-1;
position = binary(Array,A,Low,High);
if(position!=-1)
{
cout<<"number present at index :"<<position+1<<endl;

}
CE355 Practical -1 ID: 20CE120

else{
cout<<"the number is not present in the list"<<endl;
}
cout<<"total no. of count :"<< counter<<endl;

return 0;
}
Output Best_case
CE355 Practical -1 ID: 20CE120

Average_case
CE355 Practical -1 ID: 20CE120

Worst_case
CE355 Practical -1 ID: 20CE120

Table and Chart


Best_case
Table
n Cnt_of_best_case
3 2
CE355 Practical -1 ID: 20CE120

5 2
8 2
12 2
15 2

Chart
count_of_best_case
2.5

2
f(x) = 2

1.5

0.5

0
2 4 6 8 10 12 14 16

Average_case
Table
n Cnt_of_average_case
3 5
5 6
8 5
12 7
15 11

Chart
CE355 Practical -1 ID: 20CE120

count_of_average_case
12

10
f(x) = 0.42798353909465 x + 3.11934156378601
8

0
2 4 6 8 10 12 14 16

Worst_case
Table
n Cnt_of_worst_case
3 5
5 6
8 7
12 9
15 11

Chart
CE355 Practical -1 ID: 20CE120

count_of_worst_case
12

10 f(x) = 0.48559670781893 x + 3.4238683127572

0
2 4 6 8 10 12 14 16

You might also like