100% found this document useful (1 vote)
190 views35 pages

DAA File Format

The document describes several algorithms and their implementations: 1) Factorial using iterative and recursive approaches. Testing on input values from 1-5 shows the recursive approach takes 5-15 steps depending on input size. 2) Fibonacci series using iterative and recursive approaches. Testing on input values from 1-25 shows the recursive approach takes 7-1581 steps depending on input size. 3) Matrix addition using nested loops to add corresponding elements. Testing on matrix sizes from 1-5 shows it takes 1-25 steps depending on input size. 4) Matrix multiplication using nested loops. Testing on matrix sizes from 1-5 shows it takes 4-200 steps depending on input size. 5) Linear search using

Uploaded by

programmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
190 views35 pages

DAA File Format

The document describes several algorithms and their implementations: 1) Factorial using iterative and recursive approaches. Testing on input values from 1-5 shows the recursive approach takes 5-15 steps depending on input size. 2) Fibonacci series using iterative and recursive approaches. Testing on input values from 1-25 shows the recursive approach takes 7-1581 steps depending on input size. 3) Matrix addition using nested loops to add corresponding elements. Testing on matrix sizes from 1-5 shows it takes 1-25 steps depending on input size. 4) Matrix multiplication using nested loops. Testing on matrix sizes from 1-5 shows it takes 4-200 steps depending on input size. 5) Linear search using

Uploaded by

programmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 35

DAA 16CE068

Practical-1
Implement and Analyze Given
Algorithms

1
DAA 16CE068

Program 1: Factorial(Iterative)

Code:
#include<stdio.h>

int main()

int n;

int f=1;

int i=1;

int count=0;

count++;

scanf("%d",&n);

while(i!=n+1)

f=f*i;

i++;

count++;

};

printf("%d",f);

printf(" count %d",count);

2
DAA 16CE068

Output:

Table:
No of
instructions count
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9

Graph:

3
DAA 16CE068

4
DAA 16CE068

Factorial(Recursive)

Code:
#include<iostream>

using namespace std;

static int count;

int fact(int n)

if(n==1)

return 1;

int f=n;

count=0;

f=f*fact(f-1);

count++;

return f;

int main()

int n;

cin>>n;

long int x=fact(n);

cout<<x;

cout<<"count is"<<count;

5
DAA 16CE068

Output:

Table:

input count
4 5
9 10
14 15
19 20
24 25

Graph:

Program 2: Fibonacci Series(Iterative)


6
DAA 16CE068

Code:
#include<iostream>

using namespace std;

int main(){

int x = 0, y = 1, z, i, n, count = 0;

cout<<"Enter the number of terms you want :";

cin>>n;

cout<<"\nFibonacci series : ";

for(i=0;i<n;i++){

count++;

if(i == 0)

cout<<0<<"\t";

else if(i == 1)

cout<<1<<"\t";

else{

z = x + y;

x = y;

y = z;

cout<<z<<"\t";

} }

cout<<"\nTotal steps :"<<count;

7
DAA 16CE068

Output:

Table:

input count
1 1
2 2
3 3
4 4
5 5
Graph:

Program 1: Factorial(Recursive)

Code:
8
DAA 16CE068

#include<iostream>

using namespace std;

int count;

int fibonacci(int n)

if((n==1)||(n==0))

return(n);

else

count++;

return(fibonacci(n-1)+fibonacci(n-2));

int main()

int n,i=0;

cout<<"Input the number of terms for Fibonacci Series:";

cin>>n;

cout<<"\nFibonacci Series is as follows\n";

9
DAA 16CE068

while(i<n)

cout<<" "<<fibonacci(i);

i++;

cout<<"Count is"<<count;

return 0;

Output:

Table:

input count
5 7

10
DAA 16CE068

10 133
15 1581
20 17690
25 196392

Graph:

Program 3: Matrix Addition

Code:

11
DAA 16CE068

#include<iostream>

using namespace std;

static int count;

int main()

int n,m;

int count=0;

cout<<"enter size of matrix:";

cin>>n;

cin>>m;

int a[n][m];

int b[n][m];

int c[n][m];

cout<<"enter matrix 1:";

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

cin>>a[i][j];

cout<<"enter matrix 2:";

for(int i=0;i<n;i++)

12
DAA 16CE068

for(int j=0;j<m;j++)

cin>>b[i][j];

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

c[i][j]=a[i][j]+b[i][j];

count++;

for(int i=0;i<n;i++)

for(int j=0;j<m;j++)

cout<<c[i][j]<<" ";

cout<<"\n";

cout<<"count is "<<count;

13
DAA 16CE068

Output:

Table:

input input
size a size b count
1 1 1
2 2 4
3 3 9
4 4 16
5 5 25

Graph:

14
DAA 16CE068

Matrix Multiplication(Iterative)

15
DAA 16CE068

Code:
#include <iostream>

using namespace std;

int count;

int main()

while(true){

int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

cout << "Enter rows and columns for first matrix: ";

cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";

cin >> r2 >> c2;

// If column of first matrix in not equal to row of second matrix,

// ask the user to enter the size of matrix again.

while (c1!=r2)

cout << "Error! column of first matrix not equal to row of second.";

cout << "Enter rows and columns for first matrix: ";

cin >> r1 >> c1;

16
DAA 16CE068

cout << "Enter rows and columns for second matrix: ";

cin >> r2 >> c2;

// Storing elements of first matrix.

cout << endl << "Enter elements of matrix 1:" << endl;

for(i = 0; i < r1; ++i)

for(j = 0; j < c1; ++j)

count++;

cout << "Enter element a" << i + 1 << j + 1 << " : ";

cin >> a[i][j];

// Storing elements of second matrix.

cout << endl << "Enter elements of matrix 2:" << endl;

for(i = 0; i < r2; ++i)

for(j = 0; j < c2; ++j)

count++;

cout << "Enter element b" << i + 1 << j + 1 << " : ";

cin >> b[i][j];

17
DAA 16CE068

// Initializing elements of matrix mult to 0.

for(i = 0; i < r1; ++i)

for(j = 0; j < c2; ++j)

count++;

mult[i][j]=0;

// Multiplying matrix a and b and storing in array mult.

for(i = 0; i < r1; ++i)

for(j = 0; j < c2; ++j)

for(k = 0; k < c1; ++k)

count++;

mult[i][j] += a[i][k] * b[k][j];

// Displaying the multiplication of two matrix.

cout << endl << "Output Matrix: " << endl;

for(i = 0; i < r1; ++i)

for(j = 0; j < c2; ++j)

cout << " " << mult[i][j];

if(j == c2-1)

18
DAA 16CE068

cout << endl;

cout<<"NO OF COUNT IS"<<count;

count=0;

return 0;

Output:

Table:

19
DAA 16CE068

input a input b count


1 1 4
2 2 20
3 3 54
4 4 112
5 5 200

Graph:

Program 3:Recursive Linear Search

20
DAA 16CE068

Code:
/* Recursive function to search x in arr[l..r] */

#include<iostream>

#include<stdio.h>

using namespace std;

int count;

int recSearch(int arr[], int l, int r, int x)

if (r < l)

count++;

return -1;

if (arr[l] == x)

count++;

return l;

count++;

return recSearch(arr, l+1, r, x);

int main()

21
DAA 16CE068

int no;

cout<<"ENTER SIZE OF ELEMENTS "<<endl;

cin>>no;

int arr[no], i;

cout<<"ENTER ELEMENTS"<<endl;

for(int i=0;i<no;i++)

cin>>arr[i];

int n = sizeof(arr)/sizeof(arr[0]);

int x ;

cout<<"ENTER NUMBER TO SEARCH"<<endl;

cin>>x;

int index = recSearch(arr, 0, n-1, x);

if (index != -1)

printf("Element %d is present at index %d", x, index);

else

printf("Element %d is not present", x);

return 0;

Output:

22
DAA 16CE068

Table:
Worst Case

input count
1 1
2 2
3 3
4 4
5 5

Best Case

input count
1 1
2 1
3 1
4 1
5 1

Graph:

23
DAA 16CE068

Worst case

Best case

Program 1: Binary Search(Recursive)

24
DAA 16CE068

Code:
// C program to implement recursive Binary Search

#include <stdio.h>

#include<iostream>

using namespace std;

// A recursive binary search function. It returns

// location of x in given array arr[l..r] is present,

// otherwise -1

int count;

int binarySearch(int arr[], int l, int r, int x)

if (r >= l)

int mid = l + (r - l)/2;

// If the element is present at the middle

// itself

if (arr[mid] == x)

{count++;

return mid;}

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid] > x)

25
DAA 16CE068

{ count++;

return binarySearch(arr, l, mid-1, x); }

// Else the element can only be present

// in right subarray

count++;

return binarySearch(arr, mid+1, r, x);

// We reach here when element is not

// present in array

return -1;

int main(void)

{ while(true){

int no;

cout<<"ENTER SIZE OF ELEMENTS "<<endl;

cin>>no;

int arr[no], i;

cout<<"ENTER ELEMENTS"<<endl;

for(int i=0;i<no;i++)

{ cin>>arr[i]; }

26
DAA 16CE068

int n = sizeof(arr)/sizeof(arr[0]);

int x = 10;

int result = binarySearch(arr, 0, n-1, x);

(result == -1)? printf("Element is not present in array")

: printf("Element is present at index %d",

result);

cout<<"COUNT IS"<<count;

count=0;

return 0;

Output:

Table:

27
DAA 16CE068

Worst Case

input count
1 1
2 2
3 2
4 3
5 3
6 3
7 3
8 4
9 4
10 4

Best Case

input count
1 1
2 2
3 2
4 3
5 3
6 3
7 3
8 4
9 4
10 4

Graph:

28
DAA 16CE068

Worst case

Best case

29
DAA 16CE068

Program 5: Find a subset of a given set S = {s1,s2,.....,sn} of n positive


integers whose sum is equal to a given positive integer d. For example,
if S= {1, 2, 5, 6, 8} and d = 9 there are two solutions {1,2,6} and {1,8}.A
suitable message is to be displayed if the given problem instance
doesn't have a solution.

Code:
#include<iostream>

#include<math.h>

using namespace std;

int count=0;

void subset(int num,int n, int x[])

{ count++;

int i;

for(i=1;i<=n;i++)

x[i]=0;

for(i=n;num!=0;i--)

{ x[i]=num%2;

num=num/2; }

int main()

int a[10];

int x[10];

int n,d,sum,present=0;

30
DAA 16CE068

int j;

cout<<"enter the number of elements of set";

cin>>n;

cout<<"enter the elements of set";

for(int i=1;i<=n;i++)

cin>>a[i];

cout<<"enter the positive integer sum";

cin>>d;

if(d>0)

for(int i=1;i<=pow(2,n)-1;i++)

count++;

subset(i,n,x);

sum=0;

for(j=1;j<=n;j++)

if(x[j]==1)

sum=sum+a[j];

if(d==sum)

cout<<"Subset={";

present=1;

31
DAA 16CE068

for(j=1;j<=n;j++)

if(x[j]==1)

cout<<a[j]<<",";

cout<<"}="<<d;

cout<<"\n";

if(present==0)

cout<<"Solution does not exists";

cout<<" \n count is "<<count;

Output:

32
DAA 16CE068

Table:

input count
5 62
10 2046
15 65534
20 2097150
25 67108862

Graph:

33
DAA 16CE068

Conclusion: Complexity table for all programs


 Comparative study of Linear Search & Binary Search:

Basis For Comparison Linear Search Binary Search

Time Complexity O(N) O(log2N)

Best Case Time First Element O(1) Center Element O(1)

Prerequisite for an array No array Array must be in sorted


order

Worst case for N number N comparisons are Can conclude after only
of elements required log2N comparisons

Can be implemented on Array and Linked List Cannot be directly


implemented on linked
list

Algorithm type Iterative in nature Divide and conquer in


nature

Usefulness Easy to use and no need Anyhow tricky algorithm


for any ordered elements and elements should be
organized in order.

Conclusion:

Sr No. Problem Definition Practical Complexity Theoretical

Equation Complexity

1 Factorial (Iterative) Y=X O(n) O(n)

2 Factorial (Recursive) Y=X O(n) O(n)

3 Fibonacci (Iterative) Y=X O(n) O(n)

4 Fibonacci (Recursive) Y = - O(2n)


0.5023x
1.6515e

5 Matrix Addition Y = X2 O(n2) O(n2)

6 Matrix Multiplication Y = X3 O(n3) O(n3)

34
DAA 16CE068

7 Linear Search Y=X O(n) O(n)

8 Binary Search Y = O(logen) O(logen)


2.018ln(x) –
0.3801

We have studied all above algorithms. As a conclusion from the study, we


can say that to find factorial of a number we can use any algorithm iterative or
recursive. Complexity for both algorithms is same. To find Fibonacci series
iterative method is beneficial because complexity of iterative algorithm increases
linearly as input increase. But in recursive algorithm, it increases exponentially.
Complexity of matrix addition and multiplication is respectively O(n 2) and O(n3).
From linear search and binary search, binary search is more useful because
complexity of binary search is O(log n). But for binary search the list should be
sorted. Linear search can be applied on both sorted and unsorted list.

35

You might also like