Sie sind auf Seite 1von 35

Soma Seal, PGT(C.Sc.

) KV CISF RTC(A) Thakkolam Page 1


Index
S.No. Program Page
no.
1 Write a program to convert decimal to binary & binary to decimal 4
2 Write a program to find area of triangle using function overloading 5
3 Write a program to read a string and print out the following 6
4 Write a program to read a string and print it after replacing each of its capital 6
alphabets by the corresponding small alphabet and each small alphabet by its
corresponding capital alphabet
5 Write a program to input 2 matrices and then display multiplication of two 7
matrices
6 Write a program to input elements in a 2D array and then display the sum of 8
main diagonal elements of this array
7 Write a function to check whether a given string is palindrome or not 8
8 Define a class student with the following specifications: 9
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.

9 Create a class student with data members name, class, section, roll No. and 10
function members getdata() and printdata(). From this class derive a class
'Sr_std' with additional data member stream. Also include another function
member change_stream().

10 Write a program to read a text file “PASSAGE.TXT” and display the number of 11
words starting with a capital alphabet and the no. of occurrences of the word
“is”.
11 Write a program to input a text file name, read the contents of the file and 12
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
12 Given a binary file SPORTS.DAT, containing records of a structure type named 13
sports having 2 members, event and participant. Write a C++ program
having a function that would read contents from the file SPORTS.DAT and
create a file named ATHLETIC.DAT copying only those records from
SPORTS.DAT where the event name is “Athletics”.
13 A C++ program to Copy one file onto the end of another, adding line 14
numbers
14 A menu driven program to scan, append, modify and view records of a Binary 15
file
15 17
Menu driven telephone directory program (Binary File handling)

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 2


16 A program to implement Stack using Arrays 20
17 A program to implement Stack using Linked list 21
18 A program to implement Queue using Array 23
19 A program to implement Queue using Linked list 24
20 A Program to implement Circular Queue 26
21 A program to implement Linear search in an array 28
22 A Program to implement Binary Search in an array 29
23 30
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

24 K-map problem 33
If F(P,Q,R,S) = π (0,2,4,5,6,7,8,10,11,12,14) , obtain the simplified form using K-Map
25 SQL – 1 34
26 SQL – 2 35

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 3


Computer Science (083) Lab Manual for Class XII

C++ Programming

SN. Program Description


1. Write a program to convert decimal to binary & binary to decimal.

Source code –
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a,g,h,s=0,i=0,d,b,ch;
cout<<endl<<"Enter the choice - "<<endl<<"1:Binary to Decimal"<<endl<<"2:Decimal to
Binary"<<endl;
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter the Binary Digit"<<endl;
cin>>b;
while(b>0)
{
a=b%10;
s=s+a*(pow(2,i));
i=i+1;
b=b/10;
}
cout<<"Answer ="<<s<<endl;
break;
case 2: cout<<"Enter the Decimal"<<endl;
cin>>d;
while(d>0)
{
a=d%2;
a=a*pow(10,i);
s=s+a;
i=i+1;
d=d/2;
}
cout<<"Answer ="<<s<<endl;
break;
default: cout<<"Wrong choice"<<endl;
break;
}
getch();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 4


2. Write a program to find area of triangle using function overloading.

Source code –
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<math.h>
//function 1
void area(float r)
{float area_c;
area_c=(3.14*r*r);
cout<<"Area of the circle="<<area_c<<endl;
}
//function 2
void area(int l,int h)
{float area_r;
area_r=l*h;
cout<<"Area of the rectangle = "<<area_r<<endl;
}
//function 3
void area(float a,float b,float c)
{float s,area_t ;
s=(a+b+c)/2;
area_t=pow((s*(s-a)*(s-b)*(s-c)),0.5);
cout<<"Area of the triangle = "<<area_t<<endl;
}
void main()
{
clrscr();
int ch;
float r,a,b,c,l,h;
cout<<"1.Area of circle"<<endl<<"2.Area of rectangle"<<endl;cout<<"3.Area of
triangle"<<endl<<"4.Exit"<<endl;
X:
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter the radius of the circle"<<endl;
cin>>r;
void area (float);
area(r);
goto X;
case 2:cout<<"Enter the sides of rectangle"<<endl;
cin>>l>>h;
void area (float,float);
area (l,h);
goto X;
case 3:cout<<"Enter the sides of triangle"<<endl;
cin>>a>>b>>c;
void area (float,float,float);
area (a,b,c);
goto X;
case 4:exit(0);
default:cout<<"Wrong Choice"<<endl;
goto X;}
getch();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 5


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

Source code –
#include<iostream.h>
void main()
{
char str[30];
int U=0;L=0;N=0;
cout<<”Enter the string : “;
cin.getline(str,30);
for( int i=0;str[i]!=’\0’;i++)
{
if(isupper(str[i])
U++;
else if(islower(str[i])
L++;
else
N++;
}
cout<<”No. of capital alphabets : “<<U<<endl;
cout<<”No. of small alphabets : “<<L<<endl;
cout<<”No. of non-alphabets : “<<N<<endl;
}
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>
void main()
{
char str[30];
cout<<”Enter the string : “;
cin.getline(str,30);
for( int i=0;str[i]!=’\0’;i++)
{
if(isupper(str[i])
str[i]=tolower(str[i]);
else
str[i]=toupper(str[i]);
}
cout.write(str,30);
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 6


5. Write a program to input 2 matrices and then display multiplication of two matrices.

Source code –
#include <iostream.h>
int main()
{
int arr1[20][20],arr2[10][10];
int result[20][20];
int m,n;
int i,j;
// prompts the user to enter the rows and columns of the matrix
cout<<" Enter the number of rows (m) : ";
cin>>m;
cout<<" Enter the number of columns (n) : ";
cin>>n;
cout<<" \n Enter the elements of the 1st matrix \n ";
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
cout<<" Element "<<i<<" : "<<j<<" --> ";
cin>>arr1[i][j];
}
cout<<"\n Enter the elements of the 2nd matrix : \n ";
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
cout<<" Element "<<i<<" : "<<j<<" --> ";
cin>>arr2[i][j];
}
//------------ display matrics -----------------------
cout<<"\n 1st matrix \n ";
for (i=0; i<m; i++)
{ for (j=0; j<n; j++)
cout<<" "<<arr1[i][j];
cout<<"\n";
}
cout<<"\n 2nd matrix \n ";
for (i=0; i<m; i++)
{
for (j=0; j<n;j++)
cout<<" "<<arr2[i][j];
cout<<"\n";
}
cout<<" \n Resultatn matrix \n";
int k=0;
// multiplies the two matrices together
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
result[i][j] = 0;
for(k=0;k< m;k++)
result[i][j] = result[i][j] + arr1[i][k] * arr2[k][j];
} // end of j sub loop
} // end of i main loop
// displays the resultant matrix
for (i=0; i<m; i++)
for (j=0; j<n; j++)
cout<<result[i][j]<<" ";
cout<<"\n \n";
return 0;
} // end of main

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 7


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

Source code –
#include<iostream.h>
void main()
{
int arr[10][10];
int i,j,sum=0;
cout<”Enter the no. of rows : “;
cin>>m;
cout<<”Enter the no. of columns : “;
cin>>n;
cout<<”Enter the elements of the matrix :”<<endl;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
fin>>arr[i][j];
fout<<”The matrix is : “<<endl;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<arr[i][j]<<”\t”;
if(i==j)
sum += arr[i][j];
}
cout<<endl;
}
cout<<”Sum of the diagonal elements of the matrix is : “<<sum;
}
7. Write a function to check whether a given string is palindrome or not.

Source code –
#include<iostream.h>
#include<string.h>
void main()
{
char str[30];
int I,j,len,flag=0;
cout<<”Enter the string : “;
cin.getline(str,30);
len=strlen(str);
for(i=0,j=len-1;str[i]!=’\0’;i++,j--)
{
if(str[i]!=str[j])
{
flag=1;
break;
}
}
if(flag==1)
cout<<”string is not palindrome”;
else
cout<<”String is palindrome”;
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 8


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 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.

Source code –
#include<iostream.h>
class student
{
int adno;
char name[20];
int CLASS;
int rno;
public:
void getdata()
{
cout<<”Enter admission no. : “;
cin>>adno;
cout<<”Enter name : “;
cin>>name;
cout<<”Enter Class : “;
cin>>CLASS;
cout<<”Enter Roll no. : “;
cin>>rno;
}
void showdata()
{
cout<<adno<<endl;
cout<<name<<endl;
cout<<CLASS<<endl;
cout<<rno<<endl;
}
};
void main()
{
student s[10];
cout<<”Enter values for 10 students : “;
for(int i=0;i<10;i++)
{
cout<<”Enter values for student “<<i+1<<” : “;
s[i].getdata();
}
cout<<”Values for 10 students : “;
for(i=0;i<10;i++)
{
cout<<”Values for student “<<i+1<<” : “;
s[i].showdata();
}
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 9


9. Create a class student with data members name, class, section, roll No. and function members
getdata() and printdata(). 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.

Source code –
#include<iostream.h>
class student
{
char name[20];
int CLASS;
char section;
int rno;
public:
void getdata()
{
cout<<”Enter roll no., name, class and section : “;
cin>>rno<<name<<CLASS<<section;
}
void printdata()
{
cout<<rno<<endl;
cout<<name<<endl;
cout<<CLASS<<endl;
cout<<section<<endl;
}
};
class Sr_std: public student
{
char stream[20];
public:
void change_stream()
{
getdata();
cout<<”enter stream : “;
cin>>stream;
}
void display()
{
printdata();
cout<<stream;
}
};

void main()
{
Sr_std s;
cout<<”Enter details of student :”;
s.change_stream();
cout<<”The details are – “;
s.display();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 10


10. Write a program to read a text file “PASSAGE.TXT” and display the number of words starting with a
capital alphabet and the no. of occurrences of the word “is”.

Source code –
#include<fstream.h>
#include<string.h>
#include<process.h>
void main()
{
int wc=0,uc=0;
ifstream fin(“PASSAGE.TXT”, ios::in);
if(!fin)
{
cout<<”File cannot be opened!!!”;
exit(0);
}
else
{
while(fin)
{
fin>>word;
if(strcmp(word,”is”)==0 || strcmp(word,”Is”)==0)
wc++;
if(isupper(word[0])
uc++;
}
}
cout<<”The no. of words starting with capital letter = “<<uc<<endl;
cout<<”The no. of occurrences of word \”is\” = “<<wc;
fin.close();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 11


11. 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

Source code –
#include<fstream.h>
#include<string.h>
#include<process.h>
Void main()
{
Ifstream fin(“PASSAGE.TXT”, ios::in);
Ofstream fout(“COPY.TXT”, ios::out);
If(!fin || !fout)
{
Cout<<”File cannot be opened!!!”;
Exit(0);
}
Else
{
While(fin)
{
Fin>>word;
If(word[0]==’A’ || word[0]==’E’ || word[0]==’I’ || word[0]==’O’
|| word[0]==’U’)
Fout<<word<<” “;
}
}
Fin.close();
Fout.close();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 12


12. Given a binary file SPORTS.DAT, containing records of a structure type named sports having 2
members, event and participant. Write a C++ program having a function that would read contents
from the file SPORTS.DAT and create a file named ATHLETIC.DAT copying only those records from
SPORTS.DAT where the event name is “Athletics”.

Source code –
#include<fstream.h>
#include<string.h>
#include<process.h>
Struct sports
{
Char event[20];
Char participant[10][30];
};
Void main()
{
Ifstream fin(“SPORTS.DAT”, ios::in|ios::binary);
Ofstream fout(“ATHLETIC.DAT”, ios::out|ios::binary);
Sports s1;
If(!fin || !fout)
{
Cout<<”File cannot be opened!!!”;
Exit(0);
}
Else
{
While(fin)
{
Fin.read((char *)&s1, sizeof(s1));
If(strcmp(s1.event,”athletics”)==0)
Fout.write((char*)&s1,sizeof(s1));
}
}
Fin.close();
Fout.close();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 13


13. A C++ program to Copy one file onto the end of another, adding line numbers.

Source Code:

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

/*
* Copy one file onto the end of another, adding line numbers
*/

int main ()
{
char myline[256];
int lc = 0;

ofstream outfile("demo.txt",ios::app);

ifstream infile("stdcodes.xyz");
if (! infile) {
cerr << "Failed to open input file\n";
exit(1);
}

while (1) {
infile.getline(myline,256);
if (infile.eof()) break;
lc++;
outfile << lc << ": " << myline << "\n";
}
infile.close();
outfile.close();

cout << "Output " << lc << " records" << endl;

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 14


14.
A menu driven program to scan, append, modify and view records of a Binary file.

Source code:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

Static int totrec=0; //totrec variable keep track for total variable entered//Initially Record
scanned are Zero
void main()
{
int choice;
while(1)
{
clrscr();
cout<<"Choose your choice\nNOTE : one choice for one record(except viewing data)\n";
cout<<"1) Scanning initial records\n";
cout<<"2) Appending records\n";
cout<<"3) Modifying or append records\n";
cout<<"4) Viewing records\n";
cout<<"5) Exit\n";
cout<<"Enter your choice : ";
cin>>choice;
switch (choice)
{
case 1 : {
ofstream outfile;
outfile.open("emp",ios::out);
cout<<"\n\nPlease enter the details as per demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name;
outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by him\her : ";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec= totrec + 1;
outfile.close();
}
break;
case 2 : {
ofstream outfile;
outfile.open("emp",ios::app);
cout<<"\n\nPlease enter the details as per demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name; outfile<<name<<endl;
cout<<"Enter Age : ";
int age; cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by him : ";
char lang[25];
cin>>lang; outfile<<lang<<endl;
totrec = totrec + 1; outfile.close(); }
break;
case 3 : { ofstream outfile; outfile.open("emp",ios::ate);
cout<<"Are you interested in adding record\nenter y or n\n";
Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 15
char ans;
cin>>ans;
if(ans=='y' || ans=='Y')
{
cout<<"\n\nPlease enter the details as per demanded\n";
cout<<"\nEnter the name : ";
char name[20];
cin>>name;
outfile<<name<<endl;
cout<<"Enter Age : ";
int age;
cin>>age;
outfile<<age<<endl;
cout<<"Enter programming language known by him : ";
char lang[25];
cin>>lang;
outfile<<lang<<endl;
totrec = totrec + 1;
}
outfile.close();
}
break;
case 4 : {
ifstream infile;
infile.open("emp",ios::in);
const int size=80;
char line[size]; int counter=totrec;
while(counter > 0)
{
infile.getline(line,size);
cout<<"\n\nNAME : "<<line<<endl;
infile.getline(line,size);
cout<<"AGE : "<<line<<endl;
infile.getline(line,size);
cout<<"LANGUAGE : "<<line<<endl;
counter--;
}
infile.close();
}
getch();
break;
case 5 : break;
default : cout<<"\nInvalid Choice\nTRY AGAIN\n";
}
}
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 16


15.
Menu driven telephone directory program (Binary File handling)

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

class phoneBook{
char name[20],phno[6];
public:
void getdata();
void showdata();
char *getname(){ return name; }
char *getphno(){ return phno; }
void update(char *nm,char *telno){
strcpy(name,nm);
strcpy(phno,telno);
}
};

void phoneBook :: getdata(){


cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phno;
}

void phoneBook :: showdata(){


cout<<"\n";
cout<<setw(15)<<name;
cout<<setw(8)<<phno;
}
void main(){
phoneBook rec; fstream file;
file.open("c:\\phone.dat", ios::ate | ios::in | ios::out | ios::binary);
char ch,nm[20],telno[6]; int choice,found=0;
while(1){
clrscr();
cout<<"\n*****Phone Book*****\n";
cout<<"1) Add New Record\n";
cout<<"2) Display All Records\n";
cout<<"3) Search Telephone No.\n";
cout<<"4) Search Person Name\n";
cout<<"5) Update Telephone No.\n";
cout<<"6) Exit\n";
cout<<"Choose your choice : ";
cin>>choice;
switch(choice){
case 1 : //New Record
rec.getdata();
cin.get(ch);
file.write((char *) &rec, sizeof(rec)); break;
case 2 : //Display All Records
file.seekg(0,ios::beg);
cout<<"\n\nRecords in Phone Book\n";
while(file){
file.read((char *) &rec, sizeof(rec));
if(!file.eof())
rec.showdata();
}
file.clear(); getch(); break;
Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 17
case 3 : //Search Tel. no. when person name is known.
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(nm,rec.getname())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;

case 4 : //Search name on basis of tel. no


cout<<"\n\nEnter Telephone No : ";
cin>>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec)))
{
if(strcmp(telno,rec.getphno())==0)
{
found=1;
rec.showdata();
}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
getch();
break;
case 5 : //Update Telephone No.
cout<<"\n\nEnter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0; int cnt=0;
while(file.read((char *) &rec, sizeof(rec)))
{
cnt++;
if(strcmp(nm,rec.getname())==0)
{ found=1; break;}
}
file.clear();
if(found==0)
cout<<"\n\n---Record Not found---\n";
else
{
int location = (cnt-1) * sizeof(rec);
cin.get(ch);
if(file.eof())
file.clear();
cout<<"Enter New Telephone No : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec, sizeof(rec));
Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 18
file.flush();

}
break;
case 6 : break;
}
}
file.close();
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 19


16. A program to implement Stack using Arrays.

Source Code –

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";
return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};

main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 20


break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}

17. A program to implement Stack using Linked list.

Source Code –

#include <cstdlib>
#include<iostream>
using namespace std;

class node
{
public:
int data;
node* next;
};

class StackusingList
{
public:
StackusingList(int max)
{
top = NULL;
maxnum = max;
count=0;
}

void push(int element)


{
if(count == maxnum)
cout<<"stack is full";
else
{
node *newTop = new node;
if(top == NULL)
{
newTop->data = element;
newTop->next = NULL;
top = newTop;
count++;
}
else
{
newTop->data = element;
newTop->next = top;
top = newTop;
count++;
}
}
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 21


void pop()
{
if(top == NULL)
cout<< "nothing to pop";
else
{
node * old = top;
top = top->next;
count--;
delete(old);
}
}
void print()
{
node *temp;
temp = top;
while(temp!=NULL)
{
cout<<temp->data<<",";
temp=temp->next;
}
}
private:
node *top;
int count; //head
int maxnum;
int stackData;
};

int main(int argc, char** argv) {


StackusingList *sl = new StackusingList(5);
sl->push(1);
sl->push(2);
sl->push(3);
sl->push(4);
sl->push(5);
sl->push(6);

sl->pop();
cout<<"new stack\n";
sl->print();

return 0;
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 22


18. A program to implement Queue using Array.

Source code –
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1; front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1; return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow"; return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty"; return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
main()
{
int ch; queue qu;
while(1)
{
cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch; qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}
Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 23
19. A program to implement Queue using Linked list.

Source Code -
#include<iostream>
#include<cstdlib>
using namespace std;
struct node{
int info;
struct node *next;
};
class Queue{
private:
node *rear; node *front;
public:
Queue();
void enqueue();
void dequeue();
void display();
};
Queue::Queue(){
rear = NULL; front = NULL;
}
void Queue::enqueue(){
int data;
node *temp = new node;
cout<<"Enter the data to enqueue: ";
cin>>data;
temp->info = data; temp->next = NULL;
if(front == NULL){
front = temp;
}else{
rear->next = temp;
}
rear = temp;
}
void Queue::dequeue(){
node *temp = new node;
if(front == NULL){
cout<<"\nQueue is Emtpty\n";
}else{
temp = front;
front = front->next;
cout<<"The data Dequeued is "<<temp->info;
delete temp; }
}
void Queue::display(){
node *p = new node;
p = front;
if(front == NULL){
cout<<"\nNothing to Display\n";
}else{
while(p!=NULL){
cout<<endl<<p->info;
p = p->next; } }
}
int main(){
Queue queue; int choice;
while(true){
cout<<"\n1.Enqueue\n2. Dequeue\n3. Display\n 4.Quit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 24
case 1:
queue.enqueue();
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
exit(0);
break;
default:
cout<<"\nInvalid Input. Try again! \n";
break;
}
}
return 0;
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 25


20. A Program to implement Circular Queue.

Source Code –

# include <iostream.h>
# include <process.h>
# include <conio.h>
int display_menu();
class circularqueue
{
int arr[10];
int front,rear;
int size;
public:
circularqueue()
{
front=0;
rear=0;
size=10;
}
void display();
void enqueue();
void delete_element();
};

void circularqueue :: display()


{
cout<<endl;
if(front!=0 && rear!=0)
{
int i=front;
cout<<"arr["<<i<<"] :"<<arr[i]<<endl;
while(i!=rear)
{
i=(i % size)+1;
cout<<"arr["<<i<<"] :"<<arr[i]<<endl;
}

}
else
{
cout<<"Queue is empty"<<endl;
}
getch();
}

void circularqueue :: enqueue()


{
cout<<endl;
if(front==0 && rear==0)
{
cout<<"Enter Number to enqueue at Position arr["<<rear+1<<"] :";
cin>>arr[1];
rear=1;
front=1;
}
else
{
int next=(rear % size)+1;
if(next==front)
{
cout<<"Queue is Full ..."; getch(); }
Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 26
Else
{
cout<<"Enter Number to enqueue at Position arr["<<next<<"] :";

cin>>arr[next];

rear=next;
}
}
}

void circularqueue :: delete_element()


{
cout<<endl;
if(rear==0 && front==0)
{
cout<<"Queue is empty ...";
getch();
return;
}

if(rear==front)
{
rear=0;
front=0;
}
else
{
front=(front % size)+1;
}
}
void main()
{
circularqueue cq1;
while(1)
{
switch(display_menu())
{
case 1: cq1.enqueue();
break;
case 2: cq1.delete_element();
break;
case 3: cq1.display();
break;
case 4: exit(1);
}
}
}
int display_menu()
{
int c;
clrscr();
cout<<endl;
cout<<"| 1 | : Enqueue element"<<endl;
cout<<"| 2 | : Delete element"<<endl;
cout<<"| 3 | : Display"<<endl;
cout<<"| 4 | : Exit"<<endl;
cout<<"Enter your Choice :";
cin>>c;
return c;
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 27


21.
A program to implement Linear search in an array.

Source Code –

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

int lsearch(int Arr[], int s, int VAL);

int main()
{
int Arr[100],n,val,found;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);

if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";

getch();
return 0;
}

int lsearch(int Arr[], int s, int VAL)


{
for(int I=0; I<s; I++)
{
if(Arr[I]==VAL)
return 1;
}
return 0;
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 28


22.
A Program to implement Binary Search in an array.

Source Code –

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

int bsearch(int AR[], int N, int VAL);

int main()
{
int AR[100],n,val,found;
cout<<"Enter number of elements you want to insert ";
cin>>n;
cout<<"Enter element in ascending order\n";
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);

if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";

getch();
return 0;
}

int bsearch(int AR[], int N, int VAL)


{
int Mid,Lbound=0,Ubound=N-1;

while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2;
if(VAL>AR[Mid])
Lbound=Mid+1;
else if(VAL<AR[Mid])
Ubound=Mid-1;
else
return 1;
}

return 0;
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 29


23.
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two parameters name of the array
and number of elements in the array.

Source Code –

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

void accept(int Arr[], int s);


void display(int Arr[], int s);
void isort(int Arr[], int s);
void ssort(int Arr[], int s);
void bsort(int Arr[],int s);

int main()
{
int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n;
do
{
cout<<"\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort method";
cout<<"\n4. Sort the array using selection sort method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;

switch(choice)
{
case 1: accept(Arr,n);
break;
case 2: display(Arr,n);
break;
case 3: isort(Arr,n);
break;
case 4: ssort(Arr,n);
break;
case 5: bsort(Arr,n);
break;
case 6: break;
default:cout<<"\nInvalid choice";
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 30


}while(choice!=6);

return 0;
}

void accept(int Arr[], int s)


{
for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}

void display(int Arr[], int s)


{
cout<<"The elements of the array are:\n";
for(int I=0;I<s;I++)
cout<<Arr[I]<<" ";

void isort(int Arr[], int s)


{
int I,J,Temp;
for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1;
while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{
int I,J,Temp,Small;
for(I=0;I<s-1;I++)
{
Small=I;
for(J=I+1;J<s;J++) //finding the smallest element
if(Arr[J]<Arr[Small])
Small=J;
if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 31


void bsort(int Arr[],int s)
{
int I,J,Temp;
for(I=0;I<s-1;I++) //sorting
{
for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 32


25 If F(P,Q,R,S) = π (0,2,4,5,6,7,8,10,11,12,14) , obtain the simplified form using K-Map

Reducing:
Octet = M0.M2.M4.M6.M8.M10.M12.M14
=S
Quad = M4.M5.M6.M7
= P+Q’
Pair = M10.M11
= P’+Q+R’

Therefore POS of F(P,Q,R,S)= S.(P+Q’).(P’+Q+R’)

S.(P+Q’).(P’+Q+R’)
S

P
Q
R

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 33


Index for SQL

1. Consider the following tables EMPLOYEES and EMPSALARY. Write SQL commands for the
statements(i) to (iv) and give outputs for SQL queries (v) to (viii).

Books
Bookid BookName AuthorName Publisher Price Type Qty
C0001 Fast Cook Lata Kapoor EPB 355 Cookery 5
F0001 The Tears William Hopkins First Publ 650 Fiction 20
T0001 My First C++ Brain & Brooke EPB 350 Text 10
T0002 C++ Brainworks A.W. Rossaine TDH 350 Text 15
F0002 Thunderbolts Anna Roberts First publ. 750 Fiction 50

Issued
Bookid QtyIssued
T0001 4
C0001 5
F0001 2

(i) To display Book name, author name and price of First publishers books.
ANS: SELECT BookNmae, AuthorName,Price from Books where Publisher="First
Publ";

(ii) To display the names and price of books in ascending order of their price.
ANS:SELECT BookName, Price FROM Books ORDER BY Price;

(iii) To display bookid, bookname, type & qtyissued


ANS: SELECT Book.Bookid, BookName, Type, QtyIssued from Books, Isssued Where
Books.Bookid=Issued.Bookid;

(iv) To increase the price of all books of EPB publishers by 50.


ANS: UPDATE Books SET Price= Price + 50 WHERE Publisher = "EPB";

(v) SELECT Count(*) FROM Books;


ANS: 5

(vi) SELECT BookName , AuthorName FROM Books WHERE Publisher = "EPB";


ANS:
Fast Cook Lata Kapoor
My First C++ Brain & Brooke

(vii) SELECT COUNT(DISTINCT Publishers ) FROM Books WHERE Price>=400;


ANS: 1

(viii) SELECT MAX(Price) From Books WHERE Qty > 15;


ANS: 750

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 34


2. Consider the table Worker given below Write commands in MySql for (i) to (iv) and output
for (v) to (vii) :

(i) To display the details of all WORKERs in descending order of DOB.


Ans: SELECT * FROM WORKER ORDER BY DOB DESC;

(ii) To display NAME and DESIG of those WORKERs, whose PLEVEL is either
P001 or P002.
Ans: SELECT NAME, DESIG FROM WORKER WHERE PLEVEL = ‘P001’ OR PLEVEL = ‘P002’;
OR
(iii) To display the content of all the WORKERs table, whose DOB is in between '19-JAN-1984'
and '18-JAN-1987'.
Ans: SELECT * FROM WORKER WHERE DOB>=’1984-01-19’ AND DOB<=’1987-01-18;

(iv) To add a new row with the following:


19, 'Daya Kishore', 'Operator', 'P003', '19-Jun-2008', '11-Jun-1984'
Ans: INSERT INTO WORKER VALUES (19, 'Daya Kishore', 'Operator', 'P003',
'2008-06-19', '1984-6-11');

(v) SELECT COUNT (PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;


Ans: COUNT (PLEVEL) PLEVEL
1 P001
2 P003
3 P002

(vi) SELECT MAX (DOB), MIN (DOJ) FROM WORKER;


Ans: MAX (DOB) MIN (DOJ)
1987-07-12 2004-09-13

(vii) SELECT DISTINCT DESIG FROM WROKER.


Ans: DISTINCT DESIG
Supervisor
Operator
Mechanic
Clerk

Soma Seal, PGT(C.Sc.) KV CISF RTC(A) Thakkolam Page 35

Das könnte Ihnen auch gefallen