0% found this document useful (0 votes)
118 views70 pages

Lab Practical File (C++ & SQL) : Chandan Mishra Mrs. Apeksha Joshi

The document contains a lab practical file for a student named Chandan Mishra. It lists 20 programming problems covering topics like input/output, strings, structures, functions, classes. For each problem, it lists the problem statement, sample source code to solve it, and sample output. It also contains the student's and teacher's signatures.

Uploaded by

Chandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views70 pages

Lab Practical File (C++ & SQL) : Chandan Mishra Mrs. Apeksha Joshi

The document contains a lab practical file for a student named Chandan Mishra. It lists 20 programming problems covering topics like input/output, strings, structures, functions, classes. For each problem, it lists the problem statement, sample source code to solve it, and sample output. It also contains the student's and teacher's signatures.

Uploaded by

Chandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 70

LAB PRACTICAL FILE (C++ & SQL)

CHANDAN MISHRA Mrs. APEKSHA JOSHI


Program Page Date Signa
S.N No. ture
o.

Write a program to input day


1 number of a week and display the
corresponding week day.
write a program to enter two
2 numbers m and n and display m
multiples of n.
Write a program to read a string
3 and print out the Following :
[1].No. of capital alphabets.
[2].No. of small alphabets.
[3].No. of non-alphabets.

write a program to read a string


4 and print it after replacing
each of its capital alphabet by
corresponding small alphabet
and each small alphabet by its
corresponding capital alphabet.

Write a program to store


5 information of 10 employees
and to display of an employee
depending upon the employee
number given by the user using
structure.

6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0

[1]. Write a programme to input day number of a week and display the
corresponding day name.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int n;
cout<<"Enter The Day Number In The Week\n";
cin>>n;
cout<<"\nThe Day Name Is- ";
switch(n)
{ case 1:{
cout<<"Sonday";
}break;
case 2:{
cout<<"Monday"; }break;
case 3:{
cout<<"Tuesday"; }break;
case 4:{
cout<<"Wednesday"; }break;
case 5:{
cout<<"Thursday"; }break;
case 6:{
cout<<"Friday"; }break;
case 7:{
cout<<"Saturday"; }break;
}
getch();
}
OUTPUT
[2]. Write a program to input two numbers m and n and display first m multiples of
n.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
int m,n;
cout<<"Enter The Value Of n \n";
cin>>n;
cout<<"Enter tHE Value Of m \n";
cin>>m;
cout<<"The First m Multiple Of n Are :-\n";
for(int i=1;i<=m;i++)
{ cout<<n<<"*"<<i<<"="<<n*i<<"\n";
}
getch();
}
OUTPUT
[3].Write a program to read a string and print out the followi ng:
1)No. Of capital alphabets,
2)No. Of small alphabet s,
3)No. Of non alphabet.
SOURCE CODE

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
clrscr();
char string[40];
int cap=0,sml=0,non=0;
cout<<"Enter The String\n";
cin.getline(string,40);
for(int i=0;string[i]!='\0';i++)
{
if(isupper(string[i]))
{ cap++;
}
if(islower(string[i]))
{ sml++;
}
if(!isalpha(string[i]))
{ non++;
}}
cout<<"\n\nThe Number Of Capital Alphabet Is\n"<<cap;
cout<<"\n\nThe Number Of Small Alphabet Is\n"<<sml;
cout<<"\n\nThe Number Of Non-Alphabet Is\n"<<non;
getch();
}
OUTPUT

[4].Write a program to read a string and print it after replacing each of its capital
alphabets by the corresponding small alphabet and each small alphabet by its
corresponding capital alphabet.
SOURCE CODE
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
#include<stdio.h>
void main(){ clrscr();
char string[30];
cout<<"Enter The String\n";
cin.getline(string,30);
for(int i=0;string[i]!='\0';i++)
{ if(isupper(string[i]))
{ string[i]=tolower(string[i]); }
else if(islower(string[i]))
{ string[i]=toupper(string[i]); }}
cout<<"\n\nThe Updated String Is :-\n"<<string;
getch();
}
OUTPUT

[5]. Write a program to store information of 10 employees and to display of an


employee depending upon the employee number given by the user using structure.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct addr{
int house_no;
char area[20];
char city[20];
};
struct emply{
int emp_no;
char emp_name[20];
char emp_desig[20];
addr adrs;
float salary;
}e[10];
void enter(int s){
for(int i=0;i<s;i++)
{ clrscr();
cout<<"\nEnter The Employee Number "<<"\n";
cin>>e[i].emp_no;
cout<<"\nEnter The Employee Name "<<"\n";
gets(e[i].emp_name);
cout<<"\nEnter The Employee Designation "<<"\n";
gets(e[i].emp_desig);
cout<<"\nAddress of Employee "<<":-\n"<<"\n Enter The House Number \n";
cin>>e[i].adrs.house_no;
cout<<"\nEnter The Area \n";
gets(e[i].adrs.area);
cout<<"\nEnter The City \n";
gets(e[i].adrs.city);
cout<<"\nEnter The Salary Of The Employee "<<"\n";
cin>>e[i].salary;
}}
void show(int a){
cout<<"\nEmployee Number is "<<e[a].emp_no;
cout<<"\nEmployee Name is "<<e[a].emp_name;
cout<<"\nEmployee Designation is "<<e[a].emp_desig;

cout<<"\nAddress of Employee "<<":-\n"<<"The House Number is


"<<e[a].adrs.house_no;
cout<<"\nThe Area is "<<e[a].adrs.area;
cout<<"\nThe City is "<<e[a].adrs.city;
cout<<"\n The Salary Of The Employee is "<<e[a].salary;
}
void main()
{ clrscr();
int n,a;
cout<<"\nEnter The Number Of Employees Of Which You Want To Add Details (max
10)\n";
cin>>a;
enter(a);
clrscr();
cout<<"\n\nEnter The Employee Number Of Which You Want To See Detail\n";
cin>>n;
for(int i=0;i<a;i++)
{ if(n==e[i].emp_no)
{ show(i); }}
getch(); }
OUTPUT
[6]. Write a menu driven program to calculate the TSA and volume of a
cube,cuboid,or
cylinder depending upon user’s choice. (using
Function overloading).
SOURCE CODE
#include<iostream.h>
#include<conio.h>
float tsa(float l,float b,float h)
{
float ar;
ar=2*(l*b+b*h+l*h);
return ar;
}
float tsa(float a)
{
float ar;
ar=6*(a*a);
return ar;
}
float tsa(float r,float h)
{ float ar;
ar=(2*3.14*r*(r+h));
return ar;
}
float vol(float l,float b,float h)
{ float vl;
vl=l*b*h;
return vl;
}
float vol(float a)
{
return(a*a*a);
}
float vol(float r,float h)
{ float vl;
vl=3.14*(r*r*h);
return vl;
}
void main(){ clrscr();
int n; char ans='y';
while(ans=='y'||ans=='Y'){
cout<<"\nEnter Your
Choice:-\n"<<"\t1.Cube\n"<<"\t2.Cuboid\n"<<"\t3.Cylinder\n";
cin>>n;
switch(n)
{ case 1:{ clrscr();
float a;
cout<<"\n\n\t\t\t\tCUBE\nEnter The Side Of The Cube\n";
cin>>a;
cout<<"\nThe Total Surface Area Of Cube Is\n"<<tsa(a);
cout<<"\nThe Volume Of Cube Is\n"<<vol(a);
cout<<"\nWant To Enter Again(y/n)\n";
cin>>ans;
}break;
case 2:{ clrscr();
float l,b,h;
cout<<"\n\n\t\t\t\tCUBOID\nEnter the Length \n";
cin>>l;
cout<<"\nEnter the Breadth \n";
cin>>b;
cout<<"\nEnter the Height \n";
cin>>h;
cout<<"\nThe Total Surface Is\n"<<tsa(l,b,h);
cout<<"\nThe Volume Is\n"<<vol(l,b,h);
cout<<"\nWant To Enter Again(y/n)\n";
cin>>ans;
}break;
case 3:{clrscr();
float r,h;
cout<<"\n\n\t\t\t\tCYLINDER\nEnter The Radius\n";
cin>>r;
cout<<"\nEnter The Height\n";
cin>>h;
cout<<"\nThe Total Surface Is\n"<<tsa(r,h);
cout<<"\nThe Volume Is\n"<<vol(r,h);
cout<<"\nWant To Enter Again(y/n)\n";
cin>>ans;
}break;
default:{cout<<"\nYou Have Entered A Wrong Choice...\nWant To Enter
Again(y/n)\n";
cin>>ans; }}}
getch(); }
OUTPUT
[7]. Define a class Outfit in C++ with the
following description
Private: OCode of type string
OType of Type string
OSize of type string
OFabric of type sTring
Price of type f loat
A function InitPrice() which calculates and
Assign the value of OPrice as follows:
For the v alue of OFabric as “ DENIM”
Type Price
TROUSER 1500
JACKET 2500
For the material other than “ DENIM” the above mentioned Price gets reduced by
25%.
Public: A function Input( ) to input the value of the data members OCode, OType,
OSize and OFabric and invoke the function InitPrice() .
A Display function That display the content of all the data members for the Outfit.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class outfit
{
private:
char ocode[20];
char otype[20];
char osize[20];
char ofabric[20];
float price;
void initprice()
{
if(strcmpi(ofabric,"denim")==0)
{ if(strcmpi(otype,"trouser")==0)
price=1500;
else if(strcmpi(otype,"jacket")==0)
price=2500;
}
else { if(strcmpi(otype,"trouser")==0)
price=1125;
else if(strcmpi(otype,"jacket")==0)
price=1875;
}}
public:
void input(){clrscr();
cout<<"\n\n\t\t\t\tINPUT\nEnter the Code \n";
gets(ocode);
cout<<"\nEnter the Type (Trouser/jacket)\n";
gets(otype);
cout<<"\nEnter the Size\n";
gets(osize);
cout<<"\nEnter the Fabric (Denim/Any Other)\n";
gets(ofabric);
initprice();
}
void output(){clrscr();
cout<<"\n\n\t\t\t\tOUTPUT\nThe Code Is\n"<<ocode;
cout<<"\n\nThe Type Is \n"<<otype;
cout<<"\n\nThe Size Is\n"<<osize;
cout<<"\n\nThe Fabric Is\n"<<ofabric;
cout<<"\n\nThe Price Is\n"<<price;
}};
void main(){
outfit ot;
ot.input();
ot. output();
getch();
}
OUTPUT

[8].Define a class student with the following specifications:


Private members of the class:
Admission Number-An Integer
Name -string of 20 characters
Class -Integer Roll Number-Integer
Public members of the cl ass:
getdata( )-To input the data
showdata( )-To display the data
Write a program to define an array of 10 object of this
class, input the data in this array and then display this
list .
SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student{
private:
int Admisn_no;
char Name[20];
int Class;
int Roll_no;
public:
void getdata(){
clrscr();
cout<<"\n\n\t\t\t\tINPUT\nEnter The Admission Number Of Student \n";
cin>>Admisn_no;
cout<<"\nEnter The Name Of The Student \n";
gets(Name);
cout<<"\nEnter The Class Of The Student\n";
cin>>Class;
cout<<"\nEnter The Roll Number Of The Student\n";
cin>>Roll_no;
}
void showdata(){
clrscr();
cout<<"\n\n\t\t\t\tOUTPUT\n The Admission Number Of Student Is
\n"<<Admisn_no;
cout<<"\n The Name Of The Student Is\n"<<Name;
cout<<"\n The Class Of The Student Is\n"<<Class;
cout<<"\n The Roll Number Of The Student Is\n"<<Roll_no;
}
int adms_no(){ return(Admisn_no);}
};
void main(){
student s[10]; // Array of 10 Objects of Class
int n;
for(int i=0;i<10;i++){
s[i].getdata();
} clrscr();
cout<<"\nEnter Admission Number of Student Of which You Want To See Thje
Detail\n";
cin>>n;
for(int j=0;j<10;j++)
{
if(n==s[j].Admisn_no)
{
s[j].showdata();
}}
getch();
}
OUTPUT
[9]. Create a class PERSON with the following specifications :
Private : Data members : name-character string, ph long.
Public : A constructor that initialize the name as ‘NULL’ and phone number as 0.
 Getdata() – to get all values from the user.
 Putdata() – to display all the data.
A destructor to destruct all the constructor .
Given that Person class above write the declaration of class Spouse and does the
following :
i. It has an extra data member – spousename.
ii. Redefine getdata and Putdaa function.
Now create another class Children that inherits from Spouse and does the following:
i. It has an extra data member – childname.
ii. Redefine getdata and Putdaa function.
Now create objects of all three classes and call Getdata() and Putdata().
Code :
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

class PERSON{
protected: char Name[100];
long int Ph_No;

public:
PERSON() {
strcpy(Name,"NULL");
Ph_No=0; }
void Getdata()
{ cout<<"\n\n"<<"Enter The Person Name : ";
cin>>Name;
cout<<"\n\n"<<"Enter The Phone Number : ";
cin>>Ph_No;
}
void Putdata()
{
cout<<"\n\n"<<"Person Name Is : "<<Name;
cout<<"\n\n"<<"Phone Number Is : "<<Ph_No;
}
~PERSON()
{ cout<<"\n\n"<<"Exit";
getch();
}P1;
class Spouse:public PERSON{
protected:
char spousename[100];
public:
void Getdata() {
PERSON::Getdata();
cout<<"\n\n"<<"Enter The Spouse Name : ";
cin>>spousename;
}
void Putdata(){
PERSON::Putdata();
cout<<"\n\n"<<"Spouse Name Is : "<<spousename;
}}S1;
class Children:public Spouse{
protected: char childname[100];
public:
void Getdata()
{ Spouse::Getdata();
cout<<"\n\n"<<"Enter The Child Name : ";
cin>>childname;
}

void Putdata()
{ Spouse::Putdata();
cout<<"\n\n"<<"Child Name Is : "<<childname;
} }C1;
void main()
{ clrscr();
P1.Getdata();
S1.Getdata();
C1.Getdata();
cout<<"\n\n"<<"Class Called By Object Of Class PERSON ";
P1.Putdata();
cout<<"\n\n"<<"Class Called By Object Of Class Spouse ";
S1.Putdata();
cout<<"\n\n"<<"Class Called By Object Of Class Children ";
C1.Putdata();
getch();
}

OUTPUT
[12]. Declare a structure telerec in C++, containing name[20] and telephone number.
Wrtite a program to maintain a file telephone record. the program should allow the
following functions on the file :
1) To append records in the file .
2) Display the name and the telephone number .if the telephone number .If
telephone number does not exixst display error message “record not found”.
3) Display the telephone number9s0 for a given name.If the name does not exixt
then display “record not found”.

SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<process.h>
#include<stdio.h>

class telephonerec{
long int tno;
char name[30];
public:
void getdata()
{ clrscr();
cout<<"\n\nEnter The Name : ";
gets(name);
cout<<"\nEnter Telephone Number : ";
cin>>tno; }
void putdata()
{ clrscr();
cout<<"\nName :\n";
puts(name);
cout<<"\nTelephone Number :\n ";
cout<<tno<<"\n";
}
void insert();
void searchbyno();
void searchbyname();
long gettellno(){ return tno; }
char *getname(){ return name; }
}s1;
void telephonerec::searchbyname(){
ifstream fin;
char name[100];
cout<<"Enter Name To Be Searched : ";
gets(name);
fin.open("telrec.txt",ios::in);
if(!fin){cout<<"cannt open";exit(1);}
while(fin.read((char*)&s1,sizeof(s1))){
if(strcmp(name,s1.getname())==0)
s1.putdata();
else
cout<<"\n Record not found";
}
fin.close();
getch();
}
void telephonerec::insert(){
ofstream fout;
fout.open("telrec.txt",ios::app|ios::out);
char ans='y';
while(ans=='y'){
cout<<"\n\t\t\tFILE INPUT\n";
s1.getdata();
fout.write((char*)&s1,sizeof(s1));
cout<<"Record added to file";
cout<<"\n Want to enter more record\n" ;
cin>>ans;
}
fout.close();
getch();
}
void telephonerec::searchbyno()
{ ifstream fin;
fin.open("telrec.txt",ios::in);
long int trnol;
cout<<"Enter Telephone No to be search";
cin>>trnol;
while(fin.read((char*)&s1,sizeof(s1))){
if(trnol==s1.gettellno())
s1.putdata();
else
cout<<"\n Record not found"; }
fin.close();
getch();
}
void main()
{ clrscr();
int ch;char choice;
do{
cout<<" 1. Insert \n 2. Search By Name \n 3. Search By Number \n 4. Exit\n";
cout<<"\nEnter your choice\n";
cin>>ch;
switch(ch){
case 1: s1.insert(); break;
case 2: s1.searchbyname(); break;
case 3: s1.searchbyno(); break;
default:cout<<"wrong choice"; exit(1);
} cout<<"\nWant to continue\n"<<endl; cin>>choice; }while(choice=='y');
getch();
}

OUTPUT
[15]. Write a program to perform Linear Search in an
array. ( Using functions).

SOURCE CODE
#include<iostream.h>
#include<conio.h>
int Lsearch(int a[],int n,int value)
{
for(int i=0;i<n;i++)
{ if(a[i]==value)
{ return i; } }
return(-1);
}
void main()
{
clrscr();
int ar[20],N,arr_value;
cout<<"\n Enter the size of array(max 20).\n";
cin>>N;
cout<<"\n Enter the array element\n";
for(int i=0;i<N;i++)
{ cin>>ar[i];
}
char ans='y';
while(ans=='Y'||ans=='y')
{
cout<<"\nEnter Element to be searched..\n";
cin>>arr_value;
if(Lsearch(ar,N,arr_value)==-1)
{ cout<<"\nSorry...\nSearch not found \n";
}
else
{ cout<<"\nSerach found at position "<<Lsearch(ar,N,arr_value)+1<<"\n";
}
cout<<"\n Want to enter again(y/n)..\n";
cin>>ans; }
getch(); }
OUTPUT

[16]. Write a program to perform Binary Search in an


array ( Using funCTION).

SOURCE CODE
#include<iostream.h>
#include<conio.h>
int Bsearch(int a[],int s,int value)
{
int beg=0,last=(s-1),mid;
while(beg<=last)
{
mid=(beg+last)/2;
if(value==a[mid])
return(mid);
else if(value>a[mid])
beg=mid+1;
else
last=mid-1;
}
return(-1);
}
void main()
{
clrscr();
int ar[20],n,ar_value,fvalue;
cout<<"\n Enter the size of the array(max 20);\n";
cin>>n;
cout<<"\n Enter the Array element \n";
for(int i=0;i<n;i++)
{ cin>>ar[i]; }
char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter the Element to be searched for..\n";
cin>>ar_value;
fvalue=Bsearch(ar,n,ar_value);
if(fvalue==-1)
{
cout<<"\n Sorry..Search Not Found\n";
}
else
{ cout<<"\n Search found at position "<<fvalue+1<<"\n";
}
cout<<"\nWant To Search Again(y/n)..\n";
cin>>ans;
} getch(); }
OUTPUT

[17].. Write a program to insert an element at a


ParTicular location in an array.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
int pos(int a[],int s,int value)
{
int pos;
if(value<a[0])
pos=0;
else
{ for(int i=0;i<s;i++)
{ if((a[i]<=value)&&(value<a[i+1]))
{ pos=i+1;
break;
}
}
if(i==s-1)
pos=s;
}
return(pos);
}
void main()
{
clrscr();
int ar[20],ar_value,n,fval;
cout<<"\n Enter the size of the array (max 20)\n";
cin>>n;
cout<<"\n Enter the array element\n";
for(int i=0;i<n;i++)
{ cin>>ar[i]; }
char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n Enter element to be inserted\n";
cin>>ar_value;
fval=pos(ar,n,ar_value);
for(i=n;i>fval;i--)
{ ar[i]=ar[i-1]; }
ar[fval]=ar_value;
n+=1;
cout<<"\n The updated array is:-\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" "<<"\n";
cout<<"\nWant to insert again(y/n)..\n";
cin>>ans;
}
getch();
}
OUTPUT

[18]. Write a pRogram to delete an element from an


Array.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
int search(int ar[],int s,int v)
{
for(int i=0;i<s;i++)
{
if(ar[i]==v)
return i;
}
return(-1);
}
void main()
{
clrscr();
int a[20],arr_value,n,index;
cout<<"\nEnter the array size(max 20)\n";
cin>>n;
cout<<"\nEnter the array elements\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
char ans='y';
while(ans=='Y'||ans=='y')
{
cout<<"\n Enter element to be deleted..\n";
cin>>arr_value;
index=search(a,n,arr_value);

if(index!=-1)
{
for(i=index;i<n;i++)
{ a[i]=a[i+1]; }
n-=1;
cout<<"\nThe array after deleting the element is:-\n";
for(i=0;i<n;i++)
{ cout<<a[i]<<" ";
}
cout<<"\n\nWant to delete again(y/n)..\n";
cin>>ans;
}
else
{ cout<<"\n Sorry...No such element found\n Want to enter
again(y/n)..\n";
cin>>ans;
}}
getch(); }
OUTPUT
[19] or [20]. Write a progam to perform Sel eTcion Sort to an
Array.

SOURCE CODE
#include<iostream.h>
#include<conio.h>
void selsort(int ar[],int s)
{
int sml,pos,tmp;
for(int i=0;i<(s-1);i++)
{ sml=ar[i];
pos=i;
for(int j=i+1;j<s;j++)
{
if(ar[j]<sml)
{ sml=ar[j]; pos=j; }
}
tmp=ar[i];
ar[i]=ar[pos];
ar[pos]=tmp;
cout<<"\n Array after pass "<<i+1<<" is : ";
for(j=0;j<s;j++)
cout<<ar[j]<<" ";
}
}
int main()
{ clrscr();
int a[20],value,n,fval;
cout<<"\n Enter the size of the array\n";
cin>>n;
cout<<"\n Enter the array element\n";
for(int i=0;i<n;i++)
{ cin>>a[i];
}
selsort(a,n);
cout<<"\n\n The sorted array is :-\n";
for(i=0;i<n;i++)
cout<<" "<<a[i]<<" ";
getch();
}

OUTPUT
[21]. Write a program to implement Insertion Sort to an array .
SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<limits.h> // for INT_MIN
void Insort(int ar[],int s)
{
int tmp,m;
ar[0]=INT_MIN;
for(int i=1;i<=s;i++)
{ tmp=ar[i];
m=i-1;
while(tmp<ar[m])
{
ar[m+1]=ar[m];
m--;
}
ar[m+1]=tmp;
cout<<"\n Array after "<<i<<" go is:- ";
for(int j=1;j<=s;j++)
cout<<ar[j]<<" ";
}
}
void main()
{ clrscr();
int a[20],ar_val,n,fval;
cout<<"\n Enter the size of the array (max 20)..\n";
cin>>n;
cout<<"\n Enter the array elements\n";
for(int i=0;i<n;i++)
{ cin>>a[i]; }
Insort(a,n);
cout<<"\n\n The array after Insertion sort is:-\n";
for(i=0;i<n;i++)
{ cout<<a[i]<<" "; }
getch(); }

OUTPUT
[22]. Write a program to merge two arrays which are
In ascending order, to give resultant in ascending/ descending order.
SOURCE CODE
#include<iostream.h>
#include<conio.h>
void Merge(int A[],int m,int B[],int n,int C[])
{ int a,b,c;
for(a=0,b=0,c=0;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()
{ clrscr();
int A[20],B[20],C[20],m,n,mn=0;
cout<<"\nEnter the size of First array (Ascending)..\n";
cin>>m;
cout<<"\nEnter the First array elements\n";
for(int i=0;i<m;i++)
cin>>A[i];
cout<<"\nEnter the size of secod array (Ascending)..\n";
cin>>n;
mn=m+n;
cout<<"\nEnter Second array elements\n";
for(i=0;i<n;i++)
cin>>B[i];
Merge(A,m,B,n,C);
cout<<"\n\nThe Merged array (Ascending) is:-\n";
for(i=0;i<mn;i++)
cout<<C[i]<<" ";
cout<<"\n\nThe Merged array (Descending) is:-\n";
for(i=(mn-1);i>=0;i--)
cout<<C[i]<<" ";
getch(); }
OUTPUT
[23]. Write a function in C++ which accepts an integer
Array and its size as arguments/ parameters and exchanges the values of first half
side elements with the second half side elements of the array .

SOURCE CODE
#include<iostream.h>
#include<conio.h>
void swap(int a[],int s)
{ int i,j,tmp,mid;
mid=s/2;
if(s%2==0)
j=mid;
else
j=mid+1;
for(i=0;i<mid;i++,j++)
{ tmp=a[i];
a[i]=a[j];
a[j]=tmp;
}}
void main()
{ clrscr();
int ar[20],n;
cout<<"\nEnter the size of the array (max 20)..\n";
cin>>n;
cout<<"\nEnter the elements of the array..\n";
for(int i=0;i<n;i++)
{ cin>>ar[i]; }
swap(ar,n);
cout<<"\nThe updated Array is..\n";
for(i=0;i<n;i++)
{ cout<<ar[i]<<" "; }
getch(); }
OUTPUT

[24]. Write a function in C++ which accepts an integer array and its size as
arguments/ parameters and
Assigns the elements in to a two dimensional array of
Integers in the following format :
If the array is1, 2, 3, 4, 5, 6 If the array is 1,2,3
The resultant 2D array is: The resultant 2D array is:
1 2 3 4 5 6 1 2 3
1 2 3 4 5 0 1 2 0
1 2 3 4 0 0 1 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0
SOURCE CODE
#include<iostream.h>
#include<conio.h>
void Updat(int a[20][20],int s)
{
int A[20][20];
for(int i=0;i<s;i++)
{ for(int j=0;j<s;j++)
{ { if((i+j)>=s)
A[i][j]=0;
else
A[i][j]=a[i][j];
}
a[i][j]=A[i][j];
} }}
void main()
{ clrscr();
int ar[20][20],n,i,j;
cout<<"\nEnter The 2D-Array Size (max 20)..\n";
cin>>n;
cout<<"\nEnter The 2D-Array Elements (Row wise)...\n";
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
{ cin>>ar[i][j]; }
cout<<"\n";
}
Updat(ar,n);
cout<<"\nThe Updated Array Is :-\n";
for(i=0;i<n;i++)
{ for(int j=0;j<n;j++)
{ cout<<" ";
cout<<ar[i][j]<<" ";
} cout<<"\n";
}
getch();
}
OUTPUT
[25]. Write a menu driven program which allows the user to perform the
following operations on a stack( Array implementation ) :
1)Push 2)Pop 3) Display

Source code
/* Operations On Array stack */
#include<iostream.h>
#include<conio.h>
#include<process.h>
int Pop(int [],int&);
int Push(int [],int&,int);
void Display(int [],int);
const int size=50;
void main()
{
clrscr();
int stack[size],no,top=-1,res;
char ch='y';
int n;
while(ch=='y'||ch=='Y')
{ clrscr();
cout<<"\nEnter your choice :-\n [1]. Push()\n [2]. Pop()\n [3]. Display()\n [4].
Exit()";
cin>>n;
switch(n){
case 1:{ char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n\t\tPush()\nEnter item for insertion :";
cin>>no;
res=Push(stack,top,no);
if(res==-1)
{ cout<<"\nOverflow!!! Programme closing";
exit(0); }
cout<<"\nThe stack now is\n";
Display(stack,top);
cout<<"\nWant to Push more... ";
cin>>ans;
}
}break;
case 2:{ char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n\t\tPop()\nDeletion of elements..";
res=Pop(stack,top);
if(res==-1)
{ cout<<"Underflow!!! Programme closing..";
exit(0); }
else { cout<<"\n Element deleted is:"<<res<<endl; }
cout<<"\nThe stack now is\n";
Display(stack,top);
cout<<"\n Want To Pop More";
cin>>ans;
}
}break;
case 3:{ cout<<"\nThe elements of Stack are\n";
Display(stack,top);
cout<<"\n Want To go back";
cin>>ch;
}break;
case 4:{ exit(0); }break;
default : { cout<<"\n You have entered a wrong choice..want to enter
again.";
cin>>ch; }break;
}
getch();
}
}
int Push(int stack[],int&top,int ele)
{
if(top==size-1) return(-1);
else { top++;
stack[top]=ele;
}
return(0);
}
int Pop(int stack[],int&top)
{ int ret;
if(top==-1) return(-1);
else { ret=stack[top];
top--;
}
return(ret);
}
void Display(int stack[],int top)
{ if(top==-1) return;
cout<<stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<endl;
}

Output
[26]. Write a menu driven program which allows the user to perform the following
operations on a queue ( Array implementation) :
1)Insert
2)Delete
3)Display

Source code
/* Operations On Array stack */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
int Delete(int []);
int Insert(int [],int);
void Display(int [],int,int);
const int size=50;
int queue[size],front=-1,rear=-1;
void main()
{
clrscr();
int no,res;
char ch='y';
int n;
while(ch=='y'||ch=='Y')
{ clrscr();
cout<<"\nEnter your choice :-\n [1]. Insert()\n [2]. Delete()\n [3]. Display()\n
[4]. Exit()\n";
cin>>n;
switch(n){
case 1:{ char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n\t\tInsert()\nEnter item for insertion :\n";
cin>>no;
res=Insert(queue,no);
if(res==-1)
{ cout<<"\nOverflow!!! Programme closing";
exit(0); }
cout<<"\nNow the queue (front to rear) is :\n";
Display(queue,front,rear);
cout<<"Want to insert more... ";
cin>>ans;
}
}break;
case 2:{ char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\n\t\tDelete()\nDeletion of elements..";
res=Delete(queue);
if(res==-1)
{ cout<<"Underflow!!! Programme closing..";
exit(0); }
else { cout<<"\n Element deleted is:"<<res<<endl; }
cout<<"\nThe queue now is (front to rear)\n";
Display(queue,front,rear);
cout<<"\n Want To Delete More";
cin>>ans; }
}break;
case 3:{ cout<<"\nThe elements of Queue are(front to rear) \n";
Display(queue,front,rear);
cout<<"\n Want To go back";
cin>>ch;
}break;
case 4:{ exit(0); }break;
default : { cout<<"\n You have entered a wrong choice..want to enter
again.";
cin>>ch; }break;
}
getch();
}
}
int Insert(int queue[],int ele)
{
if(rear==size-1) return(-1);
else if(rear==-1)
{ front=rear=0;
queue[rear]=ele; }
else{ rear++;
queue[rear]=ele; }
return(0);
}
int Delete(int queue[])
{ int ret;
if(front==-1) return(-1);
else { ret=queue[front];
if(front==rear) front=rear=-1;
else front++; }
return(ret);
}
void Display(int queue[],int front,int rear)
{ if(front==-1) return;
for(int i=front;i<rear;i++)
cout<<queue[i]<<"<-\n";
cout<<queue[rear]<<endl;
}

Output
[27]. Source code
/* Operations On linked stack */
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<process.h>
struct node{ int info;
node*next;
}*top,*newptr,*save,*ptr;
node*new_node(int);
void Pop();
void Push(node*);
void Display(node*);
void main()
{
clrscr();
top=NULL;
int inf;
char ch='y';
int n;
while(ch=='y'||ch=='Y')
{ clrscr();
cout<<"\nEnter your choice :-\n [1]. Push()\n [2]. Pop()\n [3]. Display()\n [4].
Exit()";
cin>>n;
switch(n){
case 1:{ char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\nEnter information for new node :";
cin>>inf;
newptr=new_node(inf);
if(newptr==NULL)
{ cout<<"\nCan't create new node!!! Programme closing";
exit(0); }
Push(newptr);
cout<<"\nWant to enter new node ... ";
cin>>ans;
}
}break;
case 2:{ char ans='y';
do{
cout<<"\nThe stack now is:\n";
Display(top); system("pause");
cout<<"\n Want To Pop an element(y/n)";
cin>>ans;
if(ans=='y'||ans=='Y')Pop();
}while(ans=='y'||ans=='Y');
}break;
case 3:{ cout<<"\nThe elements of Stack are\n";
Display(top);
cout<<"\n Want To go back";
cin>>ch;
}break;
case 4:{ exit(0); }break;
default : { cout<<"\n You have entered a wrong choice..want to enter
again.";
cin>>ch; }break;
}
getch();
}
}
node*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";
}
Output
[28].
Source code
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<process.h>
struct node{ int info;
node*next;
}*frnt,*newptr,*save,*ptr,*rar;
node*new_node(int);
void Insert(node*);
void Delnode();
void Display(node*);
void main()
{
clrscr();
frnt=rar=NULL;
int inf;
char ch='y';
int n;
while(ch=='y'||ch=='Y')
{ clrscr();
cout<<"\nEnter your choice :-\n [1]. Insret()\n [2]. Delete()\n [3]. Display()\n
[4]. Exit()";
cin>>n;
switch(n){
case 1:{ cout<<"\n\t\tInsert()";
char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\nEnter information for new node :";
cin>>inf;
newptr=new_node(inf);
if(newptr==NULL)
{ cout<<"\nCan't create new node!!! Programme closing";
exit(0); }
Insert(newptr);
cout<<"\nWant to enter new node ... ";
cin>>ans;
}
}break;
case 2:{ cout<<"\n\t\tDelete()"; char ans='y';
do{
cout<<"\nThe linked queue now is:\n";
Display(frnt);
cout<<"\n Want To delete node(y/n)";
cin>>ans;
if(ans=='y'||ans=='Y')Delnode();
}while(ans=='y'||ans=='Y');
}break;
case 3:{ cout<<"\nThe elements of Linked queue are\n";
Display(frnt);
cout<<"\n Want To go back";
cin>>ch;
}break;
case 4:{ exit(0); }break;
default : { cout<<"\n You have entered a wrong choice..want to enter
again.";
cin>>ch; }break;
}
getch();
}
}
node*new_node(int n){
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return(ptr);
}
void Insert(node*np)
{
if(frnt==NULL) { frnt=rar=np; }
else {
rar->next=np; rar=np;
}
}
void Delnode()
{
if(frnt==NULL) cout<<"Underflow!!!\n";
else{ ptr=frnt; frnt=frnt->next;
delete ptr; }
}
void Display(node*np)
{ while(np!=NULL) {
cout<<np->info<<" -> ";
np=np->next;
} cout<<"!!\n";
}
Output
[30]. Source code
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
int Delete(int cque[]);
int Insert(int [],int);
void Display(int [],int,int);
const int size=7;
int cque[size],front=-1,rear=-1;
void main()
{
clrscr();
int no,res;
char ch='y';
int n;
while(ch=='y'||ch=='Y')
{ clrscr();
cout<<"\nEnter your choice :-\n [1]. Insert()\n [2]. Delete()\n [3]. Display()\n
[4]. Exit()\n";
cin>>n;
switch(n){
case 1:{ char ans='y';
while(ans=='y'||ans=='Y')
{
cout<<"\nEnter item for insertion :\n";
cin>>no;
res=Insert(cque,no);
if(res==-1)
{ cout<<"\nOverflow!!! Programme closing";
exit(0); }
Display(cque,front,rear);
cout<<"Want to insert more... ";
cin>>ans;
}
}break;
case 2:{
cout<<"\n\t\tDelete()";
no=Delete(cque);
cout<<"\n Element deleted is:"<<no<<endl;
Display(cque,front,rear);
system("pause");
}break;
case 3:{ cout<<"\nThe elements of Circular Queue are(front to rear)
\n";
Display(cque,front,rear);
cout<<"\n Want To go back";
cin>>ch;
}break;
case 4:{ exit(0); }break;
default : { cout<<"\n You have entered a wrong choice..want to enter
again.";
cin>>ch; }break;
}
getch();
}
}
int Insert(int cque[],int ele)
{
if(((front==0)&&(rear==size-1))||(front==rear+1)) return(-1);
else if(rear==-1) front=rear=0;
else if(rear==size-1) rear=0;
else rear++;
cque[rear]=ele;
return(0);
}
int Delete(int cque[])
{ int ret;
if(front==-1) return(-1);
else { ret=cque[front];
if(front==rear) front=rear=-1;
else if(front==size-1)
front=0;
else front++;
}
return(ret);
}
void Display(int cque[],int front,int rear)
{ int i=0;
cout<<"\nCircular queue is\n"<<"(front shown as>>>,rear as <<< and free space
as-)\n";
if(front==-1) return;
if(rear>=front)
{ for(i=0;i<front;i++) cout<<"-";
cout<<">>>";
for(int i=front;i<rear;i++)
cout<<cque[i]<<"<-";
cout<<cque[rear]<<"<<<"<<endl;
}
else{ for(i=0;i<rear;i++) cout<<cque[i]<<" <- ";
cout<<cque[rear]<<"<<<";
for(;i<front;i++) cout<<"-";
cout<<">>>";
for(i=front;i<size;i++) cout<<cque[i]<<" <- ";
cout<<"\t...wrap around...";
}
}
Output

You might also like