0% found this document useful (0 votes)
12 views9 pages

DAA

The document contains a series of practical exercises demonstrating various programming concepts in C++, including array implementation, stack operations, linear and binary search, bubble sort, finding minimum and maximum values, recursive functions, and generating Fibonacci series. Each practical includes code snippets, aims, and outputs to illustrate the functionality. The exercises cover fundamental data structures and algorithms, showcasing their implementation and execution results.

Uploaded by

janumali1991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

DAA

The document contains a series of practical exercises demonstrating various programming concepts in C++, including array implementation, stack operations, linear and binary search, bubble sort, finding minimum and maximum values, recursive functions, and generating Fibonacci series. Each practical includes code snippets, aims, and outputs to illustrate the functionality. The exercises cover fundamental data structures and algorithms, showcasing their implementation and execution results.

Uploaded by

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

Practical no.

Aim :- Implementation of 1-D array using C++

#include <iostream>

using namespace std;

int main() {

// Declaration and initialization of an array

int arr[5] = {10, 20, 30, 40, 50};

// Accessing elements of the array

cout << "Element at index 2: " << arr[2] << endl;

// Modifying elements of the array

arr[3] = 60;

cout << "Modified element at index 3: " << arr[3] << endl;

// Calculating the sum of all elements

int sum = 0;

for (int i = 0; i < 5; i++) {

sum += arr[i];

cout << "Sum of all elements: " << sum << endl;

return 0;

Output :-

Element at index 2: 30
Modified element at index 3: 60

Sum of all elements: 170

=== Code Execution Successful ===

Practical no.2

Aim:- Push Pop and display

#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}

Output :

1) Push in stack

2) Pop from stack

3) Display stack

4) Exit

Enter choice:

Enter value to be pushed:

34

Enter choice:

45

Invalid Choice

Enter choice:

Enter value to be pushed:

45

Enter choice:

3
Stack elements are:45 34

Enter choice:

Enter value to be pushed:

66

Enter choice:

Stack elements are:66 45 34

Enter choice:

The popped element is 66

Enter choice:

=== Session Ended. Please Run the code again ===

Practical no .3

Aim:- Linear Search using C++

#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int key) {


for (int i = 0; i < n; i++) {
if (arr[i] == key) {
// Return the index of the found element
return i;
}
}
// Return -1 if the element is not found
return -1;
}

int main() {
int arr[] = {10, 25, 45, 30, 75, 90};
int n = sizeof(arr) / sizeof(arr[0]);
int key = 90;

int result = linearSearch(arr, n, key);

if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

return 0;
}

Output:-
Element found at index: 5

Practical no 4
Aim:- Binary Search using C++

#include <iostream>
using namespace std;
void binary_search(int a[], int low, int high, int key){
int mid;
mid = (low + high) / 2;
if (low <= high) {
if (a[mid] == key)
cout << "Element found at index: " << mid << endl;
else if(key < a[mid])
binary_search(a, low, mid-1, key);
else if (a[mid] < key)
binary_search(a, mid+1, high, key);
} else if (low > high)
cout << "Unsuccessful Search" <<endl;
}
int main(){
int i, n, low, high, key;
n = 5;
low = 0;
high = n-1;
int a[10] = {12, 14, 18, 22, 39};
key = 39;
binary_search(a, low, high, key);
key = 12;
binary_search(a, low, high, key);
return 0;
}
Output :-
Element found at index: 4
Element found at index: 0

=== Code Execution Successful ===

Practical no 5
Aim:- Bubble Sort
#include<iostream>
using namespace std;
void bubbleSort(int *array, int size){
for(int i = 0; i<size; i++) {
int swaps = 0; //flag to detect any swap is there or not
for(int j = 0; j<size-i-1; j++) {
if(array[j] > array[j+1]) { //when the current item is bigger than
next
int temp;
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
swaps = 1; //set swap flag
}
}
if(!swaps)
break; // No swap in this pass, so array is sorted
}
}
int main(){
int n;
n = 5;
int arr[5] = {67, 44, 82, 17, 20}; //initialize an array
cout << "Array before Sorting: ";
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
bubbleSort(arr, n);
cout << "Array after Sorting: ";
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
}
Output:-
Array before Sorting: 67 44 82 17 20
Array after Sorting: 17 20 44 67 82

=== Code Execution Successful ===

Practical no 6
Aim:- find Nth Minimum Elements using iteractive Approach

#include <iostream>
using namespace std;
void getMinMax(int arr[] , int N){
int max = arr[0], min = arr[0];
for(int i = 1; i < N; i++){
if(max < arr[i])
max = arr[i];
if(min > arr[i])
min = arr[i];
}
cout<<"Maximum Value = "<<max<<"\n";
cout<<"Minimum Value = "<<min;
}
int main(){
int arr[] = {2, 1, 6, 9, 4, 10, 15, 21};
int N = 8;
getMinMax(arr, N);
return 0
}

Output:-
Maximum Value = 21
Minimum Value = 1

=== Code Execution Successful ===


Practical no. 7
Aim :- To find Nth of Maximum Elements using Recursive approach
#include <iostream>
using namespace std;
int CalcMinValue(int arr[], int n) {
return (n == 1) ? arr[0] : min(arr[n - 1], CalcMinValue(arr, n - 1));
}
int CalcMaxValue(int arr[], int n) {
return (n == 1) ? arr[0] : max(arr[n -1], CalcMinValue(arr, n - 1));
}
int main() {
int arr[] = {2, 1, 6, 9, 4, 10, 15, 21};
int N = 8;
cout<<"Maximum Value = "<<CalcMaxValue(arr, N)<<endl;
cout<<"Minimum Value = "<<CalcMinValue(arr, N);
return 0;
}

Output
Maximum Value = 21
Minimum Value = 1

=== Code Execution Successful ===

Practical no .8

Aim:- program on recursive function using c++

#include <iostream>
using namespace std;

// Recursive Function to Calculate Factorial


int factorial(int num) {
// Base case
if (num <= 1) {
return 1;
}
// Recursive case
else {
return num * factorial(num - 1);
}
}

int main() {
int positive_number;
cout << "Enter a positive integer: ";
cin >> positive_number;
if (positive_number < 0) {
cout << "Wrong Input, Factorial is not Defined for Negative Integer"
<< endl;
} else {
cout << "Factorial of " << positive_number << " is " <<
factorial(positive_number) << endl;
}
return 0;
}

Output :-
Enter a positive integer: 12

Factorial of 12 is 479001600

=== Code Execution Successful ===

Practical no 9

Aim:- to Greedy approach for coin change

Practical no 10

Aim:- Fibonacci series using C++

#include <iostream>

using namespace std;

int fib(int x) {

if((x==1)||(x==0)) {

return(x);

}else {

return(fib(x-1)+fib(x-2));

int main() {

int x , i=0;

cout << "Enter the number of terms of series : ";

cin >> x;
cout << "\nFibonnaci Series : ";

while(i < x) {

cout << " " << fib(i);

i++;

return 0;

Output :-

Enter the number of terms of series : 5

Fibonnaci Series : 0 1 1 2 3

=== Code Execution Successful ===

You might also like