You are on page 1of 94

COMPUTER

SCIENCE PRACTICAL
FILE

NAME:
CLASS: XII (Non medical) PGT-Computer Science
ROLL NO: Sign:_______________
Index
SNo. Program Description Date Sign.
1. Write a program to read a string and print out the following :
1) No. of capital alphabets,
2) No. of small alphabets,
3) No. of non-alphabets
2. 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.

3. Write a program to input 10 elements in an array and then display these


elements in reverse order.
4. Write a program to input elements in a 2D array and then display the sum of
main diagonal elements of this array.
5. Write a function to check whether a given string is palindrome or not.
6. 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 class:
getdata() - To input the data
showdata() - To display the data

Write a program to define an array of 10 objects of this class, input the data in
this array and then display this list.
7. Define a POINT class for two-dimensional points (x, y). Include a default
constructor, a copy constructor, a negate() function to transform the point into
its negative, a norm() function to return the point's distance from the origin
(0,0), and a print() function besides the functions to input and display the
coordinates of the point.
Use this class in a menu driven program to perform various operations on a
point.
8. Create a class student with data members name, class, section, roll No. and
function members getdata(), printdata(), and promote(). From this class derive a
class 'Sr_std' with additional data member stream. Also include another
function member change_stream().
Use these classes in a program.
9. Write a program to input the name of a text file from the user and display:
a) The number of blanks present in the file.
b) The number of lines present in the file.
c) The number of capital alphabets present in the file.
d) The number of small alphabets present in the file.
e) The number of lines starting with a capital alphabet.
f) The number of words present in the file.
g) The number of digits present in the file.
h) The number of words ending with a vowel
10. Write a program to input a text file name, read the contents of the file and
create a new file named COPY.TXT, which shall contain only those words
from the original file which don’t start with an uppercase vowel (i.e., with ‘A’,
‘E’, ‘I’, ‘O’, ‘U’). For example, if the original file contains
The First Step To Getting The Things You Want Out Of Life
is This: Decide What You Want. - Ben Stein

Then the text file COPY.TXT shall contain


The First Step To Getting The Things You Want Life is This:
Decide What You Want. - Ben Stein

11. Declare a structure telerec in C++, containing name (20 characters) and
telephone number. Write a program to maintain a binary file of telephone
records. The program should allow the following functions on the binary file:
1) To append records in the file.
2) Display the name for a given telephone number. If the telephone number
does not exist then display error message "record not found".
3) Display the telephone number(s) for a given name. If the name does not
exist then display error message "record not found".
12. Write a C++ Program to perform the following operations on the binary.dat file
1.Create Data File
2.Display Record From Data File
3.Modify Paricular Record From Data File
4.Delete Particular Record From Data File

13. Write a program including function TOPBOTTOM(intM[][5],int R,int C)in


C++, which finds and displays sum of the values in topmost row and sum of
values in bottommost row of a matrix M.
14. Write a program in c++ to perform the function in which values should be
multiplied by 2 if number is odd and multiplied by 3 if no is even in a 2-D
array.
15. Write a program in C++ to input a matrix and convert it into its transpose using
functions.
16. Write a program to perform transversal in 1-D array.
17. Write a program to swap the value through call by reference by using pointer .
18. Write a menu driven program which allows the user to perform the following
operations on a one dimensional array:

Insertion, deletion, searching, sorting (bubble, selection, insertion), display.

The program should automatically perform binary search if the array is sorted
and linear search otherwise.
19. Write a program to input integer data in two arrays. Sort one of the arrays in
ascending order and the other in descending order. Then merge them into a
third array so that the data in the third array is in ascending order. The program
should then display the data from all the three arrays.
20. Write a program to input two matrices, find their sum and difference depending
upon user’s choice, and then display the resultant matrix along with the original
matrices.
21. 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
22. 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
23. Write a menu driven program which allows the user to perform the following
operations on a stack (Linked implementation):
1) Push
2) Pop
3) Display
24. Write a menu driven program which allows the user to perform the following
functions on a queue (Linked implementation):
1) Insert
2) Delete
3) Display
25. Write a menu driven program which allows the user to perform the following
operations on a Circular queue (Array implementation):
1) Insert
2) Delete
3) Display
26 SQL Commands
Q1.Write a program to read a string and print out the following :
I. No. of capital alphabets
II. No. of small alphabets
III. No. of non-alphabets

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
void main()
{clrscr();
char str[100];
int a1=0,a2=0,a3=0;

cout<<"enter the string:-";


gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(isupper(str[i]))
{a1++;}
else if(islower(str[i]))
{a2++;}
else
{a3++;}
}
cout<<"\nNo. of uppercase alphabets"<<a1;
cout<<"\nNo. of lowercase alphabets"<<a2;
cout<<"\nNo. of non-alphabets"<<a3;
getch();}
OUTPUT
Q2.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.

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char str[100];
cout<<"enter the string:-";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(isupper(str[i]))
{ str[i]=tolower(str[i]);
}
else if(islower(str[i]))
{ str[i]=toupper(str[i]);
}
}
cout<<"\n\nThe modified string is "<<str;
getch();
}
OUTPUT
Q3. Write a program to input 10 elements in an array and then display these elements
in reverse order.

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i,a[10],size;
cout<<"Enter the size of array you want:";
cin>>size;
cout<<"\n Enter the array:\n";
for(i=0;i<size;i++)
{cin>>a[i];
}
cout<<"\n\nThe array in reverse is : \n";
for(i=size-1;i>=0;i--)
{
cout<<a[i]<<" ";
}
getch();
}

OUTPUT
Q4.Write a program to input elements in a 2D array and then display the sum of
main diagonal elements of this array.

#include<iostream.h>
#include<conio.h>
void sum(int m[10][10],int size)
{int p1=0,p2=0;
for(int i=0,j=size-1;i<size;i++,j--)

{p1=p1+m[i][i];
p2=p2+m[i][j];
}
cout<<"\n\nsum of diagonal 1="<<p1;
cout<<"\n\nsum of diagonal 2="<<p2;
}
void main()
{clrscr();
inti,j,a[10][10],size;
cout<<"enter the size=";
cin>>size;
for(i=0;i<size;i++)
{for(j=0;j<size;j++)
{cin>>a[i][j];
}}
sum(a,size);
getch();
}
OUTPUT
Q5.Write a function to check whether a given string is palindrome or not.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void checker(char ch[100])


{ char ch1[100],ch2[100];
strcpy(ch2,ch);
strcpy(ch1,strrev(ch));
if(strcmp(ch1,ch2)==0)
{
cout<<"\nstring is palindrome ";
}
else
{cout<<"\nstring is not palindrome" ;
}}
void main()
{
clrscr();
char cha[100];
cout<<"\nenter the string=";
gets(cha);
checker(cha);
getch();
}
OUTPUT
Q6.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 class:
getdata() - To input the data
showdata() - To display the data

Write a program to define an array of 10 objects of this class, input the data in this
array and then display this list.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

class student
{
int adm_no;
char name[10];
int cls;
int roll_no;

public:
void getdata()
{
cout<<"\nEnter the admission no:-";
cin>>adm_no;
cout<<"Enter the name:-";
gets(name);
cout<<"Enter the class:-";
cin>>cls;
cout<<"Enter the roll no:-";
cin>>roll_no;
}
void showdata()
{
cout<<"\nAdmission no:"<<adm_no;
cout<<"\nName:"<<name;
cout<<"\nClass:"<<cls;
cout<<"\nRoll no:"<<roll_no;
}
};

void main()
{clrscr();
student a[2];
for(int i=0;i<10;i++)
{
cout<<"\nenter the record of student "<<i+1<<":";
a[i].getdata();
}
for(i=0;i<10;i++)
{
a[i].showdata();
cout<<"\n";
}
getch();
}
OUTPUT
Q7.Define a POINT class for two-dimensional points (x, y). Include a default
constructor, a copy constructor, a negate() function to transform the point into its
negative, a norm() function to return the point's distance from the origin (0,0), and a
print() function besides the functions to input and display the coordinates of the
point.
Use this class in a menu driven program to perform various operations on a point.

#include<iostream.h>
#include<conio.h>
class POINT
{ intx,y;
public:
POINT()
{ x=y=0; }

POINT norm(POINT P1,POINT P2)


{ POINT D;
D.x=P1.x-P2.x;
D.y=P1.y-P2.y;
return D;
}
void input()
{ cout<<"\nEnter the co-ordinate x:";
cin>>x;
cout<<"\nEnter the co-ordinate y:";
cin>>y; }
void print()
{ cout<<"\nX="<<x;
cout<<"\nY="<<y; }
};
void main()
{ POINT P1,P2,P3,P;
clrscr();
cout<<"\nP1";
P1.input();
cout<<"\nP2";
P2.input();
P3=P.norm(P1,P2);
cout<<"\nP1";
P1.print();
cout<<"\nP2";
P2.print();
cout<<"\nDistance between two points:";
P3.print();
getch();
}

OUTPUT
Q8.Create a class student with data members name, class, section, roll No. and
function members getdata(), printdata(), and promote(). From this class derive a
class 'Sr_std' with additional data member stream. Also include another function
member change_stream().
Use these classes in a program.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class student
{
protected:
char name[40];
char clas[4];
char section;
int rollno;
float marks;
public:
void getdata()
{ cout<<"\nEnter the student name:";
gets(name);
cout<<"\nEnter the class:";
gets(clas);
cout<<"\nEnter the section:";
cin>>section;
cout<<"\nEnter the marks:";
cin>>marks;
}
Void printdata()
{ cout<<"\nthe student name:";
puts(name);
cout<<"\nclass:";
puts(clas);
cout<<"\nsection:"<<section;
cout<<"\nMarks:"<<marks;
}
void promote()
{
if(marks>=50)
{
cout<<"\nYOU ARE PROMOTED TO NEXT CLASS!";
}
else
{ cout<<"\nNOT PROMOTED!";
}
}
};
classsr_std:public student
{ protected:
char stream[12];
public:
void changestream()
{
if(marks>=50)
{ strcpy(stream,"science");
}
else if(marks>=33)
{
strcpy(stream,"commerce");

}
cout<<"\nStream is:"<<stream;
}
};
void main()
{
sr_std a;
clrscr();
a.getdata();
a.printdata();
a.promote();
a.changestream();
getch();
}
OUTPUT

––
Q9.Write a program to input the name of a text file from the user and display:
a) The number of blanks present in the file.
b) The number of lines present in the file.
c) The number of capital alphabets present in the file.
d) The number of small alphabets present in the file.
e) The number of lines starting with a capital alphabet.
f) The number of words present in the file.
g) The number of digits present in the file.
h) The number of words ending with a vowel

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<fstream.h>
#include<string.h>
void main()
{
clrscr();
ofstream ofs;
ofs.open("proj.txt");
char str,ch[100],z[10];
ofs<<"This Is for Test only.And for the File 123 !@#";
ofs.close();
ifstream ifs("proj.txt");
int m1=0,m2=0,m3=0,m4=0,m5=0,m6=0,m7=0,m8=0;
while(!ifs.eof())
{
ifs.getline(ch,100,'.');
if(ch[0]>='A'&&ch[0]<='Z')
{m4++;}
}
ifs.close();
ifs.open("proj.txt");
while(!ifs.eof())
{if(ifs.getline(ch,100,'.'))
{
m5++;
}

}
ifs.close();
ifs.open("proj.txt");
while(!ifs.eof())
{
ifs.get(str);
cout<<str;
if(str==' ')
{ m1++;
}
else if(isalpha(str))
{
m2++;
}
else if(isdigit(str))
{
m3++;
}
}
ifs.close();
ifs.open("proj.txt");
while(!ifs.eof())
{ifs.get(str);

if(islower(str))
{m6++;}
else
if(isupper(str))
{
m7++;
}
}
ifs.close();
ifs.open("proj.txt");

while(!ifs.eof())
{
ifs>>z;
int r;
r=strlen(z);
if(toupper(z[r-1])=='A'||toupper(z[r-1])=='E'||toupper(z[r-1])=='I'||toupper(z[r-1])=='O'||toupper(z[r-
1])=='U')
{
m8++;
}
}
cout<<"\n"<<"\nno. of blanks="<<m1<<"\nno. of words="<<m2<<"\nno. of digits="<<m3<<"\nno. of
lines starting with a capital alphabet="<<m4<<"\nno of lines= "<<m5<<"\nno. of small
alphabets="<<m6<<"\nno. of capital alphabets="<<m7<<"\nno. of words ending with a vowel="<<m8;
getch();
}

OUTPUT
Q10.Write a program to input a text file name, read the contents of the file and
create a new file named COPY.TXT, which shall contain only those words from the
original file which don’t start with an uppercase vowel (i.e., with ‘A’, ‘E’, ‘I’, ‘O’,
‘U’). For example, if the original file contains
The First Step To Getting The Things You Want Out Of Life is This:
Decide What You Want. - Ben Stein

Then the text file COPY.TXT shall contain


The First Step To Getting The Things You Want Life is This: Decide What
You Want. - Ben Stein

#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{clrscr();
ofstream old;
old.open("prog.txt");
old<<"The First Step To Getting The Things You Want Out Of Life is This: Decide What You Want. - Ben
Stein ";
old.close();
ifstream ifs("prog.txt");
ofstream ofs("COPY.TXT");
char str[10];

while(!ifs.eof())
{
ifs>>str;

if(str[0]=='A'||str[0]=='E'||str[0]=='I'||str[0]=='O'||str[0]=='U')
{
cout<<"";
}
else
{
ofs<<str;
cout<<str<<" ";
}
}
cout<<"\nDesired Content is copied";
ifs.close();
ofs.close();
getch();}

OUTPUT
Q11.Declare a structure telerec in C++, containing name (20 characters) and
telephone number. Write a program to maintain a binary file of telephone records.
The program should allow the following functions on the binary file:
1) To append records in the file.
2) Display the name for a given telephone number. If the telephone number does
not exist then display error message "record not found".
3) Display the telephone number(s) for a given name. If the name does not exist
then display error message "record not found".

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct telerec
{
char name[20];
long telno;
}s;
void input()
{
ofstream ofs;
ofs.open("records.dat",ios::binary|ios::app);
char ch;
while(1)
{
cout<<"\n enter the name:";
gets(s.name);
cout<<"\n enter the telephone no.:";
cin>>s.telno;
ofs.write((char*)&s,sizeof(s));
cout<<"do you want to add more record:";
cin>>ch;
if(ch=='n')
break;
}
ofs.close();
}
void search_ph_by_name()
{
char n[70];
int f=0;
cout<<"Enter the name (to search phone no.):";
gets(n);
ifstream ifs("records.dat",ios::in|ios::binary);
while(ifs.read((char*)&s,sizeof(s)))
{
if(strcmp(n,s.name)==0)
{cout<<"\ntelelpone no. is:"<<s.telno;
f=1;
break;
}
}
if(f==0)
{cout<<"record not found";}
ifs.close();
}
void search_name_by_phone()
{
long ph;
int f=0;
cout<<"Enter the phone no.(to serach name):";
cin>>ph;
ifstream ifs("records.dat",ios::in|ios::binary);
while(ifs.read((char*)&s,sizeof(s)))
{
if(ph==s.telno)
{cout<<"\nname is:"<<s.name;
f=1;
break;
}
}
if(f==0)
{cout<<"record not found";}
ifs.close();
}

void main()
{clrscr();
char n[20],choice,ch1='y';
int found=0,find=0;
long tn;
while(1)
{
cout<<"\n 1. Enter data";
cout<<"\n 2. Search by name";
cout<<"\n 3. Search by telephone";
cout<<"\n 4. Exit";
cout<<"\n\n Enter your choice:";

cin>>choice;
switch(choice)
{
case '1':
input();
break;

case '2':
search_ph_by_name();
break;
case '3':
search_name_by_phone();
break;
case '4':
exit(0);

}
}
getch();}
OUTPUT
Q12. Write a C++ Program to perform the following operations on the student.dat
file
1.Create Data File
2.Display Record From Data File
3.Modify Paricular Record From Data File
4.Delete Particular Record From Data File

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
class student
{
public:
int rno;
char name[30];
int age;
void input();
void output();
void create_file();
void read_file();
void modify_record();
void delete_record();
int getrno();
};
void student::input()
{
cout<<"\n enter roll no";
cin>>rno;
cout<<"\n enter name ";
gets(name); cout<<"\n enter age"; cin>>age;
}
void student::output()
{
cout<< "\n roll no:"<<rno; cout<< "\n name :"<<name; cout<< "\n age:"<<age; }
int student::getrno()
{ return rno;}
void student::create_file()
{
ofstream fout; char ch;
fout.open("student.dat", ios::out | ios:: binary);
clrscr();
student s;
do
{
s.input();
fout.write((char*)&s,sizeof(s));
cout<<"\n more record y/n";
cin>>ch;
}while(ch=='y' || ch=='Y');
fout.close();
}
void student::read_file()
{
ifstream fin;
student s;
fin.seekg(0);
fin.open("student.dat",ios::in | ios:: binary);
while(fin.read((char *) &s,sizeof(student)))
{
s.output();
cout<< "\n"; }
fin.close(); }
void student::modify_record()
{ student s; fstream file;
file.open("student.dat",ios::in|ios::ate|ios::binary);
int r,found=0;
cout<<"\n enter the rollo no of student whom data to be modified";
cin>>r;
file.seekg(0);
while(!file.eof())
{
int pos=file.tellg();
file.read((char*)&s,sizeof(s));
if(r==s.getrno())
{
cout<< "\n enter new record";
s.input();
file.seekp(pos,ios::beg);
file.write((char *)&s,sizeof(s));
cout<< "\n record modified successfully";
found=1;
break;
}
}
if(found==0)
{cout<<"\n Record is not found!";}
file.close();
}
void student::delete_record()
{ int r;
fstream file("student.dat", ios::in|ios::binary);
fstream newfile("newstu.dat",ios::out|ios::binary);
student s;
cout<<"\n enter the rollno no of student whom record to be deleted";
cin>>r;
while(file.read((char *)&s,sizeof(s)))
{
if (r!=s.getrno())
{
newfile.write((char *)&s,sizeof(s));
}
}
file.close();
newfile.close();
remove("student.dat");
rename("newstu.dat","student.dat");
}
void main()
{
int ch;
clrscr();
student s;
while(1)
{

cout<<"\nMAIN MENU\n1.Create\n2.Display\n3.Modify\n4.Delete\n5.Exit";
cout<<"\n ENTER THE CHOICE(1-6)";
cin>>ch;
getch();
switch(ch)
{
case 1:
s.create_file();
break;
case 2:
s.read_file();
break;
case 3:
s.modify_record();
break;
case 4:
s.delete_record();
break;
case 5:
exit(0);
default:
cout<<"\n INVALID CHOICE!";
}} }
OUTPUT
Q13. Write a program including function TOPBOTTOM(intM[][5],int R,int C)in C++,
which finds and displays sum of the values in topmost row and sum of values in
bottommost row of a matrix M.

#include<iostream.h>
#include<conio.h>

void calculate(int a[][10],int r,int c)


{
int i;
int s1=0,s2=0;

for(i=0;i<r;i++)
{
s1=s1+a[0][i];
s2=s2+a[c-1][i];
}
cout<<"\nSum of top row:"<<s1;
cout<<"\nSum of bottom row:"<<s2;
}
void main()
{
clrscr();
int b[10][10];
int p,q;
cout<<"\nEnter the no of rows:";
cin>>p;
cout<<"\nEnter the no of columns:";
cin>>q;
cout<<"\nEnter the array\n";
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
cin>>b[i][j];
}
}
calculate(b,p,q);

getch();
}
OUTPUT
Q14. Write a program in c++ to perform the function in which values should be
multiplied by 2 if number is odd and multiplied by 3 if no is even in a 2-D array.

#include<iostream.h>
#include<conio.h>

void calc(int a[][10],int r,int c)


{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]%2==0)
{
a[i][j]=a[i][j]*3;
}
else
{
a[i][j]=a[i][j]*2;
}
}

}
cout<<"New array is:";
for(i=0;i<r;i++)
{ cout<<"\n";
for(j=0;j<c;j++)
{
cout<<a[i][j]<<" ";
}
}

}
void main()
{
clrscr();
int b[10][10];
int p,q;
cout<<"\nEnter the no of rows:";
cin>>p;
cout<<"\nEnter the no of columns:";
cin>>q;

cout<<"\nEnter the array\n";

for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
cin>>b[i][j];
}
}
calc(b,p,q);
getch();
}

OUTPUT
Q15. Write a program in C++ to input a matrix and convert it into its transpose using
functions.
#include<iostream.h>
#include<conio.h>

void change(int a[][10],int r,int c)


{
int i,j;
int s=0;

for(i=0;i<r;i++)
{
for(j=i;j<c;j++)
{
s=a[i][j];
a[i][j]=a[j][i];
a[j][i]=s;
}
}
cout<<"\n New array is:";
for(i=0;i<r;i++)
{ cout<<"\n";
for(j=0;j<c;j++)
{
cout<<a[i][j]<<" ";
}
}

}
void main()
{
clrscr();
int b[10][10];
int p,q;
cout<<"\nEnter the no of rows:";
cin>>p;

cout<<"\nEnter the no of columns:";


cin>>q;
cout<<"\nEnter the array\n";

for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
cin>>b[i][j];
}
}
change(b,p,q);
getch();
}

OUTPUT
Q16. Write a program to perform transversal in 1-D array.

#include<iostream.h>
#include<conio.h>

void print_array(int a[],int n)


{

int i;
cout<<"\n Given array is :\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";

}
int sum(int a[],int n)
{
int i,s=0;
for(i=0;i<n;i++)
s=s+a[i];

return s;

}
void main()
{
clrscr();
int b[10];
int n,s;
cout<<"\nEnter the size of array:";
cin>>n;

cout<<"\nEnter the array\n";

for(int i=0;i<n;i++)
{
cin>>b[i];
}
print_array(b,n);
s=sum(b,n);
cout<<"\n Sum of all the elements of the given array is:"<<s;
getch();
}
OUTPUT
Q17. Write a program to swap the value through call by reference by using pointer

#include <iostream.h>
#include<conio.h>

void swap(int* p, int* q)


{
int temp;
temp = *p;
*p = *q;
*q = temp;
}

void main()
{ clrscr();
int a,b ;
cout<<"Enter the two numbers"<<endl;
cin>>a>>b;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

swap(&a, &b);

cout << "\nAfter swapping" << endl;


cout << "a = " << a << endl;
cout << "b = " << b << endl;
getch();
}
OUTPUT
Q18.Write a menu driven program which allows the user to perform the following
operations on a one dimensional array:

Insertion, deletion, searching, sorting (bubble, selection, insertion), display.

The program should automatically perform binary search if the array is sorted and
linear search otherwise.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void bubble_sort(intarr[],int size)


{

for(int i=0;i<size;i++)
{
for(int j=0;j<(size-i);j++)
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
cout<<"\nBubble sorting done:";
}
void display(int a[],int n)
{
cout<<"\nnew array is-\n";
for(in ti=0; i<n; i++)
{
cout<<a[i]<<" ";
}
}

void select_sort(int a[ ], int s)


{inti, j, p, felement;
for(i=0;i<s-1;i++)
{felement=a[i]; // initialize small with the first element of unsorted part of the array
p=i; // keep index of the smallest number of unsorted part of the array in p

for(j=i+1; j<s;j++) //loop for selecting the smallest element form unsorted array
{if(a[j]<felement)
{felement=a[j];
p=j;
}
}// end of inner loop----------
//----------exchange the smallest element with ith element-------------
a[p]=a[i];
a[i]=felement;

}
cout<<"\nSelect sorting done:";
}

void insert_sort(int a[ ],int n)


{inti, j,p;
for (i=1; i<n; i++)
{p=a[i];
j=i-1;

while(j>=0&&a[j]>p)
{
a[j+1]=a[j];
j--;
}
a[j+1]=p;
}
cout<<"\nInsertion sort done";
}

void linear_search(int a[], int size)


{
Int itm,i=0,fl=0;
cout<<"\nEnter the element to search:";
cin>>itm;

for(i=0;i<size;i++)
{
if(a[i]==itm)
{
fl=1;
break;
}
}
if(fl==1)
cout<<"\nElement is found at position "<<i+1;
else
cout<<"\nElement not found";
}

void Delete(int a[ ], int&n)


{
intitm;
inti=0;
cout<<"\nEnter the element to delete:";
cin>>itm;
while(i<n && a[i]<itm)
i++;
if (a[i]==itm)
{while (i<n)
{a[i]=a[i+1];
i++;
}
cout<<"\nRequired item is successfully deleted";
}
else
cout<<"\nRequired item is not found in the array";
n--;
}

void insert(int a[ ], int&size)


{
intitm;
inti=size-1;
cout<<"\nEnter the element you want to insert:";
cin>>itm;
while (i>=0 && a[i]>itm)
{
a[i+1]=a[i];
i--;
}
a[i+1]=itm;
size++;
}
void main()
{ clrscr();
intarr[20];
char choice;
inti=0,n;
cout<<"\nEnter the no of elements you want:-";
cin>>n;
cout<<"\nEnter the array: \n";
for(i=0;i<n;i++)
{cin>>arr[i];}
clrscr();
cout<<"\n1.Insert\n2.Delete\n3.Linear Search\n4.Insertion Sort\n5.Bubble Sort\n6.Selection
Sort\n7.Display\n8.Exit";
cout<<"\nEnter your choice:";
cin>>choice;
switch(choice)
{
case '1':
{
insert(arr,n);
display(arr,n);
break;
}
case '2':
{
Delete(arr,n);
display(arr,n);
break;
}
case '3':
{
linear_search(arr,n);
break;
}
case '4':
{
insert_sort(arr,n);
display(arr,n);
break;
}
case '5':
{
bubble_sort(arr,n);
display(arr,n);
break;
}
case '6':
{
select_sort(arr,n);
display(arr,n);
break;
}
case '7':
{
display(arr,n);
break;
}
case '8':
{ exit(0);
}
default:
{ cout<<"\n enter a valid choice-";
}
}

getch();}
OUTPUT
Q19. Write a program to input integer data in two arrays. Sort one of the arrays in
ascending order and the other in descending order. Then merge them into a third
array so that the data in the third array is in ascending order. The program should
then display the data from all the three arrays.

#include<iostream.h>
#include<conio.h>
#include<ctype.h>

void main()
{ clrscr();
int arr1[10],arr2[10],arr[20];
int i,j,n,size,temp;

cout<<"\nEnter the size of array:";


cin>>n;
cout<<"\nEnter the elements of array 1:\n";

for(i=0;i<n;i++)
{
cin>>arr1[i];
}
cout<<"\n Enter the elements of array 2:\n";

for(i=0;i<n;i++)
{
cin>>arr2[i];
}
//__________DESCENDING ORDER______________
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr1[i]<arr1[j])
{
temp=arr1[i];
arr1[i]=arr1[j];
arr1[j]=temp;
}
}
}
//____________ASCENDING ORDER____________
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr2[i]>arr2[j])
{
temp=arr2[j];
arr2[i]=arr2[j];
arr2[j]=temp;
}
}
}
//________________MERGING ARRAYS____________
size=2*n;
int k;
for(i=0;i<n;i++)
{
arr[i]=arr1[i];

}
for(i=0,k=n;k<size&&i<n;i++,k++)
{
arr[k]=arr2[i];
}

//_______MERGED ARRAY IN ASCENDING_________


for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}}
cout<<"\nNew Merged & Sorted array is\n ";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}

getch();
}
OUTPUT
Q20.Write a program to input two matrices, find their sum and difference depending
upon user’s choice, and then display the resultant matrix along with the original
matrices.

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void display(int a[3][3])
{
for(inti=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[i][j]<<" ";

}cout<<"\n";

}
}
void main()
{
clrscr();
int arr1[3][3], arr2[3][3], i, j, arr3[3][3];
char choice;
cout<<"\nEnter array 1 elements :\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>arr1[i][j];
}
}
cout<<"\nEnter array 2 elements :\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr2[i][j];
}
}
clrscr();
cout<<"\n1.ADD\n2.SUB\n3.EXIT";
cout<<"\n\nEnter your choice:";
cin>>choice;
switch(choice)
{
case '1':
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
}
}
cout<<"\nAddition successful";
cout<<"\nNew matrix will be :\n";
display(arr3);
break;
}
case '2':
{cout<<"\nSubtraction successful\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j]-arr2[i][j];
}
}
cout<<"\nResultant Array is:\n";
display(arr3);
break;
}
case '3':
{exit(0);
}
default:
{cout<<"\n Enter a valid choice";
}
}
getch();}
OUTPUT

3. EXIT

3. EXIT
Q21.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

#include<iostream.h>
#include<conio.h>
#define size 2
#include<stdlib.h>
class stack
{int a[size];
int top;
public:
stack()
{
top= -1;
}
void push(int item)
{if(top==size-1)
cout<<"\nStack is full\n\n";
else
a[++top]=item;

}
int pop()
{if (top==-1)
{cout<<"\nStack is empty\n";
return -1;
}
else
return a[top--];
}
void display(); };
void stack::display()
{
int temp=top;
while(temp>=0)
{
cout<<a[temp--]<<"\n";
}
}
void main()
{clrscr();
stack s;
int ch,a;
char str;
do

{
cout<<"\n1.Push\n2.Pop\n3.Exit" ;
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nEnter element:-";
cin>>a;
clrscr();
s.push(a);

s.display();
break;
}
case 2:
{clrscr();
s.pop();
s.display();
break;
}
case 3:
{
exit(0);
}
default:
{cout<<"\nEnter a valid choice";
}}
cout<<"\n____________________________________________________________________________
___";
cout<<"\nwant to continue(y/n):";
cin>>str;

}while(str=='Y'||str=='y');

getch();}
OUTPUT
Q22.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

#include<iostream.h>
#include<conio.h>
#define size 2
#include<stdlib.h>

class queue
{int front , rear;
int a[5];
public:
queue()
{
front=0;
rear=0;
}
void insert(int item)
{
if(rear==size)
cout<<"\nQueue is full"<<endl;
else
a[rear++]=item;}
int Delete()
{
if(front==rear)
{cout<<"Queue is empty"<<endl;
return 0;
}
else
return a[front++];
}
void display();};
void queue::display()
{
if(front==-1)
cout<<"\nQueue is empty";
else
{
for(inti=front;i<rear;i++)
{ cout<<a[i]<<"\n";}
}
}
void main()
{clrscr();
queue s;
int ch,a;
char str;
do

{
cout<<"\n1.Insert\n2.Delete\n3.Exit" ;
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nEnter element:-";
cin>>a;
clrscr();
s.insert(a);

s.display();
break;
}
case 2:
{clrscr();
s.Delete();
s.display();
break;
}
case 3:
{
exit(0); }
default:
{cout<<"\nEnter a valid choice"; }}
cout<<"\n________________________________________________________________________";
cout<<"\nwant to continue(y/n):";
cin>>str;
}while(str=='Y'||str=='y');
getch(); }
OUTPUT
Q23.Write a menu driven program which allows the user to perform the following
operations on a stack (Linked implementation):
1) Push
2) Pop
3) Display

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

class stack
{
struct node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};

void stack::push()
{
int item;
struct node *ptr;
cout<<"\nEnter a number to insert: ";
cin>>item;
ptr=new node;
ptr->data=item;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
}
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
}
temp=top;
top=top->next;
cout<<"\nPoped value is "<<temp->data;
delete temp;
}
void stack::display()
{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
}

void main()
{ clrscr();
stack s;
intch;
char a='a';
while(a=='a')
{
cout<<"1:PUSH\n2:POP\n3:DISPLAY\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>ch;
switch(ch)
{
case 1:
s.push();
clrscr();
s.display();
break;
case 2:
s.pop();
clrscr();
s.display();
break;
case 3:
clrscr();
s.display();
break;
case 4:
exit(0);
default:
cout<<"\nPlease enter correct choice!!";
break;
}
cin>>a;
clrscr();
}
getch();
}
OUTPUT
Q24.Write a menu driven program which allows the user to perform the following
functions on a queue (Linked implementation):
1) Insert
2) Delete
3) Display

#include<conio.h>
#include<iostream.h>
#include<process.h>
struct node
{
int data;
struct node *next;
};

class queue
{
struct node *front,*rear;
public:
queue()
{
front=rear=NULL;
}
void insert();
void Delete();
void display();
};

void queue::insert()
{
int value;
struct node *ptr;
cout<<"\nEnter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(front==NULL)
front=ptr;
else
rear->next=ptr;
rear=ptr;

}
void queue::Delete()
{
if(front==NULL)
{
cout<<"\nQueue is empty!!";
}
struct node *temp;
temp=front;
front=front->next;
cout<<"\nDeleted value is "<<temp->data;
delete temp;

}
void queue::display()
{
struct node *temp=front;
if(front==NULL)
{
cout<<"\nThe Queue is empty!!";
getch();
return;
}
cout<<"\nThe Queue is\n";
while(temp!=NULL)
{
cout<<temp->data<<" ->";
temp=temp->next;
}
cout<<"NULL";

}
void main()
{
clrscr();
queue q;
intch,cha=1;
while(cha==1)
{
cout<<"1:INSERT\n2:DELETE\n3:DISPLAY\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>ch;
switch(ch)
{
case 1:
q.insert();
q.display();
break;
case 2:
q.Delete();
q.display();
break;
case 3:
q.display();
break;
case 4:
exit(0);
break;
default:
cout<<"Please enter correct choice!!";
getch();
break;
}
cin>>cha;
clrscr();
}

}
OUTPUT
Q25.Write a menu driven program which allows the user to perform the following
operations on a Circular queue (Array implementation):
1) Insert
2) Delete
3) Display

#define size 20
class CQueue
{ int CQ[size];
int front, rear;
public:

CQueue()
{ front = rear = -1;

void Insert(int d)
{ if(( front==0 && rear==size-1 ) || ( front==rear+1 )) cout<<”Queue is Overflow”;
else if( rear == -1 )
{ front = rear = 0; CQ[rear]=d;
}
else if( rear == size-1 )
{ rear = 0; CQ[rear]=d;
}
else
{ rear++; CQ[rear]= d;
}
}
void Remove()
{ if( front == -1 ) cout<<”Queue is Empty”;
else if( front == rear )
{ cout<< “Element deleted from Queue is:”<<CQ[front] << endl ;
front = rear = -1;
}
else if( front == size-1 )
{ cout<< “Element deleted from Queue is:”<< CQ[front];
front = 0;

}
else

}
cout<< CQ[front++] ;

void display();
};

void queue::display()
{
Cout<<”\nQueue elements:\n”;
if( rear>= front )
{ for( int i=front; i<=rear; i++)
{ cout<<CQ[i]<<”<- \t” ;
}
}

else
{ for( int i=0; i<=rear; i++) cout<<CQ[i]<<”<- \t” ;

for( int i=front; i<size; i++) cout<<CQ[i]<<”<- \t” ;


}
}

void main()
{ CQueue Q1; char ans=’Y’; int c,d;
while(ans==’Y’)
{ cout<<”1.Insert \n 2.Delete \n 3Display\n4. Exit”;
cout<<”Enter your choice:”;
cin>>c;
switch( c )
{
case 1 : cout<<”\n Input the element for insertion in queue “;
cin>>d; Q1.Insert(d);
Q1.display();
break;

case 2 : Q1.Remove();
Q1.display();
break;

case 3 :
Q1.display();
break;
case 4:
exit(0);
}
cout<<”\n Want to continue”; cin>>ans;
}
}
OUTPUT
Enter password: *
mysql> create database teacher;
Query OK, 1 row affected (0.00 sec)
mysql> use teacher;
Database changed
mysql> create table student;

mysql> create table student(rollno numeric(2), name varchar(40),dob date);


Query OK,1 row affected(0.09 sec)
mysql> insert into student(rollno,name,dob) values(1,'aron','2000-08-06');
Query OK, 1 row affected (0.05 sec)
mysql> insert into student(rollno,name,dob) values(2,'brad','2000-06-08');
Query OK, 1 row affected (0.02 sec)
mysql> insert into student(rollno,name,dob) values(3,'andrez','2000-12-20');
Query OK, 1 row affected (0.03 sec)
mysql> insert into student(rollno,name,dob) values(4,'kang','2000-01-31');
Query OK, 1 row affected (0.02 sec)
mysql> select * from student;

+--------+---------+------------+
| rollno | name | dob |
+--------+---------+------------+
| 1 | aron | 2000-08-06 |
| 2 | brad | 2000-06-08 |
| 3 | andrez| 2000-12-20 |
| 4 | kang | 2001-01-31 |
+--------+---------+------------+
4 rows in set (0.01 sec)

mysql> select * from student where rollno>2 and name='andrez';


+--------+---------+------------+
| rollno | name | dob |
+--------+---------+------------+
| 3 | andrez | 2000-12-20 |
+--------+---------+------------+
1 row in set (0.02 sec)

mysql> select * from student where rollno between 3 and 4;;


+--------+---------+------------+
| rollno | name | dob |
+--------+---------+------------+
| 3 | andrez | 2000-12-20 |
| 4 | kang | 2001-01-31 |
+--------+---------+------------+
2 rows in set (0.01 sec)
mysql> select name from student group by dob desc;
+---------+
| name |
+---------+
| kang |
|andrez |
| aron |
| brad |
+---------+
4 rows in set (0.02 sec)
| kang|
| andrez |
| aron |
| brad |
+---------+
4 rows in set (0.02 sec)

mysql> select count(rollno) from student;


+---------------+
| count(rollno) |
+---------------+
| 4|
+---------------+
1 row in set (0.00 sec)

mysql> update student set name='andrez sam' where name='andrez';


Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student;


+--------+---------------+------------+
| rollno | name | dob |
+--------+---------------+------------+
| 1 | aron | 2000-08-06 |
| 2 | brad | 2000-06-08 |
| 3 | andrez sam | 2000-12-20 |
| 4 | kang | 2001-01-31 |
+--------+---------------+------------+
4 rows in set (0.00 sec)
mysql> update student set name='kang hyun' where name='kang';
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> update student set name='brad sin' where name='brad';


Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from student;


+--------+----------------+------------+
| rollno | name | dob|
+--------+----------------+------------+
| 1 | aron | 2000-08-06 |
| 2 |brad sin | 2000-06-08 |
| 3 | andrez sam | 2000-12-20 |
| 4 | kang hyun | 2001-01-31 |
+--------+----------------+------------+
4 rows in set (0.00 sec)

mysql> desc student;


+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| rollno | decimal(2,0) | YES| | NULL | |
| name | varchar(40) | YES| | NULL | |
| dob | date | YES| | NULL | |
+--------+--------------+------+-----+---------+-------+
3 rows in set (0.05 sec)

mysql> create table marks( rollno numeric(2), marks numeric(2));


Query OK, 0 rows affected (0.06 sec)

mysql> alter table student modify column name varchar(70);;


Query OK, 4 rows affected (0.19 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> alter table student modify column name varchar(70);


Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into marks(rollno,marks) values (1,50);


Query OK, 1 row affected (0.03 sec)
mysql> insert into marks(rollno,marks) values (2,50);
Query OK, 1 row affected (0.03 sec)

mysql> insert into marks(rollno,marks) values (3,50);


Query OK, 1 row affected (0.03 sec)

mysql> insert into marks(rollno,marks) values (4,50);


Query OK, 1 row affected (0.01 sec)

mysql> select rollno,marks+40 from marks;


+--------+----------+
| rollno | marks+40 |
+--------+----------+
| 1| 90 |
| 2| 90 |
| 3| 90 |
| 4| 90 |
+--------+----------+
4 rows in set (0.00 sec)

mysql> select m.rollno,s.name from marks m ,student s where m.rollno=s.rollno;


+--------+----------------+
| rollno | name |
+--------+----------------+
| 1 | aron |
| 2 | brad sin |
| 3 | andrez sam |
| 4 | kang hyun |
+--------+----------------+
4 rows in set (0.00 sec)

mysql> exit;

You might also like