You are on page 1of 64

INDEX

C++ PROGRAMS:
1. ONE DIMENSIONAL ARRAY

2. TWO DIMENSIONAL ARRAY

3. LINEAR SEARCH

4. BINARY SEARCH

5. INSERTION IN ARRAY

6. DELETION IN ARRAY

7. BUBBLE SORTING

8. INSERTION SORTING

9. SELECTION SORTING

10. MERGING IN ARRAY

11. FRIEND FUNCTION

12. FRIEND CLASSES

13. COPY CONSTRUCTOR

14. BINARY FILE AND BASIC OPERATIONS

15. TO READ A TEXT FILE

16. TO COUNT THE NUMBER OF CHARATERS IN A FILE

17. STATIC QUEUE

18. DYNAMIC QUEUE

19. STATIC QUEUE

20. DYNAMIC QUEUE

21. TO CALCULATE TSA AND VOLUME OF A CUBE ,A CUBOID AND A CYLINDER DEPENDING
ON USER’S CHOICE
SQL COMMANDS: (15 commands)
1. Show database

2. Create database

3. To insert data

4. Use of SELECT command

5. Creating the table

6. Printing the table in descending order

7. Alter the table with ADD command

8. Alter the table with DROP command

9. LIKE command

10. SELECT-ORDER BY command

11. SELECT-DISTINCT command

12. Grouping function – MAX,MIN,AVG

13. SELECT-Grouping function- SUM,COUNT

14. SELECT-GROUP BY command

15. UPDATE command


PROGRAM 1: TO ILLUSTRATE AN ONE DIMENSIONAL ARRAY

INPUT:

#include <iostream>

using namespace std;

void print(int A[], int length)

for (int n=0; n<length; n++)

cout << A[n] << " ";

cout << "\n";

int main ()

int arr[] = {5, 10, 15};

print(arr,3);

return 0;
OUTPUT:
PROGRAM 2: TO ILLUSTRATE A TWO DIMENSIONAL ARRAY

INPUT:

#include <iostream>

using namespace std;

void print(int A[][3],int N, int M)

for (R = 0; R < N; R++)

for (C = 0; C < M; C++)

cout << A[R][C];

int main ()

int arr[4][3] ={{12, 29, 11},

{25, 25, 13},

{24, 64, 67},

{11, 18, 14}};

print(arr,4,3);

return 0;
OUTPUT:
PROGRAM 3: TO SEARCH AN ELEMENT IN AN ARRAY
THROUGH LINEAR SEARCH

INPUT:

#include<iostream.h>

#include<conio.h>

int Lsearch(int[],int,int); //i.e.,Lsearch(the array,its size,search_item)

int main()

{ int AR[50],ITEM,N,index; //array can hold max. 50 elements

cout<<"Enter desired array size(max. 50)...";

cin>>N;

cout<<"/nEnter Array elements/n";

for(int i=0; i< N; i++)

{cin>>AR[i];}

cout<<"\n.Enter Elements to be searched for...";

cin>>ITEM;

index= Lsearch(AR,N,ITEM);

if(index==-1)

cout<<"/nSorry!! Given element could not be found./n";

else cout<<"/nElement found at index:"<<index<<",Position:"<<index+1;

return 0;

}
int Lsearch(int AR[],int size,int item) //function to perform liner search

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

{ if(AR[i]== item) return i; //return index of item in case of successful search

return -1; //the control will reach here only when item is not found

OUTPUT:
PROGRAM 4: TO SEARCH FOR AN ELEMENT IN AN ARRAY
THROUGH BINARY SEARCH

INPUT :

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int Bsearch(int[],int,int); //i.e, int Bsearch(the array, its size,search_item)

int main()

{ system("cls");

int AR[50],ITEM,N,index; //array can hold max.50 elements

cout<<"Enter desired array size(max. 50)....";

cin>>N;

cout<<"\nEnter Array elements (must be sorted in ascending order\n";

for(int i=0; i<N ;i++)

cin>>AR[i];

cout<<"\n.Enter elements to be searched for...";

cin>>ITEM;

index=Bsearch(AR,N,ITEM);

if(index==-1)

cout<<"\n.Sorry! Given element could not be found.\n";

else cout<<"\n.Element found at index"<<index<<",Position:"<<index+1<<"/n";

return 1;
}

int Bsearch(int AR[],int size, int item) //function to perform binary search

{ int beg,last,mid;

beg=0 ; last=size-1;

while(beg<=last)

{ mid=(beg+last)/2;

if(item== AR[mid]) return mid;

else

if(item>AR[mid]) beg= mid+1;

else last=mid-1;

return 0;

OUTPUT:
PROGRAM 5: TO INSERT AN ELEMENT IN AN ARRAY

INPUT:

#include<iostream.h>

#include<process.h> //for exit()

int FindPos(int[],int,int); //function to determine the right position for new


element

int main()

{ int AR[50],ITEM,N,INDEX; //array can hold max.50 elements

cout<<"How many elements do you want to create array with?(max.50)....";

cin>>N;

cout<<"\nEnter Array elements(must be sorted in Asc order)\n";

for(int i=0; i<N; i++)

cin>>AR[i];

char ch='y';

while(ch=='y'||ch=='Y')

{ cout<<"\nEnter Element to be added\n";

cin>>ITEM;

if(N==50)

{ cout<<"Overflow\n";

exit(1);

int index = FindPos(AR,N,ITEM);


for(int i=0;i>index;i--)

{ AR[i]=AR[i-1];} //shift elements to create room for new element

AR[index]= ITEM; //item inserted

N += 1; //Number of elements updated

cout<<"\nWant to insert more elements ?(y/n).....";

cin>>ch;

cout<<"The array now is as shown below\n";

for( i=0; i<N; i++)

cout<<AR[i]<<" ";

cout<<endl;

return 0;

int FindPos(int AR[], int size,int item) //function to determine the position for
new element

{ int pos;

if( item<AR[0] ) pos=0;

else

{ for(int i=0; i< size-1; i++)

{ if(AR[i]<=item && item<AR[i+1])

{ pos= i+1; break;}

}
if(i== size-1) pos=size;

return pos;

OUTPUT:
PROGRAM 6: TO DELETE AN ELEMENT FROM AN ARRAY

INPUT:

#include<iostream.h>

#include<process.h> //for exit()

int Lsearch(int[],int ,int); //function to search for given element

int main()

{ int AR[50],ITEM,N, index; //array can hold max. 50 elements

cout<<"How many elements do you want to create array with?(max. 50)...";

cin>>N;

cout<<"\nEnter Array elements...\n";

for(int i=0; i<N;i++)

cin>>AR[i];

char ch='y';

while(ch=='y'||ch=='Y')

{ cout<<"\nEnter element to be deleted...";

cin>>ITEM;

if(N==0)

{ cout<<"Underflow";

exit(1);}

index= Lsearch(AR, N,ITEM);

if(index!=-1) AR[index]=0;

else cout<<"Sorry!! No such element in the array.\n";


cout<<"\nThe Array now is as shown below...\n";

cout<<"\nZero(0) signifies deleted element\n";

for( int i=0; i<N; i++)

cout<<AR[i]<<" ";

cout<<endl;

cout<<"After this emptied space will be shifted to the end of array\n";

for(i=index;i<N;i++)

{ AR[i]=AR[i+1];} //shifts elements to bring empty space at end of array

N=-1; //Number of elements updated

cout<<"\nWant to delete more elements?(y/n)...\n";

cin>>ch;}

cout<<"The array after shifting ALL emptied spaces towards right is...\n";

for(i=0; i<N; i++)

cout<<AR[i]<<"";

cout<<endl;

return 0;

int Lsearch(int AR[],int size,int item) //function to perform linear search

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

{ if(AR[i]==item) return i;

return -1; //the control will reach here only when item is not found
}

OUTPUT:
PROGRAM 7: TO SORT AN ARRAY WITH THE HELP OF
BUBBLE SORTING

INPUT :

#include<iostream.h>

#include<conio.h>

const int size=40;

int array[size],n;

void bubble_sort()

{ int temp;

for (int i=n-1;i>0;i--)

for(int j=0;j<i;j++)

if(array[j]<array[j+1])

temp=array[j];

array[j]=array[j+1];

array[j+1]=temp;

}
}

void main()

clrscr();

cout<<"Enter number of elements of the array(max 40):";

cin>>n;

cout<<"Enter Array Elements:";

for(int i=0;i<n;i++)

cin>>array[i];

bubble_sort();

cout<<"Sorted array:\n";

for(i=0;i<n;i++)

cout<<array[i]<<" ";

getch(); }

OUTPUT:
PROGRAM 8: TO SORT AN ARRAY THROUGH INSERTION SORTING

INPUT:

#include<iostream.h>

#include<conio.h>

const int size=40;

int array[size],n;

void Ins_sort()

int tmp,j;

array[0]=-32678;

for(int k=1;k<=n;k++)

j=k-1;

tmp=array[k];

while(tmp<array[j])

array[j+1]=array[j];

j--;

array[j+1]=tmp;

}
void main()

clrscr();

cout<<"Enter number of elements of array(max 40):";

cin>>n;

cout<<"Enter Array Elements:";

for(int i=1;i<=n;i++)

cin>>array[i];

Ins_sort();

cout<<"Sorted Array:\n";

for(i=1;i<=n;i++)

cout<<array[i]<<" ";

getch();

OUTPUT:
PROGRAM 9: TO SORT AN ARRAY WITH THE HELP OF
SELECTION SORTING

INPUT:

#include<iostream.h>

#include<conio.h>

const int size=40;

int array[size],n;

void sel_sort()

int small,pos,temp;

for(int i=0;i<n;i++)

small=array[i];

pos=i;

for(int j=i+1;j<n;j++)

if(array[j]<small)

small=array[j];

pos=j;

}
}

temp=array[i];

array[i]=array[pos];

array[pos]=temp;

void main()

clrscr();

cout<<"Enter the number of elements of the array(max 40):";

cin>>n;

cout<<"Enter Array Elements:";

for(int i=0;i<n;i++)

cin>>array[i];

sel_sort();

cout<<"Sorted Array:\n";

for(i=0;i<n;i++)

cout<<array[i]<<" ";

getch(); }
OUTPUT:
PROGRAM 10: TO MERGE TWO ARRAYS, A AND B, INTO AN
ARRAY C WHERE THE ELEMENTS ARE IN ASCENDING
ORDER

INPUT:

#include<iostream.h>

#include<conio.h>

int A[40],B[40],C[60];

int m,n,mn;

void Merge()

int a=0,b=0,c=0;

while(a<m&&b<n)

if(A[a]<B[b])

C[c++]=A[a++];

else

C[c++]=B[b++];

if(a<m)

while(a<m)

C[c++]=A[a++];
else

while(b<n)

C[c++]=B[b++];

void main()

cout<<"Enter number of elements of Array A:";

cin>>m;

cout<<"Enter elements of Array A:\n";

for(int i=0;i<m;i++)

cin>>A[i];

cout<<"Enter number of elements of Array B:";

cin>>n;

cout<<"Enter elements of Array B:\n";

for(i=0;i<n;i++)

cin>>B[i];

mn=m+n;

if(mn<=60)

Merge();

else
{

cout<<"Array C cannot be created!!Overflow!!!";

return;

cout<<"\nTHe sorted Array C :\n";

for(i=0;i<mn;i++)

cout<<C[i]<<" ";

getch();}

OUTPUT:
PROGRAM 11: TO ILLUSTRATE A FRIEND FUNCTION IN A
CLASS PROVIDING ACESS TO PRIVATE AND PROTECTED
MEMBERS OF THAT CLASS

INPUT:

#include <iostream>

class Rectangle

{ private :

int length;

int width;

public:

void setData(int len, int wid)

{ length = len;

width = wid; }

int getArea()

{ return length * width ; }

friend double

getCost(Rectangle); //friend of class Rectangle

};

//friend function getCost can access private member of class

double getCost (Rectangle rect)

{ double cost;
cost = rect.length * rect.width * 5;

return cost;

int main ()

{ Rectangle floor;

floor.setData(20,3);

cout << "Expense " << getCost(floor) << endl;

return 0;

OUTPUT:
PROGRAM 12: TO ILLUSTRATE A CLASS AS A FRIEND OF AN
OTHER CLASS

INPUT:

#include <iostream>

using namespace std;

class CostCalculator;

class Rectangle

{ private :

int length;

int width;

public:

void setData(int len, int wid)

{ length = len;

width = wid; }

int getArea()

{ return length * width ; }

friend class

CostCalculator; //friend of class Rectangle

};

//friend class costCalculator can access private member of class Rectangle

class CostCalculator

{ public :
double getCost (Rectangle rect)

{ double cost;

cost = rect.length * rect.width * 5;

return cost; }

};

int main ()

{ Rectangle floor;

floor.setData(20,3);

CostCalculator calc;

cout << "Expense " << calc.getCost(floor) << endl;

return 0;

OUTPUT:
PROGRAM 13: TO ILLUSTRATE A CONSTRUCTOR IN A
DERIVED CLASS B OF BASE CLASS A

INPUT:

class Rectangle

{private :

float length;

float width;

public:

Rectangle ()

{ length = 0;

width = 0; }

Rectangle (float len, float wid)

{ length = len;

width = wid; }

float area()

{ return length * width ;} };

class Box : public Rectangle

{ private :

float height;

public:

Box ()

{ height = 0; }
Box (float len, float wid, float ht) : Rectangle(len, wid)

{ height = ht; }

float volume()

{ return area() * height; }

};

int main ()

{ Box bx;

Box cx(4,8,5);

cout << bx.volume() << endl;

cout << cx.volume() << endl;

return 0;

OUTPUT:
PROGRAM 14 : TO ILLUSTRATE A BINARY FILE AND ITS
OPERATIONS SUCH AS READ(), WRITE() AND MODIFY()

INPUT:

#include<iostream.h>

#include<fstream.h>

#include<cstdio.h>

class Student

{ int admno;

char name[50];

public:

void setData()

{ cout << "\nEnter admission no. ";

cin >> admno;

cout << "Enter name of student ";

cin.getline(name,50);

void showData()

{ cout << "\nAdmission no. : " << admno;

cout << "\nStudent Name : " << name;

int retAdmno()

{ return admno;
}

};

/*

* function to write in a binary file.

*/

void write_record()

{ ofstream outFile;

outFile.open("student.dat", ios::binary | ios::app)

Student obj;

obj.setData();

outFile.write((char*)&obj, sizeof(obj));

outFile.close();

/*

* function to display records of file

*/

void display()

{ ifstream inFile;

inFile.open("student.dat", ios::binary);

Student obj;

while(inFile.read((char*)&obj, sizeof(obj)))

{ obj.showData();

} inFile.close();
}

/*

* function to search and display from binary file

*/

void search(int n)

{ ifstream inFile;

inFile.open("student.dat", ios::binary);

Student obj;

while(inFile.read((char*)&obj, sizeof(obj)))

{ if(obj.retAdmno() == n)

{ obj.showData();}

inFile.close();

/*

* function to delete a record

*/

void delete_record(int n)

{ Student obj;

ifstream inFile;

inFile.open("student.dat", ios::binary);
ofstream outFile;

outFile.open("temp.dat", ios::out | ios::binary);

while(inFile.read((char*)&obj, sizeof(obj)))

{ if(obj.retAdmno() != n)

{ outFile.write((char*)&obj, sizeof(obj)); } }

inFile.close();

outFile.close();

remove("student.dat");

rename("temp.dat", "student.dat");

/*

* function to modify a record

*/

void modify_record(int n)

{ fstream file;

file.open("student.dat",ios::in | ios::out);

Student obj;

while(file.read((char*)&obj, sizeof(obj)))

{ if(obj.retAdmno() == n)

{ cout << "\nEnter the new details of student";

obj.setData();

int pos = -1 * sizeof(obj);


file.seekp(pos, ios::cur);

file.write((char*)&obj, sizeof(obj)); }

} file.close();

int main()

{ //Store 4 records in file

for(int i = 1; i <= 4; i++)

write_record();

//Display all records

cout << "\nList of records";

display();

//Search record

cout << "\nSearch result";

search(100);

//Delete record

delete_record(100);

cout << "\nRecord Deleted";

//Modify record

cout << "\nModify Record 101 ";

modify_record(101);

return 0;

}
OUTPUT:
PROGRAM 15: TO ILLUSTRATE READING A TEXT FILE

INPUT:

#include<fstream.h>

#include<iostream.h>

int main()

{ ifstream fin;

fin.open("out.txt");

char ch;

while(!fin.eof())

{ fin.get(ch);

cout << ch; }

fin.close();

return 0;}

OUTPUT:
PROGRAM 16: TO COUNT THE NUMBER OF CHARACTERS IN
A FILE

INPUT:

#include<fstream>

#include<iostream>

using namespace std;

int main()

{ ifstream fin;

fin.open("out.txt");

int count = 0;

char ch;

while(!fin.eof())

{ fin.get(ch);

count++; } cout << "Number of characters in file are " << count;

fin.close();

return 0;}

OUTPUT:
PROGRAM 17: TO ILLUSTRATE A STATIC QUEUE WITH
BASIC OPERATIONS

INPUT:

#include<iostream.h>

#include<conio.h>

#define size 4

class cqueue

{ int data[size];

int front,rear;

public:

cqueue()

{ front=-1;rear=-1;}

void insert();

void remove();

};

void cqueue::insert()

{ if(rear==size-1&&front==0 || front==rear+1)

{ cout<<"\nCircular queue is full";

return;

}else if(rear==-1)

{rear++;

front++; }
else if(rear==size-1)

rear=0;

else

rear++;

cout<<"Enter Data : ";

cin>>data[rear];}

void cqueue::remove()

{ if(front==-1)

{ cout<<"\n Circular Queue is empty";return; }

cout<<data[front]<<" deleted"<<endl;

if(front==rear)

{ front=-1;rear=-1;}

else if(front==size-1)

front=0;

else

front++;}

void main()

{ cqueue cq;

int ch;

do

{ cout<<"\n1. Insert\n2. Remove\n3. Quit\nEnter Choice(1-3) ";

cin>>ch;
switch(ch)

case 1: cq.insert();break;

case 2: cq.remove();break;

}while(ch!=3); }

OUTPUT:
PROGRAM 18: TO ILLUSTRATE A DYNAMIC QUEUE

INPUT:

#include<iostream.h>

#include<conio.h>

struct node

{ int data;

node *next;};

class queue

{ node *rear,*front;

public:

queue()

{ rear=NULL;front=NULL;}

void qinsert();

void qdelete();

void qdisplay();

~queue();

};

void queue::qinsert()

{ node *temp;

temp=new node;
cout<<"Data :";

cin>>temp->data;

temp->next=NULL;

if(rear==NULL)

{ rear=temp;

front=temp;

} else

{ rear->next=temp;

rear=temp; }

void queue::qdelete()

{ if(front!=NULL)

{ node *temp=front;

cout<<front->data<<"deleted \n";

front=front->next;

delete temp;

if(front==NULL)

rear=NULL;

} else

cout<<"Queue Empty..";}

void queue::qdisplay()

{ node *temp=front;
while(temp!=NULL)

{ cout<<temp->data<<endl;

temp=temp->next; }

queue::~queue()

{ while(front!=NULL)

{ node *temp=front;

front=front->next;

delete temp; } }

void main()

{ queue obj; char ch;

do { cout<< "i. insert\nd. Delete\ns. Display\n q. quit ";

cin>>ch;

switch(ch)

{ case 'i' : obj.qinsert();break;

case 'd' : obj.qdelete();break;

case 's' : obj.qdisplay();

} }while(ch!='q');
OUTPUT:
PROGRAM 19: TO ILLUSTRATE A STATIC STACK

INPUT:

#include<iostream.h>

#include<conio.h>

#define size 4

class stack

{ int data[size];

int top;

public:

stack()

{ top=-1; }

void push();

void pop();

void display();

};

void stack::push()

{ if(top==size-1)

{ cout<<"\nStack is full";

return; }

else

{ top++;

cout<<"Enter Data : ";


cin>>data[top]; }

void stack::pop()

{ if(top==-1)

cout<<"\n Stack is empty";

else

{ cout<<data[top]<<"deleted "<<endl;

top--;

void stack::display()

{ int t=top;

while(t>=0)

{ cout<<data[t]<<endl;

t--;

void main()

{ stack st;

int ch;

do

{ cout<<"\n1. Push\n2. Pop\n3. Display \n4.Quit\nEnter Choice(1-4) ";


cin>>ch;

switch(ch)

{ case 1: st.push();break;

case 2: st.pop();break;

case 3: st.display();

}while(ch!=4); }

OUTPUT:
PROGRAM 20: TO ILLUSTRATE A DYNAMIC STACK WITH
BASIC OPERATIONS

INPUT:

#include<iostream.h>

#include<conio.h>

struct node

{ int data;

node *next;

};

class stack

{ node *top;

public :

stack()

{ top=NULL;}

void push();

void pop();

void display();

~stack();

};

void stack::push()

{ node *temp;

temp=new node;
cout<<"Enter data :";

cin>>temp->data;

temp->next=top;

top=temp; }

void stack::pop()

{ if(top!=NULL)

{ node *temp=top;

top=top->next;

cout<<temp->data<<"deleted";

delete temp;}

else

cout<<"Stack empty"; }

void stack::display()

{ node *temp=top;

while(temp!=NULL)

{ cout<<temp->data<<" ";

temp=temp->next; }

stack::~stack()

{while(top!=NULL)

{node *temp=top;

top=top->next;
delete temp; }

void main()

{ stack st;

char ch;

do{ cout<<"stack options\nP for push \nO for Pop \nD for Display \nQ for
quit";

cin>>ch;

switch(ch)

{ case 'P': st.push();break;

case 'O': st.pop();break;

case 'D': st.display();break; }

}while(ch!='Q');}

OUTPUT:
PROGRAM 21: A MENU DRIVEN PROGRAM TO CALCULATE
TSA AND VOLUME OF A CUBE, CUBOID AND A CYLINDER
DEPENDING ON USER’S CHOICE

INPUT:

#include<iostream.h>

#include<conio.h>

void Tsa_Volume(int a)

{cout<<"\nVolume:"<<a*a*a;

cout<<"\nTotal Surface Area:"<<6*a*a;

void Tsa_Volume(int a,int b,int c)

cout<<"\nVolume:"<<a*b*c;

cout<<"\nTotal Surface Area:"<<2*(a*b+b*c+a*c);

void Tsa_Volume(int a,int b)

cout<<"\nVolume:"<<(22*a*a*b)/7;

cout<<"\nTotal Surface Area:"<<(2*22*a*(b+a))/7;

void main()

{
int a,b,c;

int choice;

do{

clrscr();

cout<<"\t\tSurface Area & Volume Calculator \n";

cout<<"\t1.Cube\n\t2.Cuboid\n\t3.Cylinder\n\t4.Exit\nEnter your Choice :";

cin>>choice;

switch(choice)

{case 1: cout<<"Enter Side of cube:";

cin>>a;

Tsa_Volume(a);

break;

case 2: cout<<"Enter Length,Bredth,Height of the cuboid:";

cin>>a>>b>>c;

Tsa_Volume(a,b,c);

break;

case 3: cout<<"Enter Radii of Cuboid:";

cin>>a;

cout<<"Enter Height of Cuboid:";

cin>>b;

Tsa_Volume(a,b);

break;
case 4: return ;

default:cout<<"Wrong Choice!!!!";

break;}

getch();

}while(choice!=4) ;

OUTPUT:
SQL COMMANDS: (15 commands)
Command 1:

Show databases;

Command 2:

Create database;
Command 3 & 4:

Create and desc table


Command 5 & 6:

Insert & select

Command 7:

Alter table- add


Command 8:

Alter table drop

Command 9:

Select-Like
Command 10:

Select- order by

Command 11:Select-distinct
Command 12:

Grouping functions – max, min, avg

Command 13:

Select – grouping functions- sum, count


Command 14:

Select – group by

Command 15:Update
PREPARED AND SUBMITTED BY : SOMYA BHATNAGAR

CLASS 12TH –MEDICAL

SESSION -2017-18

You might also like