CPP Practical
CPP Practical
a
// WAP to store the elements in 1-D array and perform operations like searching, sorting and reverse the element
// reverse array
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Vidya Niketan Degree College"<<endl<<"SY-BSc.it"<<endl<<"Pratik"<<endl<<"Topic: Reverse
Array"<<endl<<"Output:-"<<endl;
int arr[50],size,i,j,temp;
cout<<"Enter the size of the array: ";
cin>>size;
cout<<"Enter the elements of the array: ";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
j=i-1;
i=0;
while(i<j){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
cout<<"The reversed array is: ";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
• 1.a.2
// linear search in array
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Vidya Niketan Degree College" << endl
<< "SY-BSc.it" << endl
<< "Pratik" << endl
<< "Topic: Linear Search" << endl
<< "Output:-" << endl;
int arr[10], i, num, n, c = 0, pos;
cout << "Enter the size of array: ";
cin >> n;
cout << "Enter the elements of array: ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << "Enter the number to be searched: ";
cin >> num;
for (i = 0; i < n; i++)
{
if (arr[i] == num)
{
c = 1;
pos = i + 1;
break;
}
}
if (c == 0)
{
cout << "Element not found";
}
else
{
cout << num << " Element found at position: " << pos;
}
return 0;
}
• 1.a.3
// sort element of array in ascending order
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Vidya Niketan Degree College" << endl
<< "SY-BSc.it" << endl
<< "Pratik" << endl
<< "Topic: Ascending Order in Array" << endl
<< "Output:-" << endl;
int i, a[10], temp, j;
cout << "Enter 10 elements of array: ";
for (i = 0; i < 10; i++)
{
cin >> a[i];
}
cout << endl
<< "Data before sorting: " << endl;
for (j = 0; j < 10; j++)
{
cout << a[j];
}
cout << endl;
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout << endl
<< "Data after sorting: " << endl;
for (j = 0; j < 10; j++)
{
cout << a[j] << " ";
}
return 0;
}
• 1.a.4
//sort array element in descending order
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Vidya Niketan Degree College"<<endl<<"SY-BSc.it"<<endl<<"Pratik"<<endl<<"Topic: Descending Order in
Array"<<endl<<"Output:-"<<endl;
int i,a[10],temp,j;
cout<<"Enter 10 elements\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Data before sorting: ";
for(i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"Data after sorting: ";
for(i=0;i<10;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
• 1.b
//merging of array
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Vidya Niketan Degree College"<<endl<<"SY-BSc.it"<<endl<<"Pratik"<<endl<<"Topic: Merging
Array"<<endl<<"Output:-"<<endl;
int arr1[50],arr2[50], size1,size2,size,i,j,merge[100];
cout<<"Enter the size of first array: ";
cin>>size1;
cout<<"Enter the elements of first array: ";
for(i=0;i<size1;i++)
{
cin>>arr1[i];
}
cout<<"Enter the size of second array: ";
cin>>size2;
cout<<"Enter the elements of second array: ";
for(i=0;i<size2;i++)
{
cin>>arr2[i];
}
for(i=0;i<size1;i++){
merge[i]=arr1[i];
}
size=size1+size2;
for(i=size1,j=0;i<size;i++,j++)
{
merge[i]=arr2[j];
}
cout<<"The merged array is: ";
for(i=0;i<size;i++)
{
cout<<merge[i]<<" ";
}
return 0;
}
• 1.c.1
//add two matrix
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Vidya Niketan Degree College"<<endl<<"SY-BSc.it"<<endl<<"Pratik"<<endl<<"Topic: Addition of two
Matrix"<<endl<<"Output:-"<<endl;
int mat1[3][3],mat2[3][3],i,j,mat3[3][3];
cout<<"Enter the elements of first matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter the elements of second matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat2[i][j];
}
}
cout<<"adding two matrix to from the third matrix";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
cout<<"The elements of third matrix are\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat3[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
• 1.c.2
//multiply two matrix
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Vidya Niketan Degree College"<<endl<<"SY-BSc.it"<<endl<<"Pratik"<<endl<<"Topic: Multiply two
Matrix"<<endl<<"Output:-"<<endl;
int mat1[3][3],mat2[3][3],mat3[3][3],sum=0,i,j,k;
cout<<"Enter the elements of first matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter the elements of second matrix\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat2[i][j];
}
}
cout<<"Multiplying of two matrices: "<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
for(k=0;k<3;k++){
sum=sum+mat1[i][k]*mat2[k][j];
}
mat3[i][j]=sum;
sum=0;
}
}
cout<<"The result of matrix multiplication is: "<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<mat3[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
• 1.c.3
//transpose matrix
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<"Vidya Niketan Degree College"<<endl<<"SY-BSc.it"<<endl<<"Pratik"<<endl<<"Topic: Transpose
Matrix"<<endl<<"Output:-"<<endl;
int arr[3][3],i,j,arrt[3][3];
cout<<"Enter the elements of the matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
cout<<"Transposing Array..."<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arrt[i][j]=arr[j][i];
}
}
cout<<"Transpose of the matrix is"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<arrt[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
• 2.a
// WAP to create a single linked list and display the node elements in reverse order
#include <bits/stdc++.h>
using namespace std;
struct node
{
int info;
struct node *next;
}
*start,
*newptr, *save, *ptr;
node *create_new_node(int);
void insert_at_bag(node *);
void display(node *);
int main()
{
cout << "Vidya Niketan Degree College" << endl
<< "SY-BSc.it" << endl
<< "Pratik" << endl
<< "Topic: create a single linked list and display the node elements in reverse order" << endl
<< "Output:-" << endl;
start = NULL;
int inf;
char ch = 'y';
while (ch == 'y' || ch == 'Y')
{
cout << "Enter information for the new node: ";
cin >> inf;
cout << "Creating new node..!!..Press any key to continue...";
newptr = create_new_node(inf);
if (newptr != NULL)
{
cout << endl
<< "New node created successfully..!!" << endl;
cout << "Press any key to continue...";
}
else
{
cout << "sorry can't create new node..!!..Aborting..!!" << endl;
cout << "Press any key to exit..";
exit(1);
}
cout << "Now inserting this node at beginning of the list.." << endl;
cout << "press any key to continue.." << endl;
insert_at_bag(newptr);
cout << " node successfully inserted at the beginning of the list.." << endl;
cout << "Now the list is:" << endl;
display(start);
cout << "Want to enter more nodes?(y/n): ";
cin >> ch;
}
return 0;
}
node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
if (p == NULL)
{
r->next = np;
np->prev = r;
np->next = NULL;
tail = np;
break;
}
else if (np->data < p->data)
{
r->next = np;
np->prev = r;
np->next = p;
p->prev = np;
if (p->next != NULL)
{
do
{
p = p->next;
} while (p->next != NULL);
}
tail = p;
break;
}
}
}
}
}
void traverse_tail()
{
node *t = tail;
while (t != NULL)
{
cout << t->data << "\t";
t = t->prev;
}
cout << endl;
}
void traverse_head()
{
node *t = head;
while (t != NULL)
{
cout << t->data << "\t";
t = t->next;
}
cout << endl;
}
int main()
{
cout << "Vidya Niketan Degree College" << endl
<< "SY-BSc.it" << endl
<< "Pratik" << endl
<< "Topic: create double linked list and sort element in the linked list " << endl
<< "Output:-" << endl;
int i = 0, n, x;
cout << "Enter the no of nodes: ";
cin >> n;
while (i < n)
{
cout << "Enter element " << i + 1 << ": ";
cin >> x;
create(x);
i++;
}
cout << "Traversing Doubly Linked List head first" << endl;
traverse_head();
cout << "Traversing Doubly Linked List tail first" << endl;
traverse_tail();
return 0;
}
• 3.a
//WAP to implement the comcept of stack with push,pop,display and exit operations
#include <bits/stdc++.h>
using namespace std;
class cla
{
int stk[5];
int top;
public:
cla()
{
top = -1;
}
void push(int x)
{
if (top > 4)
{
cout << "stack overflow" << endl;
return;
}
stk[++top] = x;
cout << "Inserted "<<x<<endl;;
return;
}
void pop()
{
if (top < 0)
{
cout << "stack underflow" << endl;
return;
}
cout << "deleted " << stk[top--]<<endl;
}
void display()
{
if (top < 0)
{
cout << "stack empty"<<endl;
return;
}
for (int i = top; i >= 0; i--)
{
cout << stk[i] << " ";
}
}
};
int main()
{
cla st;
int ch;
while (1)
{
cout<<endl;
cout << "1.push 2.pop 3.display 4.exit"<<endl;
cout << "Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1:
cout << "Enter the element: ";
cin >> ch;
st.push(ch);
break;
case 2:
st.pop();
break;
case 3:
st.display();
break;
case 4:
exit(0);
}
}
return 0;
}
• 3.b
// WAP to convert am infix expression to postfix and prefix conversion.
// convert infix expression to the postfix notation
#include <bits/stdc++.h>
using namespace std;
int getWeight(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
void infixToPostfix(char infix[], char postfix[], int size)
{
stack<char> s;
int weight, i = 0, k = 0;
char ch;
while (i < size)
{
ch = infix[i];
if (ch == '(')
{
s.push(ch);
i++;
continue;
}
if (ch == ')')
{
while (!s.empty() && s.top() != '(')
{
postfix[k++] = s.top();
s.pop();
}
if (!s.empty())
{
s.pop();
}
i++;
continue;
}
weight = getWeight(ch);
if (weight == 0)
{
postfix[k++] = ch;
}
else
{
if (s.empty())
{
s.push(ch);
}
else
{
while (!s.empty() && s.top() != '(' && weight <= getWeight(s.top()))
{
postfix[k++] = s.top();
s.pop();
}
s.push(ch);
}
}
i++;
}
while (!s.empty())
{
postfix[k++] = s.top();
s.pop();
}
postfix[k] = 0;
}
int main()
{
char infix[] = "A*(B+C)/D";
int size = strlen(infix);
char postfix[size];
infixToPostfix(infix, postfix, size);
cout << endl
<< "Infix Expression:: " << infix << endl;
cout << endl
<< "Postfix Expression ::" << postfix << endl;
cout << endl;
return 0;
}
• 3.c
// WAP to implement tower of hanoi problem
#include <bits/stdc++.h>
using namespace std;
void towers(int, char, char, char);
int main()
{
int num;
cout << "Enter the number of disks: ";
cin >> num;
cout << "The sequence of moves involved in the Tower of Hanoi are:" << endl;
towers(num, 'A', 'B', 'C');
return 0;
}
void towers(int n, char frompeg, char topeg, char auxpeg)
{
if (n == 1)
{
cout << "Move disk 1 from peg " << frompeg << " to peg " << topeg << endl;
return;
}
towers(n - 1, frompeg, auxpeg, topeg);
cout << "Move disk " << n << " from peg " << frompeg << " to peg " << topeg << endl;
towers(n - 1, auxpeg, topeg, frompeg);
}
• 4.a
// WAP to implement the concept of queue with insert delete display and exit operations
#include <bits/stdc++.h>
using namespace std;
class queueList
{
int queue1[5];
int front, rear;
public:
queueList()
{
front = -1;
rear = -1;
}
void insert(int x)
{
if (rear > 4)
{
cout << "Queue is full" << endl;
front = rear = -1;
return;
}
queue1[++rear] = x;
cout << "inseted " << x << endl;
}
void delet()
{
if (front == rear)
{
cout << "Queue is empty" << endl;
return;
}
cout << "deleted " << queue1[++front] << endl;
}
void display()
{
if (front == rear)
{
cout << "Queue is empty" << endl;
return;
}
for (int i = front + 1; i <= rear; i++)
{
cout << queue1[i] << " "<<endl;
}
}
};
int main()
{
queueList q;
int ch;
while (1)
{
cout << "1. insert " << endl
<< "2. Delete" << endl
<< "3. display" << endl
<< "4. exit" <<endl<< "Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1:
default:
break;
}
}
return 0;
}
• 4.b
// Wap to implement the concept of circular queue
#include <bits/stdc++.h>
using namespace std;
class cqueue
{
private:
int *arr;
int front, rear;
int MAX;
public:
cqueue(int maxsize = 10)
{
MAX = maxsize;
arr = new int[MAX];
front = rear = -1;
for (int i = 0; i < MAX; i++)
arr[i] = 0;
}
void addq(int item)
{
if ((rear + 1) % MAX == front)
{
cout << "Queue is full" << endl;
return;
}
rear = ((rear + 1) % MAX);
arr[rear] = item;
if (front == -1)
front = rear;
}
int delq()
{
int data;
if (front == -1)
{
cout << "Queue is empty" << endl;
return data;
}
data = arr[front];
arr[front] = 0;
if (front == rear)
{
front = rear = -1;
}
else
{
front = ((front + 1) % MAX);
// return data;
}
return data;
}
void display()
{
cout << endl;
for (int i = 0; i < MAX; i++)
cout << arr[i] << " ";
cout << endl;
}
};
int main()
{
cqueue a(10);
a.addq(14);
a.addq(22);
a.addq(13);
a.addq(-6);
a.addq(25);
cout<<endl<<"Elements in the circukar queue: "<<endl;
a.display();
int i=a.delq();
cout<<endl<<"Deleted element is: "<<i<<endl;
i=a.delq();
cout<<endl<<"Deleted element is: "<<i<<endl;
cout<<"Elements in the circular queue after deletation: "<<endl;
a.display();
a.addq(21);
a.addq(17);
a.addq(18);
a.addq(9);
a.addq(20);
cout<<"Elements in the circular queue after deletation: "<<endl;
a.display();
a.addq(32);
cout<<"Elements in the circular queue after deletation: "<<endl;
a.display();
return 0;
}
• 4.c
// WAP to implement the concept of deque
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
class node *next;
class node *prev;
};
class d1 : public node
{
node *head, *tail;
int top1, top2;
public:
d1()
{
head = NULL;
tail = NULL;
top1 = 0;
top2 = 0;
}
void push_front(int x)
{
node *temp;
int ch;
if (top1 + top2 >= 5)
{
cout << "Queue is full" << endl;
return;
}
if (top1 + top2 == 0)
{
head = new node;
head->data = x;
head->next = NULL;
head->prev = NULL;
tail = head;
top1++;
}
else
{
cout << "Add element 1.FIRST 2.LAST" << endl
<< "Enter your choice: ";
cin >> ch;
if (ch == 1)
{
temp = new node;
temp->data = x;
temp->next = head;
temp->prev = NULL;
head->prev = temp;
head = temp;
top1++;
}
else
{
temp = new node;
temp->data = x;
temp->next = NULL;
temp->prev = tail;
tail->next = temp;
tail = temp;
}
}
}
void pop()
{
int ch;
cout << "Delete 1.FIRST Node 2.LAST Node" << "Enter your choice: ";
cin >> ch;
if (top1 + top2 <= 0)
{
cout << "Queue is empty" << endl;
return;
}
if (ch == 1)
{
head = head->next;
head->prev = NULL;
top1--;
}
else{
top2--;
tail=tail->prev;
tail->next=NULL;
}
}
void display(){
int ch;
node *temp;
cout<<"Display from 1.Starting 2.Ending"<<endl<<"Enter your choice: ";
cin>>ch;
if(top1+top2<=0){
cout<<"Queue is empty"<<endl;
return;
}
if(ch==1){
temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
}else{
temp=tail;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->prev;
}
}
}
};
int main()
{
d1 q1;
int ch;
while (1)
{
cout<<"1.INSERT 2.DELETE 3.DISPLAY 4.EXIT"<<endl<<"Enter your choice: ";
cin>>ch;
switch (ch)
{
case 1:
cout<<"Enter element: ";
cin>>ch;
q1.push_front(ch);
break;
case 2:
q1.pop();break;
case 3:
q1.display();break;
case 4: exit(1);
}
}
return 0;
}
• 5.a
// WAP to implement bubble sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, i, j, temp;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
for (i = 1; i < n; ++i)
{
for (j = 0; j < n - i; ++j)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "ARRAY after bubble sort: " << endl;
for (i = 0; i < n; ++i)
{
cout << arr[i] << " ";
}
return 0;
}
• 5.b
// WAP to implement selection sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int size,temp;
cout<<"Enter the size of the array: ";
cin>>size;
int arr[size];
cout<<"Enter the elements of the array: ";
for(int i=0;i<size;i++){
cin>>arr[i];
}
cout<<"Sorting array using selection sort: "<<endl;
for(int i=0;i<size;i++){
for(int j=i+1;j<size;j++){
if(arr[i]>arr[j]){
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting: "<<endl;
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
return 0;
}
• 5.c
// WAP to implement insertion sort
#include <bits/stdc++.h>
using namespace std;
int main()
{
int size,temp;
cout<<"Enter the size of the array: ";
cin>>size;
int arr[size];
cout<<"Enter the elements of the array: ";
for(int i=0;i<size;i++){
cin>>arr[i];
}
cout<<"Sorting Array using insertion sort:"<<endl;
for(int i=1;i<size;i++){
temp=arr[i];
int j=i-1;
while(j>=0 && arr[j]>temp){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting: "<<endl;
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
return 0;
}
• 6.a
// WAP to implement merge sort
#include <bits/stdc++.h>
using namespace std;
void mergeSort(int[],int,int);
void merge(int[],int,int,int);
int main()
{
int p, r, i, n;
cout << "Enter the size of the array: ";
cin >> n;
int a[n];
p = 0;
r = n - 1;
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++)
{
cin >> a[i];
}
mergeSort(a, p, r);
cout << "The sorted array is: " << endl;
for (i = 0; i < n; i++)
{
cout << a[i] << " ";
}
return 0;
}
void mergeSort(int a[], int p, int r)
{
if (p < r)
{
int q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q + 1, r);
merge(a, p, q, r);
}
}
void merge(int a[], int p, int q, int r)
{
int c[r-p+1];
int i = p, j = q + 1;
int k = p;
while (i <= q && j <= r)
{
if (a[i] < a[j])
{
c[k] = a[i];
i++;
k++;
}
else
{
c[k] = a[j];
j++;
k++;
}
}
while (i <= q)
{
c[k] = a[i];
i++;
k++;
}
while (j <= r)
{
c[k] = a[j];
j++;
k++;
}
int l = p;
while (l <= r)
{
a[l] = c[l];
l++;
}
}
• 6.b
// WAP to search the element using sequential search
#include <bits/stdc++.h>
using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array: ";
cin>>size;
int arr[size],key,i;
for(int j=0;j<size;j++){
cout<<"Enter the element "<<j+1<<" : ";
cin>>arr[j];
}
for(int a=0;a<size;a++){
cout<<"array["<<a<<"] = "<<arr[a]<<endl;
}
cout<<"Enter key to search in array: ";
cin>>key;
for( i=0;i<size;i++){
if(arr[i]==key){
cout<<"Element found at index "<<i<<endl;
break;
}
}
if(i!=size){
cout<<"element found at"<<i;
}else{
cout<<"element not found";
}
return 0;
}
• 6.c
// WAP to search the elemenmt using binary search
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, i, key, low, high, mid;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for(i=0;i<n;i++){
cin >> arr[i];
}
cout << "Enter the element to be searched: ";
cin >> key;
low = 0;
high = n-1;
mid=((low+high)/2);
while(low<=high){
if(arr[mid]==key){
cout << "Element found at position: " << mid+1 << endl;
break;
}
else if(arr[mid]>key){
high = mid-1;
}
else{
low=mid+1;
}
mid=((low+high)/2);
}
if(low>high){
cout<<"Not Found!"<<key<<" is not present in the list";
}
return 0;
}