ADALAB
ADALAB
No : 1
Date : 18-07-2024
Aim
Program
#include<iostream.h>
#include<conio.h>
void hanoi(int n,char sour, char ext,char des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<sour<<" to "<<des<<endl;
return;
}
hanoi(n-1,sour,des,ext);
cout<<"Move Disk "<<n<<" from "<<sour<<" to "<<des<<endl;
hanoi(n-1,ext,sour,des);
}
int main()
{
int n;
clrscr();
cout<<"Enter no. of disks:";
cin>>n;
hanoi(n,'A','B','C');
getch();
return 0;
}
OUTPUT:
Result
Thus the program was executed successfylly and the output was verified.
Ex. NO. : 2 Traverse through binary search tree Pg. No : 2
Aim
To write a program to traverse through binary search tree using traversals.
Program
#include<iostream.h>
#include<conio.h>
class node
{
public:
int data;
node *lchild;
node *rchild;
};
class binarytree
{
public:
binarytree()
{
node *root=NULL;}
void insert(node *&root, int val)
{
if(root ==NULL)
{
root=new node;
root->data=val;
root->lchild=root->rchild=NULL;
}
else if(val< root->data)
{
insert(root->lchild,val);
}
else
{
insert(root->rchild,val);
}
}
void preorder(node *root)
{
if(root !=NULL)
{
cout<<root->data<<" "; Pg. No : 4
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(node *root)
{
if(root !=NULL)
{
inorder(root->lchild);
cout<<root->data<<" ";
inorder(root->rchild);
}
}
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
cout<<root->data<<" ";
}
}
};
int main()
{
clrscr();
int ch,val;
node *root=NULL;
binarytree b;
do
{
cout<<"\n1.INSERTION";
cout<<"\n2.Preorder";
cout<<"\n3.Postorder";
cout<<"\n4.Inorder";
cout<<"\n5.EXIT";
cout<<"\nEnter your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the value for insertion : ";
cin>>val;
b.insert(root,val);
break;
case 2:
cout<<"\nPreorder Traversal";
b.preorder(root); Pg. No : 5
break;
case 3:
cout<<"\nPostorder Traversal";
b.postorder(root);
break;
case 4:
cout<<"\nInorder Traversal";
b.inorder(root);
break;
default:
cout<<"\nInvalid";
return 0;
}
}while(ch!=4);
getch();
OUTPUT:
1.Insert
2.Preorder
3.Inorder
4.Postorder
5.Exit
Result
Thus the program was executed successfully and the output was verified.
EX. No. : 3 Stack Using Linked List Pg. No : 7
Date :
Aim
To write a program to perform various operation on stack using linked list.
Program
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
int top;
node* link;
};
struct node *top=NULL;
struct node *temp=new node;
void push( int val)
{
temp=new node;
temp->data=val;
temp->link=top;
top=temp;
}
void pop(int val)
{
if(top==0)
{
cout<<"Stack is empty";
}
else
{
val=top->data;
cout<<"Deleted Value is : "<<val;
temp=top;
top=top->link;
}
}
void main()
{
clrscr();
int ch,val;
cout<<"\n\t\tSTACK USING LINKED LIST ";
cout<<"\n1.PUSH OPERATION";
cout<<"\n2.POP OPERATION";
cout<<"\n3.DISPLAY";
cout<<"/nEnter Your Choice :";
cin>>ch;
switch(ch) Pg. No : 8
{
case 1:
push(val);
break;
case 2:
pop(val);
break;
case 3:
display();
default:
cout<<"Invalid Choice";
}
getch();
}
Output
STACK USING LINKED LIST
1.PUSH OPERATION
2.POP OPERATION
3.DISPLAY
Enter your choice: 1
Enter the value: 55
Stack elements are: 55
Result
Thus the program was executed successfully and the output was verified.
EX.NO. : 4 Circular Queue Pg. No : 10
Date :
Aim
To write a program to perform various operation in Circular Queue.
Program
#include<iostream.h>
#include<conio.h>
int f=-1,r=-1;
class circularqueue
{
int queue[50];
public:
void insert(int val,int n)
{
if (((r+1)%n)==f)
{
cout<<"Queue is full\n";
}
else if((f==-1)&&(r==-1))
{
f=0;r=0;
queue[r]=val;
}
else
{
r=((r+1)%n);
queue[r]=val;
}
}
void del(int n)
{
if((f==-1)&& (r==-1))
{
cout<<"Queue is empty \n";
}
else if(f==r)
{
int x=queue[f];
cout<<"Deleted value is : "<<x;
cout<<"\nQueue is empty\n";
f=-1;r=-1;
}
else
{ Pg. No : 11
int x=queue[f];
cout<<"Deleted value is : "<<x;
f=(f+1) % n;
}
}
void display(int n) {
int i;
if (f==-1&&r==-1) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout<<"Displaying"<<endl;
Output
Enter Queue size : 3
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 1
Input for insertion : 20
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 1
Input for insertion : 30
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 1
Input for insertion : 89
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 1
Input for insertion : 75
Queue is full
Pg. No : 13
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 2
Deleting
Deleted value is : 20
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 2
Deleting
Deleted value is : 30
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 3
Displaying
Circular Queue items -> 89
1.Insert
2.Delete
3.Display
4.Exit
Enter choice : 2
Deleting
Deleted value is 89
Queue is empty
Result
Thus the program was executed successfully and the output was verified.
EX. NO. : 5 Quick Sort
Date : Pg. No : 14
Aim
To write a program to perform sort an array of an elements using quick sort.
Program
#include<iostream.h>
#include<conio.h>
int i,j,p;
void swap(int*i,int*j)
{
int temp=*i;
*i=*j;
*j=temp;
}
partition(int a[],int l,int h)
{
i=l,j=h;
p=a[i];
while(i<j)
{
while(p>=a[i])
{
i++;
}
while(p<a[j])
{
j--;
}
if(i<j)
swap(&a[i],&a[j]);
}
swap(&a[l],&a[j]);
return j;
}
void quicksort(int a[],int l,int h)
{
if(l<h)
{
int loc=partition(a,l,h);
quicksort(a,l,loc-1);
quicksort(a,loc+1,h);
}
}
void main()
{
int n,i,a[50]; Pg. No : 15
clrscr();
cout<<"Enter the size:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
cout<<"Sorted elements are:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}
OUTPUT
Enter the Size ; 5
Enter the elements
2
3
16
89
32
Sorted elements are :
2 3 16 32 89
Result
Thus the program was executed successfully and the output was verified.
Aim
To write a program to sort number of elements in ascending order using heap sort.
Program
#include <iostream.h>
#include <conio.h>
void maxHeapify(int arr[], int n, int i)
{
int h = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[h])
h = l;
if (r < n && arr[r] > arr[h])
h = r;
if (h != i)
{
int temp = arr[i];
arr[i] = arr[h];
arr[h] = temp;
maxHeapify(arr, n, h);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
maxHeapify(arr, n, i);
for (i = n - 1; i >= 0; i--)
{
Output
Result
Thus the program was executed successfully and the output was verified.
Aim
To write a program to perform the knap sack problem using greedy method.
Program
#include<conio.h>
#include <iostream.h>
int main()
{
float weight[50], profit[50], ratio[50], totalValue = 0, capacity, temp;
int n,i;
cout << "Enter the number of items: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter Weight and Profit for item[" << i << "]:\n";
cin >> weight[i] >> profit[i];
}
cout << "Enter the capacity of the knapsack:\n";
cin >> capacity;
for (i = 0; i < n; i++)
ratio[i] = profit[i] / weight[i];
for (i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
cout << "Knapsack problem using Greedy Algorithm:\n";
for (i = 0; i < n; i++) {
if (weight[i] > capacity)
break; Pg. No : 12
else {
totalValue += profit[i];
capacity -= weight[i];
}
}
if (capacity > 0 && i < n) {
totalValue += ratio[i] * capacity;
}
cout << "\nThe maximum value is: " << totalValue << endl;
return 0;
getch();
}
Output
Enter the number of items: 4
Enter Weight and Profit for item [0]:
40 280
Enter Weight and Profit for item [1]:
10 100
Enter Weight and Profit for item [2]:
20 120
Enter Weight and Profit for item [3]:
24 120
Enter the capacity of the knapsack :
60
Knapsack problem using Greedy Algorithm:
The maximum value is: 440
Result
Thus the program was executed successfully and the output was verified.
EX.NO : 8 Search an element using Pg. No : 12
Date : Divide & Conquer
Aim
To write a program to search for an element using divide and conquer strategy.
Program
#include<iostream.h>
#include<conio.h>
int bsearch(int a[],int l,int h,int x)
{
while(l<=h)
{
int m=(l+h)/2;
if(a[m]==x)
return m;
if(x<a[m])
h=m-1;
else
l=m+1;
}
return -1;
}
void main()
{
int a[50],n,i,x,p;
clrscr();
cout<<"Enter the size:";
cin>>n;
cout<<"Enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter the x element:";
cin>>x;
p=bsearch(a,0,n-1,x);
if(p==-1)
cout<<"\nNot found";
else
cout<<"\nThe element position is:"<<p;
getch();
}
Output Pg. No : 12
Enter the array size: 5
Enter 5 elements:
33
45
23
56
12
Enter the X element : 23
The element Position is: 3
Result
Thus the program was executed successfully and the output was verified.
EX.NO. : 9 N Queens Problem Pg. No : 12
Date :
Aim
To write a program to place the N Queens on an n*n matrix so that no two queens attack.
Program
#include<iostream.h>
#include<conio.h>
#define MAX 10
int board[MAX][MAX];
void printSolution(int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1)
cout << "Q ";
else
cout << ". ";
}
cout << endl;
}
cout << endl;
}
int isSafe(int row, int col, int N) {
for (int i = 0; i < row; i++)
if (board[i][col] == 1)
return 0;
for (int i1 = row, j1 = col; i1 >= 0 && j1 >= 0; i1--, j1--)
if (board[i1][j1] == 1)
return 0;
if (!solveNQueens(0, N))
cout << "No solutions exist for " << N << "-Queens problem." << endl;
return 0;
getch();
}
Output
Enter the value of N (size of the chessboard): 4
. Q. .
. . . Q
Q . . .
. . Q .
. . Q .
Q . . .
. . . Q
. Q. .
Result
Thus the program was executed successfully and the output was verified.
Pg. No : 12
EX. NO. : 10 Virtual Function
Date :
Aim
Program
#include<iostream.h>
#include<conio.h>
class passenger
{
public:
virtual void ticket()=0;//pure virtual function and also abstrac class because no definition
};
class economyclass:public passenger
{
public:
void ticket()
{
int amount=100;
cout<<"\nCOST FOR BOOKING ECONOMY CLASS : $"<<amount;
}
void info()
{
cout<<"\n\nSTOP Searching START Travelling";
}
};
void main()
{
clrscr();
cout<<"\t\t**WELCOME TO AIRLINE**"<<endl;
economyclass e;//creating object for derived class without pointer
e.ticket();
passenger *p;
p=&e; Pg. No : 12
p->info();
cout<<"\n\n\t\tTHANK YOU";
getch();
}
Output
** WELCOME TO AIRLINE**
COST FOR BOOKING ECONOMY CLASS : $100
STOP Searching START Travelling
THANK YOU
Result
Thus the program was executed successfully and the output was verified.
Pg. No : 12
EX. NO. : 11 Parameterized Constructor
Date :
Aim
To write a program to perform parameterized constructor.
Program
#include<iostream.h>
#include<conio.h>
class mall
{
public:
mall(char name[],int floors,int countemp)
{
cout<<"\nName of the Mall : "<<name;
cout<<"\nNumber of floors in mall : "<<floors;
cout<<"\nNumber of employee : "<<countemp;
}
};
void main()
{
clrscr();
char mname[20];
int floor,emp;
cout<<"Enter name of the mall : ";
cin>>mname;
cout<<"Enter number of floors : ";
cin>>floor;
cout<<"Enter number of employee : ";
cin>>emp;
cout<<"\n\t\t_______MALL MANAGEMENT________";
mall m(mname,floor,emp);
getch();
}
Output Pg. No : 12
_______MALL MANAGEMENT________
Result
Thus the program was executed successfully and the output was verified.
EX.NO. : 12 Friend Function Pg. No : 12
Date :
Aim
To write a program to perform friend function.
Program
#include<iostream.h>
#include<conio.h>
class shop
{
private:
int i,a;
public:
void get()
{
cout<<"\nEnter number of items buy : ";
cin>>i;
}
friend int amount(shop);
};
int amount(shop c)
{
c.a=c.i*10;
cout<<"\nTotal Amount : "<<c.a;
return 0;
}
void main()
{
clrscr();
shop s;
cout<<"\n\t***Ten Rupee Shop***";
s.get();
amount(s);
getch();
}
Pg. No : 12
Output
Result
Thus the program was executed successfully and the output was verified.
Pg. No : 12
EX. NO. : 13 Function Overloading
Date :
Aim
To write a program to perform Function Overloading.
Program
#include<iostream.h>
#include<conio.h>
class math
{
public:
int area(int s)
{
return (s*s);
}
int area(int l,int b)
{
return (l*b);
}
float area(float r)
{
return (3.14*r*r);
}
float area(float b,float h)
{
return(((b*h)/2));
}
};
void main()
{
clrscr();
int s,l,b;
float r,tb,h;
math m;
cout<<"Calculating area by using function overloading";
cout<<"\nEnter Side of a square : ";
cin>>s;
cout<<"Area of square is : "<<m.area(s);
cout<<"\nEnter length and breadth of the rectangle : ";
Pg. No : 12
cin>>l>>b;
cout<<"Area of rectangle is : "<<m.area(l,b);
cout<<"\nEnter radius of circle : ";
cin>>r;
cout<<"Area of circle is " <<m.area(r);
cout<<"\nEnter base and height of triangle : ";
cin>>tb>>h;
cout<<"Area of triangle is : " <<m.area(tb,h);
getch();
}
Output
Result
Thus the program was executed successfully and the output was verified.
EX. NO. : 14 Single Inheritance Pg. No : 12
Date :
Aim
Program
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class school
{
public:
char sname[20];
char place[20];
void getsch()
{
cout<<"Enter School Name : ";
gets(sname);
cout<<"Enter Place : ";
gets(place);
}
};
class student:public school
{
public:
char name[20], regno[20];
int m1,m2,m3,m4,m5,t;
void getstud()
{
student s;
s.getsch();
s.getstud();
s.display();
getch();
}
Pg. No : 12
Output
PRINTING STUDENT DETAILS USING SINGLE INHERITANCE
Enter School Name : SHARON MATRICULATION SCHOOL
Enter Place : VILATHIKULAM
Enter Your Name : POOJA R
Enter Your Register no. : 1134
Enter Your Mark1 : 100
Enter Your Mark 2 : 100
Enter Your Mark 3: 100
Enter Your Mark 4 : 100
Enter Your Mark 5 : 100
____________STUDENT DETAILS_____________
Student Name : SHARON MATRICULATION SCHOOL
Register Number : 1134
Mark 1 : 100 Mark 2 : 100 Mark 3 : 100 Mark 4 : 100 Mark 5 : 100
Total Mark : 500
Result
Thus the program was executed successfully and the output was verified.
Aim
To write a program to perform employee details using files.
Program
#include <iostream.h>
#include <fstream.h>
#include<conio.h>
void addEmployee() {
ofstream outFile("employees.txt", ios::app);
if (!outFile) {
cout<< "Unable to open file for writing."<< endl;
return;
}
int id;
char name[50];
double salary;
cout << "Enter Employee ID: ";
cin >> id;
cin.ignore();
cout << "Enter Employee Name: ";
cin.getline(name, 50);
cout << "Enter Employee Salary: ";
cin >> salary;
outFile << id << endl;
outFile << name << endl;
outFile << salary << endl;
outFile.close();
cout << "Employee details added successfully." << endl;
}
void displayEmployees() {
ifstream inFile("employees.txt");
if (!inFile) {
cout<< "Unable to open file for reading." << endl;
return;
}
int id;
char name[50];
double salary;
inFile.close();
}
void main() {
clrscr();
int choice;
do {
cout << "\nEmployee Management System\n";
cout << "1. Add Employee\n";
cout << "2. Display Employees\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addEmployee();
break;
case 2:
displayEmployees();
break;
case 3:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 3);
getch();
}
Output
Result
Thus the program was executed successfully and the output was verified.