You are on page 1of 21

Q.

C++ Program to Find Minimum Element in an Array


using Templates
#include<conio.h>
#include<iostream.h>
template<class min>
void minelement(min e[10], int n)
{
int i, j;
min small = e[0];
for(i=0;i<n;i++)
{
if(small>e[i])
{
small = e[i];
}
}
cout<<"\nSmallest Element:"<<small;
}
void main()
{
int n=5;
int a[5] = {3,2,4,1,5};
float b[5] = {1.2,1.9,2.3,1.4,1.5};
char c[5] = {'k','b','z','d','e'};
clrscr();
minelement(a,n);
minelement(b,n);
minelement(c,n);
getch();
}

Q. C++ Program to Implement Queue using Linked List.


/*
* C++ Program to Implement Queue using Linked List
*/
#include<iostream>

#include<stdio.h>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;
void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}
int remove()
{
int x;
if(front == NULL)
{
cout<<"empty queue\n";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}
int main()
{
int n,c = 0,x;
cout<<"Enter the number of values to be pushed into queue\n";
cin>>n;

while (c < n)
{
cout<<"Enter the value to be entered into queue\n";
cin>>x;
push(x);
c++;
}
cout<<"\n\nRemoved Values\n\n";
while(true)
{
if (front != NULL)
cout<<remove()<<endl;
else
break;
}
getch();
}
Output
Enter the number of values to be pushed into queue
6
Enter the value to be entered into queue
5
Enter the value to be entered into queue
4
Enter the value to be entered into queue
3
Enter the value to be entered into queue
2
Enter the value to be entered into queue
1
Enter the value to be entered into queue
0

Removed Values
5
4
3
2
1
0

Q. overloaded '==' operator to compares two strings.


#include <iostream>
using namespace std;
#include <string.h>
class String{
private:
enum { SZ = 80 };
char str[SZ];
public:
String(){ strcpy(str, ""); }
String( char s[] ){ strcpy(str, s); }
void display() const{ cout << str; }
void getstr(){ cin.get(str, SZ); }
bool operator == (String ss) const{
return ( strcmp(str, ss.str)==0 ) ? true : false;
}
};
int main(){
String s1 = "yes";
String s2 = "no";
String s3;
cout << "\nEnter 'yes' or 'no': ";
s3.getstr();
if(s3==s1)
cout << "You typed yes\n";
else if(s3==s2)
cout << "You typed no\n";
else
cout << "You didn't follow instructions\n";
return 0;

Q. Describe various file pointers manipulations in C++.


The C++ input and output system manages two integer values associates with a
file.
These are:
get pointer specifies the location in a file where the next read operation will
occur.
put pointer specifies the location in a file where the next write operation will
occur.
In other words, these pointers indicate the current positions for read and write
operations, respectively. Each time an input or an output operation takes place,
the pointers are automatically advances sequentially.
The term pointers should not be confused with normal C++ pointers used as
address variables.
Often you may want to start reading an existing file from the beginning and
continue sequentially until the end. When writing, you ma want to start from the
beginning, deleting any existing contents, or appending new records (in which
case you can open the file with the ios:app mode specifier). These are default
actions, so no manipulation of the pointers is necessary.
Sometimes you may have to manipulate file pointers to read from and write to a
particular location in a file. The seekg() and tellg() functions allow you to set
and examine the get pointer, and the seekp() and tellp() functions perform these
same actions on the put pointer. In other words, these four functions allow you
to access the file in a non-sequential or random mode.
All the iostream clas objects can be repositioned by using either the seekg() or
the seekp() member function. These functions move the get and put pointers
respectively to an absolute address within the file or toa certain number of bytes
from a particular position.
The tellg() and tellp() functions can be used to find out the current position of
the get and put file pointers respectively in a file.

Example:
Finds out the number of records in the file billfile.dat by using the seekg() and
tellg() functions.

#include<fstream.h>
#include<iostream.h>
class bill
{
private:
intiBill_no;
floatfBill_amt;
public:
void getdata()
{
cout<<Enter Bill number;
cin>>iBill_no;
cout<<Enter Bill amount;
cin>>fBill_amt;
}
void showdata()
{
cout<<Bill number <<iBill_no<<endl;
cout<<Bill amount <<fBill_amt<<endl;
}
};
void main()
{
fstream Fi1(billfile.dat,ios::in);
Fi1.seekg(0,ios::end);
ini iEnd;
iEnd=Fi1.tellg();
cout<<The size of the file is <<iEnd<<endl;
cout<<Size of one record is <<sizeof(bill)<<endl
ini iNorec=iEnd/sizeof(bill);
cout<<There are <<iNored<<records in the file<<endl;
}
Q. IMPLEMENTATION OF STACKS

This C++ program implements the following stack operations.

Push

Pop

Top

Empty

STEPS:
Create an array to store the stack elements.
Get the size of the stack.
To push an element into the stack check if the top element is less than the size and increment
the top.
Else print overflow.
To pop an element, check if the stack is empty and decrement the stack.
If all the elements are popped, print underflow.
Find the topmost element in the stack by checking if the size is equal to top.
If the stack is empty print empty, else print not empty.
CODING:
#include<iostream.h>
#include<conio.h>
int max=7;
int t=0;
class stack
{
int s[7];
public:
void push(int);
void pop();
void top();
void empty();
void show();

};
void stack::push(int y) //Push Operation
{
if(t<max)
{
t=t+1;
s[t]=y;
}
else
cout<<endl<<"stack overflows..."<<endl;
}
void stack::pop() //Pop Operation
{
int item;
if(t>=0)
{
t=t-1;
item=s[t+1];
cout<<endl<<"popped item >>"<<item<<endl;
}
else
cout<<endl<<"stack underflows"<<endl;
}
void stack::top() //To find the top of the stack
{
if(t>=0)
cout<<endl<<"topmost element >> "<<s[t]<<endl;
else
cout<<endl<<"stack underflows..."<<endl;
}

void stack::empty() //To check if the stack is empty


{
if(t<0)
cout<<endl<<"stack is empty..."<<endl;
else
cout<<endl<<"stack is not empty..."<<endl;
}
void main()
{
int a,x;
stack s1;
clrscr();
do
{
cout<<"enter an option..."<<endl<<"1-push"<<endl<<"2-pop"<<endl<<"3top"<<endl<<"4-empty"<<endl;
cout<<"5-end"<<endl;
cin>>a;
cout<<endl;
switch(a)
{
case 1:
{
cout<<endl<<"enter a value >> "<<endl;
cin>>x;
s1.push(x);
}
break;
case 2:
s1.pop();
break;
case 3:
s1.top();

break;
case 4:
s1.empty();
break;
}
}
while(a!=5);
getch();
}

Q. Write a class to represent a vector in C++.


#include<iostream.h>
#include<conio.h>
class vec
{
int arr[10];
public :
vec() // default constructor
{
for(int i = 0 ; i < 10 ; i++)
arr[i] = 0;
}
vec(int a[]) // parameterised constructor
{
for(int i=0; i < 10 ; i++ )
{
arr[i] = a[i];
}
}
~vec() // destructor
{

delete arr;
}
void crea()
{
for(int i=0; i < 10 ; i++ )
{
cout << "\n Enter an element : ";
cin >> arr[i];
}
}
void modi(int pos, int ele)
{
arr[pos] = ele;
}
void disp()
{
cout<< "\n The elements of the array --> ";
for(int i = 0; i<10; i ++)
{
cout<< arr[i] << " ";
}
}
int disp_lar()
{
int lar = arr[0];
for(int i =1; i < 10 ; i++)
{
if ( lar < arr[i] )
{ lar = arr[i];

}
}
return lar;
}
}; // end of class
void main()
{
vec v1; // object v1 is created using default cons
v1.crea();
v1.disp();
int max = v1.disp_lar();
cout<< "\n The max element in the array is " << max;
cout<<"\n\n Enter 1 for modifying the array element :";
cout<<"\n Enter 2 for not modifying the array :";
int ch, ps, el;
cout<< "\n Enter your choice :";
while(ch == 1)
{
cout<< "\n Enter the position to be changed";
cin>>ps;
cout<< "\n Enter the element to be entered ";
cin >> el;
v1.modi(ps, el);
cout<< "\n Enter your choice :(1 or 2 )";
cin >> ch;
}
cout<< "\n The elements after modification ";
v1.disp();
cout<< "\n The Max element is ";

max = v1.dis_lar();
cout << max ;
getch();
}

Q. Fibonacci series Example Program In C++


#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
// Variable Declaration
int counter, n;
long last=1,next=0,sum;
// Get Input Value
cout<<"Enter the Number :";
cin>>n;
//Fibonacci Series Calculation
while(next<n/2)
{
cout<<last <<" ";
sum=next+last;
next=last;
last=sum;
}
// Wait For Output Screen
getch();
return 0;
}

Q. Implement Queue using C++.


# include<iostream.h>
# include<conio.h>
# define SIZE 20

class queue
{
int a[SIZE];
int front;
int rear;
public:
queue();
~queue();
void insert(int i);
int remove();
int isempty();
int isfull();
};
queue::queue()
{
front=0;
rear=0;
}
queue::~queue()
{
delete []a;
}
void queue::insert(int i)
{
if(isfull())
{
cout<<"

******

Queue is FULL !!!


No insertion allowed further.
******
";
return;
}
a[rear] = i;
rear++;
}
int queue::remove()
{
if(isempty())
{
cout<<"
******
Queue Empty !!!
Value returned will be garbage.
******
";
return (-9999);
}

return(a[front++]);
}
int queue::isempty()
{
if(front == rear)
return 1;
else

return 0;
}
int queue::isfull()
{
if(rear == SIZE)
return 1;
else
return 0;
}
void main()
{
clrscr();
queue q;
q.insert(1);
q.insert(2);
cout<<"
"<<q.remove();
cout<<"
"<<q.remove();
cout<<"
"<<q.remove();
getch();
}

C++ Operator Overloading


Operator overloading is one of the advanced concepts of C++. It is a feature through which
most of the standard operators can be used with class objects.

#include <iostream>
class example

{
public:
int a;
int b;
example operator+(const example& obj);
void operator=(const example& obj);
};
void example::operator=(const example& obj)
{
(*this).a = obj.a;
(*this).b = obj.b;
return;
}
example example::operator+(const example& obj2)
{
example tmp_obj = *this;
tmp_obj.a = tmp_obj.a + obj2.a;
tmp_obj.b = tmp_obj.b + obj2.b;
return tmp_obj;
}
int main(void)
{
example obj1, obj2, obj3;
obj1.a = 1;
obj1.b = 1;

obj2.a = 2;
obj2.b = 2;
obj3.a = 0;
obj3.b = 0;
obj3 = obj1 + obj2;
std::cout<<obj3.a<<" "<<obj3.b<<"\n";
return 0;
}

Q. C++ Program Code for Selection Sort (Ascending Order) of array


elements, with template class
#include<iostream>
#include<conio.h>
using namespace std;
template <class T>
void s_sort(T a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}

int main()
{
int a[100],i,n;
cout<<"Enter The number of Element:\n";
cin>>n;
cout<<"\nEnter Elements:\n";
for(i=0;i<n;i++)
{
cout<<"\nEnter:";
cin>>a[i];
}
s_sort(a,n);
cout<<"\nAfter Sorting\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
getch();
return 0;
}

Q. C++ Program to Implement Bubble Sort using


Templates
#include<conio.h>
#include<iostream.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)

{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
bubble element;
element = a[i];
a[i] = a[j];
a[j] = element;
}
}
}
}
void main()
{
int a[6]={1,2,3,4,4,3};
char b[4]={'s','b','d','e'};
clrscr();
bubble(a,6);
cout<<"\nSorted Order Integers: ";

for(int i=0;i<6;i++)
cout<<a[i]<<"\t";
bubble(b,4);
cout<<"\nSorted Order Characters: ";
for(int j=0;j<4;j++)
cout<<b[j]<<"\t";
getch();
}