Ds 1
Ds 1
#include<iostream>
using namespace std;
int main()
{
// Initialize an array with some elements
int arr[]={10,20,30,40,50};
int size = sizeof(arr)/sizeof(arr[0]);
cout<<"Size of given array is:"<<size<<endl;
//Traverse the array
cout<<"Elements of the array are: "<<endl;
for(int i=0;i<size;i++)
{
cout<<arr[1]<<" ";
}
cout<<endl;
return 0;
}
1
2. Program to insert an item at the end of an array.
#include<iostream>
using namespace std;
int main()
{
int MAX_SIZE=10;//Maximum capacity of the array
int arr[MAX_SIZE];//Array
int size;
int newElement;//New element to be inserted
cout<<"Enter number of elements want to input?";
cin>>size;
cout<<"Enter elements into array";
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
cout<<"Enter item to be inserted at end";
cin>>newElement;
2
//Print the updated array
cout<<"Elements of the array after insertion are:"<<endl;
for(int i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
return 0;
}
3
3. Program to Insert, Delete and Search operation on an
array.
#include<iostream>
using namespace std;
const int MAX_SIZE=100; //Maximum size of the array
int arr[MAX_SIZE]; //Array to hold elements
int size=0; //Current number of element in the array
4
if(position<0 || position>size){
cout<<"Invalid position. Please enter a position between 0 and
"<<size<<endl;
return;
}
//Shift elements to the right to make space for the new elements.
for(int i=size;i>position;--i){
arr[i]=arr[i-1];
}
arr[position]=element;
++size;
cout<<"Element inserted successfully at position "<<position<<"."<<endl;
}
5
return;
}
int main()
{
6
int choice, element, position;
while(true){ //Infinite loop
system("cls");
cout<<"\n Menu: \n";
cout<<"1. Insert element\n";
cout<<"2. Delete element\n";
cout<<"3. Display element\n";
cout<<"4. Search element\n";
cout<<"5. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:
cout<<"Enter element to insert: ";
cin>>element;
cout<<"Enter position(0 to "<<size<<"): ";
cin>>position;
insertElement(element,position);
break;
case 2:
cout<<"Enter element to delete: ";
cin>>element;
deleteElement(element);
break;
case 3:
displayArray();
break;
7
case 4:
cout<<"Enter Element to search: ";
cin>>element;
searchElement(element);
break;
case 5:
cout<<"Exiting program."<<endl;
return 0;
default:
cout<<"Invalid choice. Please try again. "<<endl;
}
system("pause");
}
}
8
4. Program to Merge two Un-Sorted array.
#include<iostream>
using namespace std;
void mergearrays(int arr1[], int arr2[], int size1, int size2, int mergedarray[])
{
for(int i=0; i<size1; i++)
{
mergedarray[i]=arr1[i];
}
for(int i=0; i<size2; i++)
{
mergedarray[size1+i]=arr2[i];
}
}
int main()
{
int size1, size2;
cout<<"Enter the size of the first array:";
cin>>size1;
int arr1[size1];
cout<<"Enter elements of the first array: \n";
for(int i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter the size of the second array: ";
cin>>size2;
int arr2[size2];
cout<<"Enter the elements of the second array: \n";
9
for(int i=0; i<size2; i++)
{
cin>>arr2[i];
}
int mergedsize = size1 + size2;
int mergedarray[mergedsize];
mergearrays(arr1,arr2,size1,size2,mergedarray);
10
5. Program to Merge two Sorted array.
#include<iostream>
using namespace std;
void mergesortedarray(int arr1[], int size1, int arr2[], int size2, int mergedarray[])
{
int i=0,j=0,k=0;
11
mergedarray[k++]=arr2[j++];
}
}
int main()
{
int size1, size2;
cout<<"Enter the size of the first sorted array: ";
cin>>size1;
int arr1[size1];
cout<<"Enter the elements of the first sorted array:\n ";
for(int i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter the size of the second sorted array: ";
cin>>size2;
int arr2[size2];
cout<<"Enter the elements of the second sorted array: \n";
for(int i=0; i<size2; i++)
{
cin>>arr2[i];
}
12
cout<<mergedarray[i]<<" ";
}
return 0;
}
13
6. Program of Addition & Subtraction of two matrices.
#include <iostream>
using namespace std;
void addMatrices(int rows, int cols, int mat1[][10], int mat2[][10], int result[][10])
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
void subtractMatrices(int rows, int cols, int mat1[][10], int mat2[][10], int result[][10])
{
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
int main()
{
int rows, cols;
cout << "Enter the number of rows and columns of the matrices: ";
cin >> rows >> cols;
int mat1[10][10], mat2[10][10], result[10][10];
14
cout << "Enter elements of the first matrix:\n";
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cin >> mat1[i][j];
}
}
cout << "Enter elements of the second matrix:\n";
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cin >> mat2[i][j];
}
}
addMatrices(rows, cols, mat1, mat2, result);
cout << "Result of matrix addition:\n";
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
subtractMatrices(rows, cols, mat1, mat2,result);
cout << "Result of matrix subtraction:\n";
for(int i = 0; i < rows; i++)
15
{
for(int j = 0; j < cols; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
16
7. Program of Product of two matrices.
#include<iostream>
using namespace std;
void multiplymatrices(int mat1[][10], int mat2[][10], int result[][10], int rows1, int cols1, int
cols2)
{
for(int i=0; i<rows1;i++)
{
for(int j=0; j<cols2; j++)
{
result[i][j]=0;
for(int k=0; k<cols1; k++)
{
result[i][j]+=mat1[i][k] * mat2[k][j];
}
}
}
}
int main()
{
int rows1, cols1, rows2, cols2;
cout<<"Enter the number of rows and columns of the first matrix: ";
cin >>rows1>>cols1;
int mat1[10][10];
cout<<"Enter elements of first matrix: \n";
for(int i=0; i<rows1; i++)
{
for(int j=0; j<cols1; j++)
{
17
cin>>mat1[i][j];
}
}
cout<<"Enter the number of rows and columns of the second matrix: ";
cin>>rows2>>cols2;
if(cols1!=rows2)
{
cout<<"Matrix Multiplication is not possible. Number of columns in the first
matrix must be equal to the number of rows in the second matrix."<<endl;
return 0;
}
int mat2[10][10];
cout<<"Enter elementsb of the second matrix: \n";
for(int i=0;i<rows2;i++)
{
for(int j=0;j<cols2;j++)
{
cin>>mat2[i][j];
}
}
int result[10][10];
multiplymatrices(mat1, mat2, result, rows1, cols2, cols2);
cout<<"1st Matrix: \n";
for(int i=0;i<rows1;i++)
{
for(int j=0;j<cols1;j++)
{
cout<<mat1[i][j]<<" ";
}
cout<<endl;
18
}
cout<<"2ns Matrix: \n";
for(int i=0;i<rows2;i++)
{
for(int j=0;j<cols2;j++)
{
cout<<mat2[i][j]<<" ";
}
cout<<endl;
}
cout<<"Result of Matrix multiplication:\n";
for(int i=0;i<rows1;i++)
{
for(int j=0;j<cols2;j++)
{
cout<<result[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
19
8. Program of Linear Search in one dimensional array.
#include<iostream>
using namespace std;
int linearsearchoned(int* arr, int size, int target)
{
for(int i=0; i<size; ++i)
{
if(arr[i]==target)
{
return i;//Return the index if the target is found
}
}
return -1;//Return -1 if the target is not found
}
int main()
{
int size;
cout<<"Ente the size of the array: ";
cin>>size;
int arr[size];
//Input elements into the array
cout<<"Enter"<<size<"Elements";
for(int i=0;i<size;++i)
{
cin>>arr[i];
}
int target;
cout<<"Enter element to search for: ";
cin>>target;
20
int result = linearsearchoned(arr, size, target);
if(result!=-1)
{
cout<<"Element is found at index: "<<result<<endl;
}
else
{
cout<<"Element is not found in the array."<<endl;
}
return 0;
}
21
9. Program of Linear Search in two dimensional array.
#include<iostream>
using namespace std;
//Function to perform linear search in 2D array
bool linearsearchtwod(int arr[][3], int rows, int cols, int target, int& foundrow, int&
foundcol)
{
for(int i=0; i<rows; ++i)
{
for(int j=0; j<cols; ++j)
{
if(arr[i][j]==target)
{
foundrow=i;
foundcol=j;
return true;//Return true if the target is found
}
}
}
return false;//Return false if the target is not found
}
int main()
{
const int rows=3;
const int cols=3;
int arr[rows][cols]={
{1,2,3},
{4,5,6},
{7,8,9}
22
};
int target;
cout<<"Enter the element to search for: ";
cin>>target;
int foundrow,foundcol;
cout<<"\n 2-D Array\n";
for(int i=0; i<rows; ++i)
{
for(int j=0; j<cols; ++j)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl;
}
if(linearsearchtwod(arr, rows, cols, target, foundrow, foundcol))
{
cout<<"Element is found at arr["<<foundrow<<"]["<<foundcol<<"]"<<endl;
}
else
{
cout<<"Element is not found in the array."<<endl;
}
return 0;
}
23
10. Program of converting pointer to string.
#include<iostream>
using namespace std;
int main()
{
char str[]="Hello, Pointer!";
char*ptr = str;//Pointer to string
cout<<"String usnig pointer: ";
while(*ptr != '\0')//Loop until the end of the string
{
cout<<*ptr;
ptr++;//Move to the next character
}
cout<<endl;
return 0;
}
24
11. Program of Pointer to reverse sentence using
recursion
#include<iostream>
using namespace std;
void reversesentence(string sentence)
{
if(sentence.length()==0)
{
return;
}
else
{
cout<<sentence.back();//Print the last character
//Recursive call with the rest of the string
reversesentence(sentence.substr(0,sentence.length()-1));
}
}
int main()
{
string sentence;//CPP string variable
cout<<"Enter a sentence: ";
getline(cin,sentence);//Input a sentence
cout<<"Reversed sentence: ";
reversesentence(sentence);//Call the recursive function
cout<<endl;
return 0;
}
25
26
12. Program of Dynamic Memory Management.
//Dynamic memory management
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int n,i;
int *p;
cout << "Enter number of elements: ";
cin >> n;
p = (int*)malloc(n * sizeof(int));
if (p == nullptr)
{
cout << "Memory allocation failed!" << endl;
return 1;
}
cout << "Memory allocated using malloc()." << endl;
cout << "Initial Elements are: ";
for (i = 0; i < n; ++i)
{
cout << p[i] << " ";
}
cout << endl;
cout << "Enter " << n << " elements: ";
for (i = 0; i < n; ++i)
{
cin >> p[i];
}
27
cout << "Entered elements are: ";
for (i = 0; i < n; ++i)
{
cout << p[i] << " ";
}
cout << endl;
p = (int*)calloc(n, sizeof(int));
if (p == nullptr)
{
cout << "Memory allocation failed!" << endl;
return 1;
}
cout << "Memory allocated using calloc()." << endl;
cout << "Initialized elements are: ";
for (i = 0; i < n; ++i)
{
cout << p[i] << " ";
}
cout << endl;
cout << "Enter " << n << " elements in array: ";
for (i = 0; i < n; ++i)
{
cin >> p[i];
}
cout << "Array elements are: ";
for (i = 0; i < n; ++i)
{
cout << p[i] << " ";
}
28
cout << endl;
int new_size;
cout << "Enter new size for the array: ";
cin >> new_size;
p = (int*)realloc(p, new_size * sizeof(int));
if (p == nullptr)
{
cout << "Memory reallocation failed!" << endl;
return 1;
}
cout << "Memory reallocated using realloc()." << endl;
if (new_size - n > 0)
{
cout << "Enter " << new_size - n<< " elements: ";
for (i = n; i < new_size; ++i)
{
cin >> p[i];
}
}
29
}
30
13. Program to Stack Operation.
#include<iostream>
#include<cstdlib>
#include<conio.h>
#define MAX 5
using namespace std;
int top=-1,stack[MAX];
void push(int);
void pop();
void peek();
void display();
int main()
{
int op,item;
while(1)
{
system("CLS");
cout<<"1. Insert a record at the top(PUSH)\n";
cout<<"2. Remove a record from the Top(POP)\n";
cout<<"3. Access a record from the top without removing(PEEK)\n";
cout<<"4. Display Stack\n";
cout<<"5. Exit\n";
cin>>op;
cout<<endl;
switch(op)
{
case 1:
cout<<"\n Enter the record that you wish to insert at the
top\n";
31
cin>>item;
push(item);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
cout<<"\n Thanks for stack implementation program!";
exit(1);
}
system("pause");
}
}
void push(int val)
{
if(top==MAX-1)
{
cout<<"\n Stack Overflow!\n";
return;
}
top+=1;
stack[top]=val;
cout<<"\n"<<val<<"was added to the stack\n";
32
}
void pop()
{
int val;
if(top==-1)
{
cout<<"\n Underflow!";
return;
}
val=stack[top];
top-=1;
cout<<"\n"<<val<<"was removed from the top.\n";
}
void peek()
{
int val;
if(top==-1)
{
cout<<"\n Stack is empty\n";
return;
}
val=stack[top];
cout<<"\n Record at the top of the stack: "<<val<<".\n";
}
void display()
{
if(top==-1)
{
cout<<"\n Stack is empty\n";
33
return;
}
int i=0;
cout<<"Stack: \n";
for(i=top;i>=0;i--)
{
cout<<"\t"<<stack[i]<<endl;
}
}
34
35
14. Program of Multiple Stack.
#include<iostream>
#define MAX 10
using namespace std;
int stack[MAX],topA=-1,topB=MAX;
void push_stackA(int val)
{
if(topA==topB-1)
{
cout<<"\n Stack-A Overflow!!";
}
else
{
topA+=1;
stack[topA]=val;
}
}
int pop_stackA()
{
int val;
if(topA==-1)
{
cout<<"\n Stack-A Unerflow!!";
}
else
{
val=stack[topA];
topA--;
cout<<"\n The value popped from the Stack-A= "<<val;
36
}
return val;
}
void display_stackA()
{
int i;
if(topA==-1)
{
cout<<"\n Empty Stack A";
}
else
{
for(i=topA;i>=0;i--)
{
cout<<"\t"<<stack[i];
}
}
}
void push_stackB(int val)
{
if(topB-1==topA)
{
cout<<"\n Stack-B Overflow!!";
}
else
{
topB-=1;
stack[topB]=val;
}
37
}
int pop_stackB()
{
int val;
if(topB==MAX)
{
cout<<"\n Stack- Underflow!!";
}
else
{
val=stack[topB];
topB++;
cout<<"\n The value popped from stack-B= "<<val;
}
}
void display_stackB()
{
int i;
if(topB==MAX)
{
cout<<"\n Empty Stack-B";
}
else
{
for(i=topB;i<MAX;i++)
{
cout<<"\t"<<stack[i];
}
}
38
}
int main()
{
int option,val;
do
{
system("cls");
cout<<"--------MENU--------";
cout<<"\n Enter 1 to Push a element into stack - A";
cout<<"\n Enter 2 to Push a element into stack - B";
cout<<"\n Enter 3 to Pop a element from stack - A";
cout<<"\n Enter 4 to Pop a element from stack - B";
cout<<"\n Enter 5 to display the stack - A";
cout<<"\n Enter 6 to display the stack - B";
cout<<"\n Enter 7 to exit";
cout<<"\n Enter your choice";
cin>>option;
switch(option)
{
case 1:
cout<<"\n Enter a value to Push on stack-A";
cin>>val;
push_stackA(val);
break;
case 2:
cout<<"\n ENter a value to Push on stack-B";
cin>>val;
push_stackB(val);
break;
39
case 3:
pop_stackA();
break;
case 4:
pop_stackB();
break;
case 5:
cout<<"\n The stack-A elements are: \n";
display_stackA();
break;
case 6:
cout<<"\n The stack-B elements are: \n";
display_stackB();
break;
}
cout<<endl;
system("pause");
}
while(option!=7);
return 0;
}
40
15. Program of Linear Queue.
#include<iostream>
#include<conio.h>
#define MAX 5
using namespace std;
int main()
{
int L_que[MAX],rear=-1,front=-1,op,i,item;
while(1)
{
system("cls");
cout<<"\n 1. Insertion";
cout<<"\n 2. Deletion";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cout<<"\n Enter your option";
cin>>op;
switch(op)
{
case 1:
cout<<"\n Enter item to insert";
cin>>item;
if(front>=0 && rear==MAX-1)
{
cout<<"\n Overflow!";
break;
}
else if(front==-1 && rear==-1)
{
41
front=0;
rear=0;
L_que[rear]=item;
}
else
{
rear=rear+1;
L_que[rear]=item;
}
break;
case 2:
if(front==-1)
{
cout<<"\n Overflow!!";
break;
}
else if(front==rear)
{
item=L_que[front];
front=-1;
rear=-1;
}
else
{
item=L_que[front];
front=front+1;
}
cout<<"\n Deleted Element = "<<item;
break;
42
case 3:
if(front==-1)
{
cout<<"\n Linear queue is empty";
break;
}
else
{
cout<"\n Linear Queue: ";
for(int i=front;i<=rear;i++)
{
cout<<"\t"<<L_que[i];
}
}
break;
case 4:
exit(1);
}
getch();
}
return 0;
}
43
16. Program of Circular Queue.
#include<iostream>
using namespace std;
int main()
{
int item,q[7],front=-1,rear=-1,op,i;
do
{
cout<<"\n 1. Insert Element";
cout<<"\n 2. Delete Element";
cout<<"\n 3. Display Elements";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice";
cin>>op;
switch(op)
{
case 1:
cout<<"\n Enter item to be inserted ";
cin>>item;
if((front==0 && rear==6) || (front==rear+1))
{
cout<<"\n Overflow!";
goto st1;
}
else if(rear==-1)
{
rear=0;
front=0;
}
44
else if(rear==6 && front>0)
{
rear=0;
}
else
{
rear=rear+1;
}
q[rear]=item;
st1:
break;
case 2:
if(rear==-1)
{
cout<<"\n Underflow!";
goto st;
}
item=q[front];
cout<<"\n Deleted Element = "<<item;
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==6 && rear<front)
{
front=0;
}
else
45
{
front=front+1;
}
st://Label
break;
case 3:
if(rear==-1)
{
cout<<"\n Queue is empty";
}
else
{
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
cout<<q[i]<<"\t";
}
}
else
{
for(i=front;i<=6;i++)
{
cout<<q[i]<<"\t";
}
for(i=0;i<=rear;i++)
{
cout<<q[i]<<"\t";
}
46
}
}
break;
case 4:
exit(1);
default:
cout<<"Enter valid input from 1-3";
}
cin.get();
}
while(1);
}
47
17. Program of Double Ended Queue.
#include<iostream>
#include<conio.h>
#define MAX 5
using namespace std;
int item,left1=-1,right1=-1,q[MAX],i;
string st1;
int main()
{
int op;
do
{
cout<<"\n 1. Insert element";
cout<<"\n 2. Delete element";
cout<<"\n 3. Display elements";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice: ";
cin>>op;
switch(op)
{
case 1:
cout<<"\n ENter iem to be inserted ";
cin>>item;
if(left1==0 && right1==MAX-1)
{
cout<<"\n Overflow!";
}
else if(left1==-1 && right1==-1)
{
48
left1=0;
right1=0;
q[left1]=item;
}
else if(left1>0 && right1==MAX-1)
{
left1=left1-1;
q[left1]=item;
}
else if(left==0 && right1<MAX-1)
{
right1=right1+1;
q[right1]=item;
}
else
{
cout<<"\n Choose the location either left or right";
cin>>st1;
if(st1=="left"||st1=="LEFT")
{
left1=left1-1;
q[left1]=item;
}
else
{
right1=right1+1;
q[right1]=item;
}
}
49
break;
case 2:
if(left1==-1 && right1==-1)
{
cout<<"\n Underflow!";
}
else if(left1==right1)
{
cout<<"\n Item Deleted = "<<q[right1];
left1=-1;
right1=-1;
}
else
{
cout<<"\n Choode the location either left or right";
cin>>st1;
if(st1=="right"||st1=="RIGHT")
{
cout<<"\n Item deleted = "<<q[right1];
right1=right1-1;
}
else
{
cout<<"\n Item deleted = "<<q[left1];
left1=left1+1;
}
}
break;
case 3:
50
if(left1==-1)
{
cout<<"\n Doubled Ended Queue is empty";
}
else
{
cout<<"\nRequired Elements are: ";
for(i=left1;i<=right1;i++)
{
cout<<q[i];
cout<<"\t";
}
}
break;
case 4:
exit(1);
default:
cout<<"\n Enter valid choice between 1-3";
}
getch();
}
while(1);
return 0;
}
51
52
18. Program of Priority Queue.
using namespace std;
#include<iostream>
#include<conio.h>
#define MAX 5
int main()
{
int p_que[MAX],pr[MAX],p,loc,ct=-1,op,i,item;
while(1)
{
system("cls");
cout<<"\n 1.Insertion:";
cout<<"\n 2.Deletion";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter option:";
cin>>op;
switch(op)
{
case 1:
cout<<"\n Enter item to insert:";
cin>>item;
if(ct==MAX-1)
{
cout<<"\n Overflow";
break;
}
else if(ct==-1)
{
53
ct=0;
pr[ct]=1;
p_que[ct]=item;
}
else
{
loc=-1;
cout<<"\n Enter priority of item b/w 1 to "<<MAX<<" :";
cin>>p;
for(i=0;i<=ct;i++)
{
if(p<pr[i])
{
loc=i;
for(i=ct;i>=loc;i--)
{
pr[i+1]=pr[i];
p_que[i+1]=p_que[i];
}
pr[i+1]=p;
p_que[i+1]=item;
ct=ct+1;
break;
}
}
if(loc==-1)
{
ct=ct+1;
pr[ct]=p;
54
p_que[ct]=item;
}
}
break;
case 2:
if(ct==-1)
{
cout<<"\n Underflow";
break;
}
else if(ct==0)
{
item=p_que[ct];
cout<<"\nDeleted item:"<<item;
ct=-1;
}
else
{
item=p_que[0];
cout<<"\n Deleted element="<<item;
for(i=0;i<ct;i++)
{
pr[i]=pr[i+1];
p_que[i]=p_que[i+1];
}
ct=ct-1;
}
break;
case 3:
55
if(ct==-1)
{
cout<<"\n Priority queue is empty";
break;
}
else
{
cout<<"\n Priority queue\n";
for(i=0;i<=ct;i++)
{
cout<<"\t"<<p_que[i]<<"\t with priority value="<<pr[i];
cout<<"\n";
}
}
break;
case 4:
exit(1);
}
getch();
}
}
56
19. Program of Merge Sort.
#include<stdio.h>
#include<conio.h>
void merge_pass(int [],int,int,int);
void merge_sort(int [],int,int);
void merge_pass(int l[],int top,int size,int bottom)
{
int temp[100];
int f=top;
int s=size+1;
int t=top;
int upper;
while((f<=size)&&(s<=bottom))
{
if(l[f]<=l[s])
{
temp[t]=l[f];
f++;
}
else
{
temp[t]=l[s];
s++;
}
t++;
}
if(f<=size)
{
for(f=f; f<=size; f++)
57
{
temp[t]=l[f];
t++;
}
}
else
{
for(s=s; s<=bottom; s++)
{
temp[t]=l[s];
t++;
}
}
for(upper=top; upper<=bottom; upper++)
{
l[upper]=temp[upper];
}
}
58
void main()
{
int a[100], i, n;
printf("\n Enter limit of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter %d no.",i+1);
scanf("%d",&a[i]);
}
printf("\n Unsorted list as follows:\n\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
i=0;
merge_sort(a,i,n-1);
printf("\n Sorted list as follows:\n\n");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}
59
20. Program of Quick sort.
#include<iostream>
using namespace std;
int a[20], n;
// Function to perform quicksort
void quickSort(int a[], int lb, int ub);
int partition(int a[], int lb, int ub);
int main()
{
int i;
cout << "Enter the size of array: ";
cin >> n;
cout << "Enter the elements of array:\n";
for (i = 0; i < n; i++)
{
cin >> a[i];
}
// Call quickSort function
quickSort(a, 0, n - 1);
cout << "Array after sorting is: ";
for (i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
// Function to perform quick sort
void quickSort(int a[], int lb, int ub)
60
{
if (lb < ub)
{
int p = partition(a, lb, ub);
quickSort(a, lb, p - 1);
quickSort(a, p + 1, ub);
}
}
// Partition function to place pivot element in the correct position
int partition(int a[], int lb, int ub)
{
int pivot = a[lb];
int left = lb;
int right = ub;
int temp;
while (left < right)
{
// Move right pointer until you find element smaller than pivot
while (a[right] > pivot)
right--;
// Move left pointer until you find element greater than pivot
while (a[left] <= pivot && left < right)
left++;
// Swap elements at left and right pointers
if (left < right)
{
temp = a[left];
a[left] = a[right];
a[right] = temp;
61
}
}
// Swap pivot with element at right pointer
a[lb] = a[right];
a[right] = pivot;
return right;
}
62
21. Program to calculate length of the string using user-
defined function.
#include<iostream>
using namespace std;
int stringLength(const char str[])
{
int length = 0;
// Increment until null terminator
while (str[length] != '\0')
{
length++;
}
return length; // Return the length of the string
}
int main()
{
char str[100]; // Array to hold the string
cout << "Enter a string: ";
cin.getline(str, 100); //Get the string input from user
// Call the user-defined function
int length = stringLength(str);
// Output the length of the string
cout << "Length of the string = " << length << endl;
return 0;
}
63
22. Program to concatenate and compare two string using
user-defined function.
#include<iostream>
using namespace std;
void concatenateStrings(const char str1[], const char str2[],char result[])
{
int i = 0, j = 0;
// Copying first string to result
while (str1[i] != '\0')
{
result[j++] = str1[i++];
}
// Copying second string to result
i = 0; // Reset index for the second string
while (str2[i] != '\0')
{
result[j++] = str2[i++];
}
result[j] = '\0'; // Null-terminate the concatenated string
}
// User-defined function to compare two strings
int compareStrings(const char str1[], const char str2[])
{
int i = 0;
// Compare each character of both strings
while (str1[i] != '\0' && str2[i] != '\0')
{
if (str1[i] != str2[i])
{
64
// Return -1 if str1 < str2, 1 if str1 > str2
return (str1[i] < str2[i]) ? -1 : 1;
}
i++;
}
// If one string ends first, determine which is shorter
if (str1[i] != '\0')
{
return 1; // str1 is longer
}
else if (str2[i] != '\0')
{
return -1; // str2 is longer
}
return 0; // Both strings are equal
}
int main()
{
// Arrays to hold the strings
char str1[100], str2[100], result[200];
// Input for first string
cout << "Enter the first string: ";
cin.getline(str1, 100);
// Input for second string
cout << "Enter the second string: ";
cin.getline(str2, 100);
// Concatenating strings
concatenateStrings(str1, str2, result);
cout << "Concatenated String: " << result << endl;
65
// Comparing strings
int comparisonResult = compareStrings(str1, str2);
if (comparisonResult == 0)
{
cout << "Both strings are equal." << endl;
}
else if (comparisonResult < 0)
{
cout<<"First string is less than the second string.";
}
else
{
cout<<"First string is greater than the second string.";
}
return 0;
}
66
23. Program to find the number of vowels, consonants,
digits and white space in a string.
#include<iostream>
using namespace std;
void countCharacters(const string &input, int &vowelCount, int &consonantCount, int
&digitCount, int &whiteSpaceCount)
{
// Initialize counts
vowelCount = 0;
consonantCount = 0;
digitCount = 0;
whiteSpaceCount = 0;
//Check each char in the input string using a traditional for lo
for (size_t i = 0; i < input.length(); i++)
{
char ch = input[i];
if (isspace(ch))
{
whiteSpaceCount++;
}
else if (isdigit(ch))
{
digitCount++;
}
else if (isalpha(ch))
{
// Convert character to lowercase for uniformity
char lowerCh = tolower(ch);
if (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o'
|| lowerCh == 'u')
67
{
vowelCount++;
}
else
{
consonantCount++;
}
}
}
}
int main()
{
string input;
// Get input from the user
cout << "Enter a string: ";
getline(cin, input);
// Variables to hold counts
int vowels = 0, consonants = 0, digits = 0, whiteSpaces = 0;
// Call the function to count characters
countCharacters(input, vowels, consonants, digits, whiteSpaces);
// Output the counts
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
cout << "Digits: " << digits << endl;
cout << "Whitespace: " << whiteSpaces << endl;
return 0;
}
68
69
24. Program to find highest and lowest frequency character
in a string.
#include<iostream>
using namespace std;
void findFrequencyCharacters(const string &input, char &highestChar, char &lowestChar, int
&highestFreq, int &lowestFreq)
{
// Array to store the frequency of ASCII characters
int frequency[256] = {0};
// Count occurrences of each character
for (size_t i = 0; i < input.length(); i++)
{
if (isalpha(input[i]))
{
frequency[(unsigned char)input[i]]++;
}
}
highestFreq = 0; // Initialize highest frequency
// Initialize lowest freq to maximum possible len of the string
lowestFreq = input.length();
// Iterate through the frequency array to find highest and lowest frequency
characters
for (int i = 0; i < 256; i++)
{
if (frequency[i] > highestFreq)
{
highestFreq = frequency[i];
highestChar = (char)i; // Convert index back to char
}
if (frequency[i] > 0 && frequency[i] < lowestFreq)
70
{
lowestFreq = frequency[i];
lowestChar = (char)i; // Convert index back to char
}
}
}
int main()
{
string input;
// Get input from the user
cout << "Enter a string: ";
getline(cin, input);
// Variables to hold results
char highestChar, lowestChar;
int highestFreq, lowestFreq;
// Call the function to find frequency characters
findFrequencyCharacters(input, highestChar, lowestChar,highestFreq, lowestFreq);
// Output the results
cout << "Highest frequency character: '" << highestChar << "' with frequency: " <<
highestFreq << endl;
cout << "Lowest frequency character: '" << lowestChar << "' with frequency: " <<
lowestFreq << endl;
return 0;
}
71