Practical

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Program-1

/*Write a program to search a number from an array using Binary Search.*/


#include<iostream>
using namespace std;

int Binary_Search(int a[], int N, int data)


{
int first,last,mid;
int found=0;
first=0;
last=N-1;
while(first<=last && found==0)
{
mid=int((first+last)/2);
if(a[mid]==data)
found=1;
if(a[mid]<data)
first=mid+1;
if(a[mid]>data)
last=mid-1;
}
return(found);
}
int main()
{
int x[10],data, i;
cout<<"\n Enter 10 numbers in ascending order:";
for(i=0;i<10;i++)
{
cin>>x[i];
}
cout<<"\n Enter No. to be searched:";
cin>>data;
int res=Binary_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
cout<<"\n not found";
return 0;
}

Page | 1
Output:

Page | 2
Program-2
/*Write a program to search a number from an array using Linear Search.*/
#include<iostream>
using namespace std;

int Linear_Search (int x[],int N,int data)


{
int i, found=0;
for(i=0;i<N;i++)
{
if(x[i]==data)
found=1;
}
return(found);
}
int main()
{
int x[10],data, i;
cout<<"\n Enter 10 numbers:";
for(i=0;i<10;i++)
{
cin>>x[i];
}
cout<<"\n Enter No. to be searched:";
cin>>data;
int res=Linear_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
cout<<"\n not found";
return 0;
}

Page | 3
Output:

Page | 4
Program-3
/* Write a program to sort an array using Bubble Sort.*/
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x[10],i,j,temp;
for(i=0;i<10;i++)
{
cout<<"\n Enter "<<i+1<<" value:\t";
cin>>x[i];
}
for(i=0;i<9;i++)
{
for(j=0;j<=(9-i);j++)
{
if(x[j]>x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
}
}
}
cout<<"\n Sorted Array";
for(i=0;i<10;i++)
{
cout<<setw(6)<<x[i];
}
return 0;
}

Page | 5
Output:

Page | 6
Program-4
/*Write a program to sort an array using Insertion Sort.*/
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x[10],i,j,temp;
cout<<"Enter 10 numbers to be sort:";
for(i=0;i<10;i++)
cin>>x[i];
for(i=1;i<10;i++)
{
temp=x[i];
j=i-1;
while(temp<x[j] && j>=0)
{
x[j+1]=x[j];
j=j-1;
}
x[j+1]=temp;
}
cout<<"\n sorted Array:";
for(i=0;i<10;i++)
cout<<setw(4)<<x[i];
return 0;
}

Page | 7
Output:

Page | 8
Program-5
/*Write a program to sort an array using Selection Sort.*/
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x[10],i,j,temp,low,pos;
cout<<"\n Enter 10 numbers to be sort:";
for(i=0;i<10;i++)
cin>>x[i];
for(i=0;i<9;i++)
{
low=x[i];
pos=i;
for(j=i+1;j<10;j++)
{
if(low>x[j])
{
low=x[j];
pos=j;
}
}
temp=x[i];
x[i]=x[pos];
x[pos]=temp;
}
cout<<"\n Sorted Array:";
for(i=0;i<10;i++)
cout<<setw(4)<<x[i];
return 0;
}

Page | 9
Output:

Page | 10
Program-6
/*Write a program in C++ to find out sum of elements matrices.*/
#include<iostream>
using namespace std;

int main()
{
int x[2][3]={10,9,8,8,3,4};
int i,j,sum;
// Processing
sum=0;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
sum=sum +x[i][j];
// output
cout<<"\n Sum of elements of matrix :"<<sum;
cout<<"\n The matrix is:";
for(i=0;i<2;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<" \t"<<x[i][j];
}
return 0;
}

Page | 11
Output:

Page | 12
Program-7
/*Write a program in C++ to separate lower matrix.*/
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x[4][4]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int i,j;
// output phase
cout<<"Lower matrix:";
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
if(i>=j)
cout<<setw(6)<<x[i][j];
else
cout<<setw(6)<<" ";
}
return 0;
}

Page | 13
Output:

Page | 14
Program-8
/*Write a program in C++ to demonstrate the working of Pointer.*/
#include<iostream>
using namespace std;

int* read()
{
int x;
x=45;
return(&x);
}
int main()
{
int *res;
res=read();
cout<<"\n Value at res:"<<res;
return 0;
}

Page | 15
Output:

Page | 16
Program-9
/*Write a program in C++ to merge two arrays.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];
cout<<"Enter Array 1 Size : ";
cin>>size1;
cout<<"Enter Array 1 Elements : ";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter Array 2 Size : ";
cin>>size2;
cout<<"Enter Array 2 Elements : ";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
for(i=0; i<size1; i++)
{
merge[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
merge[k]=arr2[i];
}
cout<<"Now the new array after merging is :\n";
for(i=0; i<size; i++)
{
cout<<merge[i]<<" ";
}
getch();
}

Page | 17
Output:

Page | 18
Program-10
/*Write a program in C++ to calculate area of a triangle using class.*/
#include<iostream.h>
#include<conio.h>
class ar
{private:
float a;
public:ar()
{a=0;}
~ar()
{a=0;}
void area(float x,float y,int u)
{a=(x*y)/2;
}
void showarea()
{cout<<"\n Area = "<<a<<” square units”;
}
}n1;
void main()
{
int n=0,i;
float h,b,r;
clrscr();
cout<<"\n Enter Base :";
cin>>h;
cout<<"\n Enter height :";
cin>>b;
n1.area(h,b,1);
n1.showarea();
}

Page | 19
Output:

Page | 20
Program-11
/*Write a program in C++ to demonstrate the functioning of Copy Constructor.*/
#include<iostream>
#include<string>
using namespace std;
class student
{
int roll;
char name[30];
public:
student()
{
cout<<"\n Constructor:";
roll=10;
strcpy(name,"Rahul");
}
student(student &s)
{
cout<<"\n Copy constructor:";
roll=s.roll;
strcpy(name,s.name);
}
void input_void()
{
cout<<"\n Enter roll no:";
cin>>roll;
cout<<"\n Enter name:";
cin>>name;
}
void show_data()
{
cout<<"\n Roll no:";
cout<<roll;
cout<<"\n Name:";
cout<<name;
}
};
int main()
{
student s;
s.show_data();
cout<<"\n";
student A(s);
A.show_data();
return 0;
}
Page | 21
Program-12
/*Write a program in C++ to demonstrate the functioning of Parameterized
Constructor.*/
#include<iostream>
using namespace std;

class read_constructor
{
int x;
public:
read_constructor(int a)
{
x=a;
}
void read_data()
{
cin>>x;
}
void show_data()
{
cout<<"Value of x:"<<x;
}
};
int main()
{
read_constructor obj(10);
obj.show_data();
return 0;
}

Page | 22
Program-13
/*Write a program to illustrate access control of inherited members in privately derived
class.*/
#include<iostream>
using namespace std;
class admin
{
int admno;
char name[30];
public:
void getadmin();
void putadmin();
};
void admin::getadmin()
{
cout<<"\nEnter admission no: ";
cin>>admno;
cout<<"\nEnter name: ";
gets(name);
}
void admin::putadmin()
{
cout<<"\nThe admission no is; ";
cout<<admno;
cout<<"\nThe name is: ";
cout<<name;
}
class acadm
{
int rno;
float marks;
public:
void getacadm();
void putacadm();
};
void acadm::getacadm()
{
cout<<"\nEnter rollno: ";
cin>>rno;
cout<<"\nEnter marks: ";
cin>>marks;
}
void acadm::putacadm()
{
cout<<"\nThe rollno is: ";
cout<<rno;
Page | 23
cout<<"\nThe total marks: ";
cout<<marks;
}
class school:public admin,public acadm //Derived class
{
char house[15],sport[20];
public:
void get_school();
void put_school();
};
void school::get_school()
{
getadmin();
getacadm();
cout<<"\nEnter house: ";
gets(house);
cout<<"\nEnter sport: ";
gets(sport);
}
void school::put_school()
{
putadmin();
putacadm();
cout<<"\nYour house is: ";
cout<<house;
cout<<"\nYour sports is: ";
cout<<sport;
}
int main()
{
school obj[10];
int n,i,j;
cout<<"Enter the no of students:";
cin>>n;
cout<<"\nEnter student details:";
for(i=0;i<n;i++)
{
obj[i].get_school(); //Input Function
}
cout<<"\nThe student details are:";
for(i=0;i<n;i++)
{
obj[i].put_school(); //Output Function
}
return 0;
}
Page | 24
Program-14
/*Write a program to demonstrate the push and pop functions in stack.*/
#include<iostream>
#include<stdlib>
#include<process>
using namespace std;

struct Node
{
int info;
Node*next;
}
*top,*newptr,*save,*ptr;
Node*Create_New_Node(int);
void Push(Node*);
void Display(Node*);
void Pop();
int main()
{
top=NULL;
int inf;
char ch=’y’;
while(ch==’y’ || ch==’Y’)
{
cout<<”\nEnter information for the new node…”;
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<”\nCannot create new node!!! Aborting!!\n”;
system(“pause”);
exit(1);
}
Push(newptr);
cout<<”\n Press Y to enter more nodes, N to exit…”;
cin>>ch;
}
system(“cls”);
do
{
cout<<”\n The Stack now is: \n”;
Display(top);
system(“pause”);
cout<<”Want to pop an element?(y/n)…”;
cin>>ch;
if(ch==’Y’ || ch==’y’)
Page | 25
Pop();
}
while(ch==’y’ || ch==’Y’);

}
Node*Create_New_Node(int n)
{
ptr=new Node;
ptr -> info=n;
ptr -> next=NULL;
return ptr;
}
void Push(node*np)
{
If(top=NULL)
top=np;
else
{
save=top;
top=np;
np -> next=save;
}
}
void Pop()
{
If(top==NULL)
cout<<”UNDERFLOW !!!\n”;
else
{
ptr=top;
top=top -> next;
delete ptr;
}
}
void Display(Node*np)
{
while(np!=NULL)
{
cout<<np -> info<<”->”;
np=np -> next;
}
cout<<”!!!\n”;
}

Page | 26
Program-15
/*Write a program to illustrate the implementation of Linear Queue.*/
#include <iostream>
using namespace std;
#define MAX 5
class Queue
{
private:
int front,rear;
int ele[MAX];
public:
Queue() //To Initalize queue
{
front = 0;
rear = -1;
}

int isFull();
int isEmpty();
void insertQueue(int item);
int deleteQueue(int *item);
};
int Queue::isFull( ) //To check queue is full or not
{
int full = 0 ;

if( rear == MAX-1 )


full = 1;

return full;
}
int Queue::isEmpty( ) //To check queue is empty or not
{
int empty = 0 ;

if( front == rear + 1 )


empty = 1;

return empty;
}
void Queue:: insertQueue(int item) //Insert Item into queue
{
if( isFull() )
{
cout<<"\nQueue OverFlow" << endl;
return;
}
ele[++rear]=item;
cout<<"\ninserted Value :" << item;
Page | 27
}
int Queue:: deleteQueue(int *item) //delete item from queue

{
if( isEmpty() )
{
cout<<"\nQueue Underflow" << endl;
return -1;
}

*item = ele[front++];
return 0;
}

int main()
{
int item=0;
Queue q = Queue();
q.insertQueue(10);
q.insertQueue(20);
q.insertQueue(30);
q.insertQueue(40);
q.insertQueue(50);
q.insertQueue(60);
if(q.deleteQueue( &item)==0)
cout<<"\nDeleted item : "<< item;
if(q.deleteQueue( &item)==0)
cout<<"\nDeleted item : "<< item;
if(q.deleteQueue( &item)==0)
cout<<"\nDeleted item : "<< item;
if(q.deleteQueue( &item)==0)
cout<<"\nDeleted item : "<< item;
if(q.deleteQueue( &item)==0)
cout<<"\nDeleted item : "<< item;
if(q.deleteQueue( &item)==0)
cout<<"\nDeleted item : "<< item;
cout<< endl;
return 0;
}

Page | 28
Output:

Page | 29
Program-16
/*Write a program to illustrate the implementation of Circular Queue.*/
#include<bits/stdc++.h>
using namespace std;
struct Queue
{
int rear, front;
int size;
int *arr;
Queue(int s)
{
front = rear = -1;
size = s;
arr = new int[s];
}
void enQueue(int value);
int deQueue();
void displayQueue();
};
void Queue::enQueue(int value)
{
if ((front == 0 && rear == size-1) ||
(rear == (front-1)%(size-1)))
{ printf("\nQueue is Full");
return;
}
else if (front == -1) /* Insert First Element */
{
front = rear = 0;
arr[rear] = value;
}
else if (rear == size-1 && front != 0)
{
rear = 0;
arr[rear] = value;
}
else
{ rear++;
arr[rear] = value;
}
}
int Queue::deQueue()
{
if (front == -1)
{ printf("\nQueue is Empty");
return INT_MIN;
}
int data = arr[front];
arr[front] = -1;
Page | 30
if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == size-1)
front = 0;
else
front++;
return data;
}
void Queue::displayQueue()
{
if (front == -1)
{
printf("\nQueue is Empty");
return;
}
printf("\nElements in Circular Queue are: ");
if (rear >= front)
{
for (int i = front; i <= rear; i++)
printf("%d ",arr[i]);
}
else
{
for (int i = front; i < size; i++)
printf("%d ", arr[i]);
for (int i = 0; i <= rear; i++)
printf("%d ", arr[i]);
}
}
int main()
{
Queue q(5);
q.enQueue(14); // Inserting elements in Circular Queue
q.enQueue(22);
q.enQueue(13);
q.enQueue(-6);
q.displayQueue(); // Display elements present in Circular Queue
printf(“\nDeleted value = %d”, q.deQueue()); // Deleting elements from Circular Queue
printf(“\nDeleted value = %d”, q.deQueue());
q.displayQueue();
q.enQueue(9);
q.enQueue(20);
q.enQueue(5);
q.displayQueue();
q.enQueue(20);
return 0;
}
Page | 31
Output:

Page | 32
Program-17
/*Write a program in C++ to show the addition and deletion of new book using Linked
List.*/
#include <iostream>
using namespace std;

//Declare Node
struct Node
{
int num;
Node *next;
};

//Declare starting (Head) node


struct Node *head=NULL;

//Insert node at start


void insertNode(int n){
struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}

//Traverse/ display all nodes (print items)


void display(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}

//delete node from start


void deleteItem(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
cout<<head->num<<" is removed."<<endl;
head=head->next;
Page | 33
}
int main(){

display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
display();
deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem();
deleteItem();
display();
return 0;
}

Page | 34
Output:

Page | 35
Program-18
/*Write a program in C++ to read the content of a Binary File. */
#include <iostream>
#include <fstream>
#define FILE_NAME "emp.dat"
using namespace std;

//class employee declaration


class Employee {
private :
int empID;
char empName[100] ;
char designation[100];
int ddj,mmj,yyj;
int ddb,mmb,yyb;
public :
//function to read employee details
void readEmployee(){
cout<<"EMPLOYEE DETAILS"<<endl;
cout<<"ENTER EMPLOYEE ID : " ;
cin>>empID;
cin.ignore(1);
cout<<"ENTER NAME OF THE EMPLOYEE : ";
cin.getline(empName,100);

cout<<"ENTER DESIGNATION : ";


cin.getline(designation,100);

cout<<"ENTER DATE OF JOIN:"<<endl;


cout<<"DATE : "; cin>>ddj;
cout<<"MONTH: "; cin>>mmj;
cout<<"YEAR : "; cin>>yyj;

cout<<"ENTER DATE OF BIRTH:"<<endl;


cout<<"DATE : "; cin>>ddb;
cout<<"MONTH: "; cin>>mmb;
cout<<"YEAR : "; cin>>yyb;
}
//function to write employee details
void displayEmployee(){
cout<<"EMPLOYEE ID: "<<empID<<endl
<<"EMPLOYEE NAME: "<<empName<<endl
<<"DESIGNATION: "<<designation<<endl
<<"DATE OF JOIN: "<<ddj<<"/"<<mmj<<"/"<<yyj<<endl
<<"DATE OF BIRTH: "<<ddb<<"/"<<mmb<<"/"<<yyb<<endl;
}
Page | 36
};

int main(){

//object of Employee class


Employee emp;
//read employee details
emp.readEmployee();

//write object into the file


fstream file;
file.open(FILE_NAME,ios::out|ios::binary);
if(!file){
cout<<"Error in creating file...\n";
return -1;
}

file.write((char*)&emp,sizeof(emp));
file.close();
cout<<"Date saved into file the file.\n";

//open file again


file.open(FILE_NAME,ios::in|ios::binary);
if(!file){
cout<<"Error in opening file...\n";
return -1;
}

if(file.read((char*)&emp,sizeof(emp))){
cout<<endl<<endl;
cout<<"Data extracted from file..\n";
//print the object
emp.displayEmployee();
}
else{
cout<<"Error in reading data from file...\n";
return -1;
}

file.close();
return 0;
}

Page | 37
Output:

Page | 38
Program-19
/*Write a program in C++ to write the content into a Binary File.*/
#include<iostream>
#include<fstream>
using namespace std;
struct Student {
int roll_no;
string name;
};
int main() {
ofstream wf("student.dat", ios::out | ios::binary);
if(!wf) {
cout << "Cannot open file!" << endl;
return 1;
}
Student wstu[3];
wstu[0].roll_no = 1;
wstu[0].name = "Ram";
wstu[1].roll_no = 2;
wstu[1].name = "Shyam";
wstu[2].roll_no = 3;
wstu[2].name = "Madhu";
for(int i = 0; i < 3; i++)
wf.write((char *) &wstu[i], sizeof(Student));
wf.close();
if(!wf.good()) {
cout << "Error occurred at writing time!" << endl;
return 1;
}
ifstream rf("student.dat", ios::out | ios::binary);
if(!rf)
{
cout << "Cannot open file!" << endl;
return 1;
}
Student rstu[3];
for(int i = 0; i < 3; i++)
rf.read((char *) &rstu[i], sizeof(Student));
rf.close();
if(!rf.good())
{
cout << "Error occurred at reading time!" << endl;
return 1;
}
cout<<"Student's Details:"<<endl;
for(int i=0; i < 3; i++) {
Page | 39
cout << "Roll No: " << wstu[i].roll_no << endl;
cout << "Name: " << wstu[i].name << endl;
cout << endl;
}
return 0;
}

Page | 40
Output:

Page | 41
Program-20
/*Write a program in C++ to create a Text File.*/
#include <bits/stdc++.h>
using namespace std;

int main()
{
fstream file;
file.open("Gfg.txt",ios::out);
if(!file)
{
cout<<"Error in creating file!!!";
return 0;
}
cout<<"File created successfully.";
file.close();

return 0;
}

Page | 42
Output:

Page | 43

You might also like