Академический Документы
Профессиональный Документы
Культура Документы
86 105 35
38 17 27
95 128 36
Q . 6. Write a C++ program to calculate the area of break;
circle ,rectangle or triangle depending upon the case 3 : cout<<”\n Enter the base and
user’s choice using structures . height of right angled triangle :”;
Soln : cin>>tri.base>>tri.height;
tri.area = tri.base* tri.height/2.0;
#include<conio.h> cout<<”\n Area of triangle is : ”
#include<iostream.h> << tri.area << “sq. units\n”;
#include<math.h> break;
struct circle }
{ getch( );
float radius ; }
float area;
}circ; OUTPUT :
1. Calculate the area of circle
struct rectangle 2. Calculate the area of rectangle
{ 3. Calculate the area of right angled triangle
float length; Enter the choice :1
float breadth; Enter the radius :5
float area; Area of circle is :78.5 sq. units
}rect;
Q. 7. Write a C++ program to evaluate students
struct triangle performance using Structure :
{ struct student { int roll_no , marks[5] ;
float base; char name[30];
float height ; };
float area;
}tri; Soln :
void main( )
{ void array : :Ssort( )
array a; { int tmp;
clrscr( ); for( int i = 0 ; i <= count – 2 ; i + +)
a.add(25); {
a.add(17); for( int j = i +1 ; j <= count – 1 ; j + +)
a.add(31); {
a.add(13); if(arr[i]>arr[j])
a.add(2); {
cout<<”\n Bubble sort.\n ”; tmp = arr[i];
cout<<”\n Array before sorting :”<<endl; arr[i] = arr[j];
a.display( ); arr[j] = tmp;
a.sort( ); }
cout<<”\n Array After sorting :”<<endl; }
a.display( ); }
getch( ); }
} void array: :display( )
OUTPUT: {
Bubble sort. for( int i = 0 ; i < count ; i + +)
Array before sorting : cout<<arr[i]<<”\t”;
25 17 31 13 2 cout<<endl;
Array before sorting : }
2 13 17 25 31
void main( )
Q.20. Write a C++ program for sorting an array {
using selection sort . array a;
Soln. clrscr( );
#include<conio.h> a.add(25);
#include<iostream.h> a.add(17);
const int MAX = 10; a.add(31);
class array a.add(13);
{ a.add(2);
private : cout<<”\n Selection sort.\n ”;
int arr[max] , count; cout<<”\n Array before sorting :”<<endl;
public : a.display( );
array( ); a.sort( );
void add( int item); cout<<”\n Array After sorting :”<<endl;
void Ssort( ); a.display( );
void display( ); getch( );
}; }
array : :array( ) OUTPUT:
{ Selection sort.
count = 0; Array before sorting :
for( int i = 0 ; i < MAX ; i + +) 25 17 31 13 2
arr[i] = 0; Array before sorting :
} 2 13 17 25 31
void array : : add(int item)
{ Q.21. Write a C++ program for sorting an array
if(count < MAX) using insertion sort .
{ Soln.
arr[count] = item ; #include<conio.h>
count + +; #include<iostream.h>
} const int MAX = 10;
else class array
cout<<”\n Array is full ”<<endl; {
} private :
int arr[max] , count; a.add(2);
public : cout<<”\n Insertion sort.\n ”;
array( ); cout<<”\n Array before sorting :”<<endl;
void add( int item); a.display( );
void Isort( ); a.sort( );
void display( ); cout<<”\n Array After sorting :”<<endl;
}; a.display( );
array : :array( ) getch( );
{ }
count = 0; OUTPUT:
for( int i = 0 ; i < MAX ; i + +) Insertion sort.
arr[i] = 0; Array before sorting :
} 25 17 31 13 2
void array : : add(int item) Array before sorting :
{ 2 13 17 25 31
if(count < MAX)
{ Q.22. Write a program to perform the following
arr[count] = item ; operations on arrays :
count + +; 1) insertion 2) deletion 3) reverse 4)traversal
} Soln.
else #include<conio.h>
cout<<”\n Array is full ”<<endl; #include<iostream.h>
} const int MAX = 5;
void array : :Isort( ) class array
{ int tmp; {
for( int i = 1 ; i <= count – 1 ; i + +) private :
{ int arr[MAX];
for( int j = 0 ; j < i ; j + +) public :
{ void insert(int pos , int num);
if(arr[j]>arr[i]) void del(int pos);
{ void reverse( );
tmp = arr[j]; void display( );
arr[j] = arr[i]; };
for( int k = i ; k < j ; k- -) void array : :insert( int pos , int num)
arr[k] = arr[k-1]; {
arr[k+1] = tmp; //shift elements to right
} for( int j = MAX – 1 ; j > = pos ; j- - )
} arr[j] = arr[j-1];
} arr[j] = num;
} }
void array: :display( ) void array : :del(int pos)
{ {
for( int i = 0 ; i < count ; i + +) //skip to the desired position
cout<<arr[i]<<”\t”; for(int j = pos ; j < MAX ; j + +)
cout<<endl; arr[j – 1] = arr[j];
} arr[j -1] = 0;
}
void main( ) void array : :reverse( )
{ {
array a; //reverses the entire array
clrscr( ); for(int j = 0 ; j < MAX/2 ; j + +)
a.add(25); {
a.add(17); int tmp = arr[j];
a.add(31); arr[j] = arr[MAX – 1 – j];
a.add(13); arr[MAX – 1 – j] = tmp;
} {
void array : :display( ) size = sz;
{ arr = new int[size];
for(int j = 0 ; j < MAX ; j + +) int n;
cout<<” “<<arr[j]; for( j = 0 ; j<size ; j + + )
} {
void main( ) cout<<”\nEnter the element no. ” << (j+1) <<” ”;
{ cin>>n;
array a ; arr[i] = n;
clrscr( ); }
a.insert(1,11); }
a.insert(2,12); void array : :sort( )
a.insert(3,13); {
a.insert(4,14); int tmp;
a.insert(5,15); for( j = 0 ; j<size ; j + + )
{
cout<<”\n Elements of Array: ”; for( k = j +1 ; k<size ; k + + )
a.display( ); {
if(arr[j] > arr[k])
a.del(5); {
a.del(2); tmp = arr[j];
cout<<”\n\n After Deletion :”; arr[j] = arr[k];
a.display( ); arr[k] = tmp;
}
a.insert(2,222); }
a.insert(5,555); }
cout<<”\n After insertion: ”; }
a.display( ); void array : :display( )
{
a.reverse( ); for( j = 0 ; j<size ; j + + )
cout<<”\n After reversing: ”; cout<<” ”<<arr[j];
a.display( ); }
void array : :merge(array &a , array &b)
getch( ); {
} int i , j , k ;
size = a.size + b.size ;
Q.23. Write a C++ program for merging of arr = new int[size];
two arrays . for(k=0 , j = 0 , i = 0 ; i<=size ; i + + )
Soln. {
#include<conio.h> if(a.arr[k]<b.arr[j])
#include<iostream.h> {
const int MAX1 = 5; arr[i] = a.arr[k];
const int MAX2 = 7; k++;
class array if(k>=a.size)
{ {
private : for(i++ ;j<b.size;j++,i++)
int *arr; arr[i] = b.arr[i];
int size; }
public : }
void create(int sz); else
void sort( ); {
void display( ); arr[i] = b.arr[j];
void merge(array &a , array &b); j++;
}; if(j>=b.size)
void array : : create(int sz) {
for(i++ ; k<=a.size ; k++ , i++) #include<conio.h>
#include<iostream.h>
arr[i] = a.arr[k]; const int MAX = 10;
} class stack
} {
} private :
} int arr[MAX] , top;
void main( ) public :
{ stack( );
clrscr( ); void push( );
array a; int pop( );
cout<<”\nEnter elements of first array :\n”; };
a.create(MAX1); stack : :stack( )
{
array b; top = -1;
cout<<”\nEnter elements of second array : \ n ”; }
b.create(MAX2); void stack: :push( int item)
{
a.sort( ); if(top= =MAX-1)
b.sort( ); {
cout<<”\n First array :\n”; cout<<endl<<”Stack is full”;
a.display( ); return ;
cout<<”\n Second array :\n”; }
b.display( ); top + +;
cout<<”\n\nAfter Merging :\n”; arr[top] = item;
array c; }
c.merge(a , b); int stack : :pop( )
c.display( ); {
getch( ); if(top= = -1)
} {
OUTPUT: cout<<endl<<”Stack is empty”;
Enter elements of first array: return NULL;
Enter element no. 1 67 }
Enter element no. 2 12 int data = arr[top];
Enter element no. 3 -4 top -- ;
Enter element no. 4 43 return data;
Enter element no. 5 2 }
Enter elements of second array: void main( )
Enter element no. 1 8 {
Enter element no. 2 10 stack s;
Enter element no. 3 -2 clrscr( );
Enter element no. 4 39 s.push(11);
Enter element no. 5 6 s.push(23);
Enter element no. 6 7 s.push(-8);
Enter element no. 7 19 s.push(16);
First array : s.push(27);
-4 2 12 43 67 s.push(14);
Second array : s.push(20);
-2 6 7 8 10 19 39 s.push(39);
After Merging : s.push(2);
-4 -2 2 6 7 8 10 12 19 39 43 67 s.push(15);
s.push(7);
Q.24. Write a C++ program to perform push and
pop operations in stack implemented using an array. int i = s.pop( );
Soln. cout<<”\n Item poped :”<<i ;
tmp -> data = item;
int i = s.pop( ); tmp -> link = top;
cout<<”\n Item poped :”<<i ; top = tmp;
}
int i = s.pop( ); int stack : :pop( )
cout<<”\n Item poped :”<<i ; {
if(top = = NULL)
int i = s.pop( ); {
cout<<”\n Item poped :”<<i ; cout<<endl<<”Stack is empty”;
return NULL;
int i = s.pop( ); }
cout<<”\n Item poped :”<<i ; node * tmp;
int item;
getch( ); tmp = top;
} item = tmp ->data;
OUTPUT: top = top - >link;
Stack is full delete tmp;
return item;
Item popped : 15 }
Item popped : 2 stack : : ~stack( )
Item popped : 39 {
Item popped : 20 if(top = =NULL)
Item popped : 14 return;
node *tmp;
Q.25. Write a C++ program to perform push and while(top!=NULL)
pop operations in stack implemented using Linked {
list . tmp = top;
Soln. top = top - >link;
#include<conio.h> delete tmp;
#include<iostream.h> }
class stack }
{ void main( )
private : {
struct node clrscr( );
{ stack s;
int data; s.push(14);
node * link; s.push(-3);
}*top; s.push(18);
public : s.push(29);
stack( ); s.push(31);
void push(int item); s.push(16);
int pop( );
~stack( ); int r = s.pop( );
}; cout<<”\n Item popped : “<<r;
stack : : stack( ) int r = s.pop( );
{ cout<<”\n Item popped : “<<r;
top = NULL; int r = s.pop( );
} cout<<”\n Item popped : “<<r;
void stack : :push(int item) getch( );
{ }
node *tmp; OUTPUT :
tmp = new node; Item popped :16
if(tmp = = NULL) Item popped :31
cout<<endl<<”Stack is full”; Item popped :29
Q.26. Write a C++ program to perform insertion a.addq(23);
and deletion operations in queue implemented using a.addq(9);
an array. a.addq(11);
Soln. a.addq(-10);
#include<conio.h> a.addq(25);
#include<iostream.h> a.addq(16);
const int MAX = 10; a.addq(17);
class queue a.addq(22);
{ a.addq(19);
private : a.addq(30);
int arr[MAX]; a.addq(32);
int front , rear; int r = a.delq( );
public : cout<<”\n Item deleted : ”<<r;
queue( ); int r = a.delq( );
void addq( int item); cout<<”\n Item deleted : ”<<r;
int delq( ); int r = a.delq( );
}; cout<<”\n Item deleted : ”<<r;
queue : :queue( ) getch( );
{ }
front = -1; OUTPUT:
rear = -1; Queue is full
} Item deleted : 23
void queue : : addq(int item) Item deleted : 9
{ Item deleted : 11
if(rear = =MAX – 1) Q.27. Write a C++ program to perform push and
{ pop operations in stack implemented using Linked
cout<<”\n Queue is full”; list .
return; Soln.
} #include<conio.h>
rear + +; #include<iostream.h>
arr[rear] = item; class queue
if(front = = -1) {
front = 0; private :
} struct node
int queue : : delq( ) {
{ int data;
int data; node * link;
if(front = =-1) }*front , *rear;
{ public :
cout<<”\n Queue is Empty”; queue( );
return Null; void addq(int item);
} int delq( );
data = arr[front]; ~queue( );
arr[front] = 0; };
if(front = =rear) queue : : queue( )
front = rear = -1; {
else front = rear =NULL;
front + +; }
void queue : :addq(int item)
return data; {
} node *tmp;
void main( ) tmp = new node;
{ if(tmp = = NULL)
clrscr( ); cout<<endl<<”Queue is full”;
queue a;
tmp -> data = item;
tmp -> link = NULL; getch( );
if(front = = NULL) }
{ OUTPUT :
rear = front = tmp; Item extracted :11
return; Item extracted :-8
} Item extracted :23
rear ->link = tmp;
rear = rear ->link; Q.28. Write a C++ program to perform insertion
} and deletion operations in a circular queue .
int queue : :delq( ) Soln.
{ #include<conio.h>
if(front = = NULL) #include<iostream.h>
{ const int MAX = 10;
cout<<endl<<”Queue is empty”; class queue
return NULL; {
} private :
node * tmp; int arr[MAX];
int item; int front , rear;
item = front ->data; public :
tmp = front; queue( );
front = front - >link; void addq( int item);
delete tmp; int delq( );
return item; void display( );
} };
queue : : ~queue( ) queue : :queue( )
{ {
if(front = =NULL) front = -1;
return; rear = -1;
node *tmp; for( int j = 0; j < MAX ; j+ +)
while(top!=NULL) arr[j]= 0;
{ }
tmp = front; void queue : : addq(int item)
front = front - >link; {
delete tmp; if(rear = =MAX – 1 && front= =0) ||
} (rear + 1 = = front) )
} {
void main( ) cout<<”\n Queue is full”;
{ return;
clrscr( ); }
queue a; if (rear = = MAX-1)
a.addq(11); rear = 0;
a.addq(-8); else
a.addq(23); rear + +;
a.addq(19);
a.addq(15); arr[rear] = item;
a.addq(16); if(front = = -1)
a.addq(28); front = 0;
}
int r = a.delq( ); int queue : : delq( )
cout<<”\n Item extracted : “<<r; {
int r = a.delq( ); int data;
cout<<”\n Item extracted : “<<r; if(front = =-1)
int r = a.delq( ); {
cout<<”\n Item extracted : “<<r; cout<<”\n Queue is Empty”;
return NULL; a.display( );
} getch( );
data = arr[front]; }
arr[front] = 0; OUTPUT:
if(front = =rear) Elements in the circular queue
front = rear = -1; 14 22 13 -6 25 0 0 0 0 0
else Item deleted : 14
{ Item deleted : 22
if(front= =MAX-1) Elements in the circular queue after deletion :
front = 0; 0 0 13 -6 25 0 0 0 0 0
else Elements in the circular queue after addition :
front + +; 0 0 13 -6 25 21 17 18 9 20
} Elements in the circular queue after addition :
return data; 32 0 13 -6 25 21 17 18 9 20
}
void queue : :display( )
{
cout<<endl; Q.29. Write a C++ program to read and write class
for( int j = 0; j < MAX ; j+ +) object in a binary file ?
cout<< arr[j]<<” ”; Soln.
cout<<endl; #include<fstream.h>
} #include<stdio.h>
void main( ) #include<process.h>
{ #include<conio.h>
clrscr( ); class joke
queue a; { private :
a.addq(14); int jokeid;
a.addq(22); char type[5];
a.addq(13); char jokedesc[255];
a.addq(-6); public :
a.addq(25); void newjokeentry( )
cout<<”\n Elements in the circular queue :”; { cout<<”\n Enter joke id :” ;
a.display( ); cin>>jokeid;
int r = a.delq( ); cout<<”\n Enter joke type : ”;
cout<<”\n Item deleted : ”<<r; gets(type);
cout<<”\n Enter joke : ”;
int r = a.delq( ); gets(jokedesc);
cout<<”\n Item deleted : ”<<r; }
cout<<”\n Elements in the circular queue void showjoke( )
after deletion :”; { cout<<”\n joke id :” <<jokeid;
a.display( ); cout<<”\n joke type : ”;
puts(type);
a.addq(21); cout<<”\n joke details : ”;
a.addq(17); puts(jokedesc);
a.addq(18); }
a.addq(9); };
a.addq(20); void main( )
cout<<”\n Elements in the circular queue {
after addition:”; clrscr( );
a.display( ); void addjoke( );//function prototype
void dispfile( ); //function prototype
a.addq(32); clrscr( );
addjoke( );//function call
cout<<”\n Elements in the circular queue dispfile( );//function call
after addition:”; getch( );
} What a wonder fool fellow
void addjoke( ) Do you wish to add more object(y/n) ? :
{ Y
joke obj; Enter joke id
char choice; 2
ofstream ofile; Enter type
ofile.open(“jokes.dat”,ios: :app| ios: :binary); Sad
if(!ofile) Ramlal married Miss world and lives a HAPPY
{ DAILY life
cout<<”\n cannot open jokes.dat for writing\n”; Do you wish to add more object(y/n) ? :
exit(1); n
} Contents of file jokes.dat are :
while(1) 1: Fun
{ What a wonder fool fellow
{ 2 : Sad
clrscr( ); Ramlal married Miss world and lives a HAPPY
cout<<\n Do you wish to add more DAILY life
objects(y/n) ?\n\n”;
cin>>choice;
if( ( choice = =’y’)| | (choice = = ‘Y’))
{
obj.newjokeentry( );
ofile.write((char *)&obj , sizeof(obj);
}
else
break;
}
ofile.close( );
}
void dispfile( )
{
joke obj;
ifstream ifile;
ifile.open(“jokes.dat”,ios: :app| ios: :binary);
if(!ifile)
{
cout<<”\n cannot open jokes.dat for reading\n”;
exit(1);
}
cout<<”\n contents of file jokes.dat are :\n\n”;
ifile.read((char*)&obj,sizeof(obj);
while( !ifile.eof( ))
{
obj.showjoke( );
ifile.read((char*)&obj,sizeof(obj);
}
ifile.close( );
}
OUTPUT:
Do you wish to add more object(y/n) ? :
Y
Enter joke id
1
Enter type
Fun