Beruflich Dokumente
Kultur Dokumente
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)
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
C++ Programming
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();
}
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();
}
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);
}
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
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”;
}
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();
}
}
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();
}
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();
}
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();
}
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();
}
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;
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";
}
}
}
#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);
}
};
}
break;
case 6 : break;
}
}
file.close();
}
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);
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;
}
sl->pop();
cout<<"new stack\n";
sl->print();
return 0;
}
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;
}
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();
};
}
else
{
cout<<"Queue is empty"<<endl;
}
getch();
}
cin>>arr[next];
rear=next;
}
}
}
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;
}
Source Code –
#include<iostream.h>
#include<conio.h>
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];
}
found=lsearch(Arr,n,val);
if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";
getch();
return 0;
}
Source Code –
#include<iostream.h>
#include<conio.h>
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];
}
found=bsearch(AR,n,val);
if(found==1)
cout<<"\nItem found";
else
cout<<"\nItem not found";
getch();
return 0;
}
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;
}
Source Code –
#include<iostream.h>
#include<conio.h>
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";
}
return 0;
}
Reducing:
Octet = M0.M2.M4.M6.M8.M10.M12.M14
=S
Quad = M4.M5.M6.M7
= P+Q’
Pair = M10.M11
= P’+Q+R’
S.(P+Q’).(P’+Q+R’)
S
P
Q
R
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;
(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;