Você está na página 1de 53

Program no.

1
Program to show concept of polymorphism to calculate
the area of circle, triangle, square.
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
float area(float a, float b, float c)
{ float s,ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar; }
float area(float a, float b);
{ return a*b; }
float area(float a);
{ return a*a; }
int main()
{ system(“ cls “);
int choice,s1,s2,s3,ar;
do
{ cout<<”\nArea Main Menu\n”;
cout<<”1. Triangle\n”;
cout<<”2. Square\n”;
cout<<”3. Rectangle\n”;
cout<<”4. Exit \n”;
cout<<” Enter your choice(1-4):”;
cin>>choice;
cout<<”\n”;
switch(choice) {
case 1 : cout<<” Enter 3 sides\n”;
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3);

cout<<” The area is” << ar << “\n”;


break;
case 2 :cout<<” Enter a side \n”;
cin>>s1;
ar=area(s1);
cout<<” The area is”<<ar<<”\n”;
break;
case 3 :cout<<” Enter length & breath \n”;
cin>>s1>>s2;
ar=area(s1,s2);
cout<<” The area is”<< ar<<”\n”;
break;
case 4 : break;
default :cout<<” Wrong Choice!!\n”; }; //end of switch
} while(choice > 0 && choice < 4);
return 0;
}
Program no.2
Program to show a function with default arguments.
#include<iostream.h>
#include<conio.h>
void display (char = '*', int = 1);
int main()
{ clrscr();
cout<<" No arguments passed:\n";
display();
cout<<"\n First argument passed: \n";
display('#');
cout<< "\n Both argument passed: \n";
display('$',5);
return 0; }
void display(char c, int n)
{ for(int i=1; i<=n; ++i)
{ cout<<c; }
cout<<endl;
getch();
}
Program no.3
Program to invoke a function that takes array as an
argument and reverse the elements of the array.
#include<iostream.h>
#include<conio.h>
void reversearray(int arr[], int start,int end)
{
while (start<end)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
void printarray (int arr[], int size)
{
for (int i=0;i<size;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void main()
{
int arr[]={1,2,3,4,5,6};
clrscr();
printarray(arr,6);
reversearray(arr,0,5);
cout<<"Reversed array is" << endl;
printarray(arr,6);
getch();}

Program no.4
Program to merge two arrays in ascending order of array1
is in descending and array2 is in ascending order using
function.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int arr1[50],arr2[50],size1,size2,size,i,j,k,merge[100];
cout<<"Enter array 1 size :";
cin>>size1;
cout<<"Enter array 1 elements : ";
for(i=0;i<size1;i++)
{ cin>>arr1[i]; }
cout<<"Enter array 2 size :";
cin>>size2;
cout<<"Enter array 2 elements :";
for(i=0;i<size2;i++)
{ cin>>arr2[i]; }
for(i=0;i<size1;i++)
{ merge[i]=arr1[i]; }
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++,k++)
{ merge[k]=arr2[i]; }
cout<<"now the new array after merging is :\n";
for(i=0;i<size;i++)
{ cout<<merge[i]<<" "; }
getch();
}

Program no.5
Program to display row sum and column sum of a 2D array
passed as an argument.
#include<conio.h>
#include<iostream.h>

int main()
{
clrscr();
int x[2][3]={10,9,8,8,3,4};
int i,j,sum;
// Processing
sum=0;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
sum=sum +x[i][j];
// output
cout<<"\n Sum of elements of matrix :"<<sum;
cout<<"\n The matrix is:";
for(i=0;i<2;i++)
// output
cout<<"\n Sum of elements of matrix :"<<sum;
cout<<"\n The matrix is:";
for(i=0;i<2;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<" \t"<<x[i][j];
}
getch();
return 0;
}

Program no.6
Define a class STOCK in C++ with following descriptions:
Private members:
Icode int type (item code)
Item string type (item name)
Price float type (price of each item)
Qty int type(quantity in stock)
Discount float type(discount% on item)
FindDisc() to calculate discount as per following rule:
If Qty<=50 Discount =0
If 50<Qty<=100 Discount =5
If Qty>100 Discount=10
Public members:
Buy() – allows user to enter values for Icode, Item, Price,
Qty and call function FindDisc() to calculate the discount.
ShowAll() – allows user to view the content of all data
members.

class STOCK {
int ICode;
char Item[20];
float Price;
int Qty;
float Discount;
void FindDisc() {
if(Qty<=50)
Discount=0;
else if(Qty<=100)
Discount=5;
else
Discount=10; }
public:
void Buy()
{ cout<<"Enter item code:";
cin>>ICode;
cout<<"Enter item name:";
gets(Item);
cout<<"Enter price of each item:";
cin>>Price;
cout<<"Enter quantity of item in stock:";
cin>>Qty;
FindDisc(); }
void ShowAll() {
cout<<"Code:"<<ICode<<"\nName:"<<Item<<"\nPrice:"<<Price<<"\nQuantity:
"<<Qty<<"\nDiscount percentage:"<<Discount; } };

Program no.7
Define a class TRAVELPLAN in C++
Private members:
Plancode long type
Place string type
No_of_traveller int type
No_of_bus int type
Public members:
--Constructor to assign initial values of Plancode as 1001,
Place as “Agra”, No_of_traveller as 5, No_of_bus as 1.
--NewPlan() allows user to enter Plancode, Place,
No_of_traveller. Also assigns number of bus as
Number of traveller Number of bus
<20 1
>=20 & <40 2
>=40 3
-- ShowPlan() to display content of all data members on
screen.
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
class TRAVELPLAN {
long plancode;
char place[20];
int no_of_traveller, no_of_bus;
int buscalc();
public:
TRAVELPLAN()
{ plancode=1001;
strcpy (place,"agra");
no_of_traveller=5, no_of_bus=1; }
void newplan();
void showplan(); };
void TRAVELPLAN::newplan()
{ cout<<"\nEnter plancode:";
cin>>plancode;
cout<<"\nEnter place:";
gets(place);
cout<<"\nEnter no. of travellers:";
cin>>no_of_traveller;
no_of_bus=buscalc(); }
int TRAVELPLAN ::buscalc()
{ if(no_of_traveller<20)
no_of_bus=1;
else if(no_of_traveller>=20 && no_of_traveller<40)
no_of_bus=2;
else if(no_of_traveller>=40);
no_of_bus=3;
return no_of_bus; }
void TRAVELPLAN::showplan()
{ cout<<"\nPlancode:"<<plancode;
cout<<"\nPlace:";
puts(place);
cout<<"\nNo. of travellers:"<<no_of_traveller;
cout<<"\nNo. of bus:"<<no_of_bus; }
void main ()
{ TRAVELPLAN T;
int ch;

clrscr();
do {
cout<<"\nEnter your choice(1-3):";
cout<<"\n1.Enter a new plan \n2.Show plans \n3. Exit\n";
cin>>ch;
switch(ch) {
case 1 : T.newplan(); break;
case 2 : T.showplan(); break;
case 3 : exit(0);
default : cout<<"\nWrong choice";
} } while(ch!=3);
getch();
}

Program no.8
Program to show multilevel inheritance.
#include<conio.h>
#include<iostream.h>
class person
{ protected:
int age;
char *name;
public:
void get1(); };
class emp : public person
{ protected:
int basic,hra;
public:
void get2(); };
class manager : public emp
{ protected:
int deptcode;
public:
void get3();
void display(); };
void person :: get1()
{ cout<<"enter your age\n";
cin>>age;
cout<<"enter your name\n";
cin>>name; }
void emp :: get2()
{ cout<<"enter your basic and HRA\n";
cin>>basic>>hra; }
void manager :: get3()
{ cout<<"enter your deptcode\n";
cin>>deptcode; }
void manager :: display()
{ cout<<"name is "<<name;
cout<<"\nage is "<<age;
cout<<"\nbasic and HRA"<<basic<<"\t"<<hra;
cout<<"\ndeptcode is "<<deptcode; }
void main()
{ clrscr();
manager obj;
obj.get1();
obj.get2();
obj.get3();
obj.display();
getch();}
Program no.9
Write a function to count no. of “he” or “she” words
present in a text file “story.txt”.
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<process.h> // for exit()
int count()
{ ifstream ifile("story.txt");
if(!ifile)
{ cout<<"could not open story.txt file";
exit(-1); }
else
{ char s[20];
int cnt=0;
while(!ifile.eof())
{ ifile>>s;
if((strcmp(s,"He"))==0||(strcmp(s,"She")==0))
cnt++; }
ifile.close();
cout<<"count of he/she in file is :"<<cnt;
return cnt; } }
Program no.10
Program to search in file having records maintain through
class in binary file.
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
class student {
int rollno;
char name[20];
char branch[3];
float marks;
char grade;
public:
void getdata() {
cout<<"rollno: ";
cin>>rollno;
cout<<"name: ";
cin>>name;
cout<<"branch: ";
cin>>branch;
cout<<"marks: ";
cin>>marks;
if(marks>=75)
{ grade='A'; }
else if(marks>=60)
{ grade='B'; }
else if(marks>=50)
{ grade='C'; }
else if(marks>=40)
{ grade='D'; }
else
{ grade='F'; } }

void putdata()
{ cout<<"Rollno: "<<rollno<<"\t name: "<<name<<"\n";
cout<<"marks: "<<marks<<"\tGrade: "<<grade<<"\n"; }
int getrno()
{ return rollno; } } stud1;
void main()
{ clrscr();
fstream fio("marks.dat",ios::in | ios::out);
char ans='y';
while(ans=='y' || ans=='Y')
{ stud1.getdata();
fio.write((char *)&stud1, sizeof(stud1));
cout<<"Record added to the file\n";
cout<<"\nWhat to enter more ? (y/n)..";
cin>>ans; }
clrscr();
int rno;
long pos;
char found='f';
cout<<"enter rollno of student to be searched :";
cin>>rno;
fio.seekg(0);
while(!fio.eof())
{ pos=fio.tellg();
fio.read((char *)&stud1,sizeof(stud1));
if(stud1.getrno()==rno)
{ stud1.putdata();
fio.seekg(pos);
found='t';
break; } }
if(found=='f')

{ cout<<"\nrecord not found in the file..!!\n";


cout<<"press any key to exit...\n";
getch();
exit(2); }
fio.close();
getch();
}
Program no.11
Program to modify data in a given binary file using class.
#include<fstream.h>
#include<stdio.h>
#include<string.h>

#include<conio.h>
#include<process.h>
class file
{ private:
int roll;
float age;
char name[100];
public:
void input();
void show();
char *getn()
{ return name; }
};file fileobj;
void file::input()
{ cout<<"enter the roll no,age and name:";
cin>>roll>>age;
gets(name); }
void file::show()
{ cout<<"Roll==>"<<roll<<endl;
cout<<"Age==>"<<age<<endl;
cout<<"Name==>"<<name<<endl; }
void modify();
fstream fil;
int main()
{ int opt;
while(1)
{ clrscr();
modify();
cout<<"display main menu"<<endl;
getch();
break; }
{ char n[100];
cout<<"enter name that should be searched:";
gets(n);
fil.open("binary.dat",ios::in| ios::out|ios::binary);
if(!fil)
{ cout<<"file not found";
exit(0); }
else
{ filread((char*)&fileobj,sizeof(fileobj));
while(!fil.eof())
{ if(strcmp(n,fileobj.getn())==0)
{ fil.seekg(0,ios::cur);
cout<<"enter new record.."<<endl;
fileobj.input();
fil.seekp(fil.tellg()
sizeof(fileobj));
fil.write((char*)&fileobj,
sizeof(fileobj)); }
else
{ cout<<"press any key....for search"<<endl;
getch(); }
fil.read((char*)&fileobj;
sizeof(fileobj)); } }
fil.close();
}

Program no.12
Program to show binary search.
#include<conio.h>
#include<iostream.h>
int bsearch(int [],int,int);
void main()
{ clrscr();
int ar[50],item,n,index;
cout<<"enter desired array size(max. 50)...";
cin>>n;
cout<<"\n enter array elements(must be sorted in asc order)\n";
for(int i=0;i<n;i++)
cin>>ar[i];
cout<<"\nenter element to be searched for...";
cin>>item;
index=bsearch(ar,n,item);
if(index==-1)
cout<<"\nsorry!! given element could not be found.\n";
else
cout<<"\nelement found at index:"<<index<<",position:"<<index+1<<endl;
getch(); }
int bsearch(int ar[],int size,int item)
{ int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if(item==ar[mid])
return mid;
else if(item>ar[mid])
beg=mid+1;

else
last=mid-1; }
return -1;
}
Program no.13
Program to show deletion in an array.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[100],i,pos,num,item,size;
cout<<"how many elements";
cin>>size;
cout<<"enter the elements of an array";
for(i=0;i<size;i++)
cin>>a[i];
cout<<"now array is"<<endl;
for(i=0;i<size;i++)
cout<<"element at position"<<i+1<<"is"<<a[i]<<endl;
cout<<"enter position number to delete";
cin>>pos;
--pos;
item=a[pos];
for(i=pos;i<size;i++)
a[i]=a[i+1];
size--;
cout<<"deleted element "<<item<<endl;
cout<<"new array is"<<endl;
for(i=0;i<size;i++)
cout<<"element at position "<<i+1<<" is "<<a[i]<<endl;
getch();
}
Program no.14
Program to show bubble sort.
#include<iostream.h>
#include<conio.h>
void bubblesort(int [],int);
void main()
{ clrscr();
int ar[50],item,n,index;
cout<<"how many elements do u want to create array with?(max. 50)..";
cin>>n;
cout<<"\nenter array elements...\n";
for(int i=0;i<n;i++)
{ cin>>ar[i]; }
bubblesort(ar,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();}
void bubblesort(int ar[],int size)
{ int tmp,ctr=0;
for(int i=0;i<size;i++)
{ for(int j=0;j<(size-1)-i;j++)
{ if(ar[j]>ar[j+1])
{ tmp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=tmp; } }
cout<<"Array after iteration-"<<++ctr<<"-is:";
for(int k=0;k<size;k++)
cout<<ar[k]<<" ";
cout<<endl; } }

Program no.15
Program to display transpose of matrix, using function that
takes matrix as an input.
#include<iostream.h>
#include<conio.h>
#define row 3
#define col 4
void main()
{ clrscr();
0int mat1[row][col],mat2[row][col],i,j;
cout<<"enter element of matrix";
cout<<"row-wise"<<endl;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
cin>>mat1[i][j];
for(i=0;i<col;i++)
for(j=0;j<row;j++)
mat2[i][j]=mat1[j][i];
cout<<"transpose of matrix is\n";
for(i=0;i<col;i++)
{ for(j=0;j<row;j++)
{ cout<<mat2[i][j]<<" "; }
cout<<endl; }
getch();
}

Program no.16
Program to swap values of two integer variables by using
passing pointers.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{ clrscr();
int x, y,*a,*b,temp;
cout<<"\nEnter two integers:";
cin>>x>>y;
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
cout<<"\nAfter swapping:";
cout<<x<<"\n"<<y;
getch();
}

Program no.17
Program to PUSH & POP using array stack using switch
case.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define size 4
#include<process.h>
class stack {
int data[size];
int top;
public:
stack()
{ top=-1; }
void push();
void pop();
void display(); };
void stack::push()
{ if(top==size-1)
{ cout<<"\nStack is full!";
return; }
else
{ top++;
cout<<"\nEnter data:" ;
cin>>data[top]; } }
void stack::pop()
{ if(top==-1)
cout<<"\nStack is empty";
else
{ cout<<data[top]<<" deleted"<<endl;
top--; } }
void stack::display()
{ int t=top;
if(t<0)
cout<<"\nNo element in stack";
while(t>=0)
{ cout<<data[t]<<endl;
t--; } }
void main()
{ clrscr();
stack st;
int ch;
do
{ cout<<"\n1. push";
cout<<"\n2. pop";
cout<<"\n3. display";
cout<<"\n4. exit";
cout<<"\nEnte your choice:";
cin>>ch;
switch(ch)
{ case '1' : st.push();
break;
case '2' : st.pop();
break;
case '3' : st.display();
break;
case '4' : exit(0);
default : cout<<"\nWrong input";
} }while(ch!=4);
getch();
}

Program no.18
Program to PUSH & POP from linked stack.
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
struct node
{ int data;
node *next; };
class stack
{ node *top;
public:
stack()
{ top=NULL; }
void push();
void pop();
void display();
~stack(); };
void stack :: push()
{ node *temp=new node;
cout<<"\nEnter data:";
cin>>temp->data;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{ temp->next=top;
top=temp; } }
void stack :: pop()
{ if (top!=NULL)
{ node *temp=top;
top=top->next;
cout<<temp->data<<" deleted";
delete temp; }
else
{ cout<<"\nStack is empty"; } }
void stack :: display()
{ node *temp=top;
if(temp==NULL)
{ cout<<"\nNo element in stack"; }
while(temp!=NULL)
{ cout<<temp->data<<"\n";
temp=temp->next; } }
stack :: ~stack()
{ while(top!=NULL)
{ node *temp=top;
top=top->next;
delete temp; } }
void main()
{ stack st;
char ch;
clrscr();
do
{ cout<<"\nStack option \nP for push \nO for pop \n D for display";
cout<<"\nQ for quit \nEnter your choice:";
cin>>ch;
switch(ch)
{ case 'P' : st.push();
break;
case 'O' : st.pop();
break;
case 'D' : st.display();
break;
case 'Q' : exit(0);
default : cout<<"\nWrong choice"; }
} while(ch!='Q');
getch();
}

Program no.19
Program to insert and delete from array queue.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 4
class queue {
int data[size];
int front, rear;
public :
queue()
{ front=-1;
rear=-1; }
void insert();
void Delete();
void display(); };
void queue::insert()
{ if(rear==size-1)
{ cout<<"\nQueue overflow";
return; }
if(front==-1)
{ front++; }
rear++;
cout<<"\nEnter data:";
cin>>data[rear]; }
void queue::Delete()
{ if(front==-1)
{ cout<<"\nQueue underflow";
return; }
cout<<"\n"<<data[front]<<" is deleted";
if(front==rear)
{ front=-1;
rear=-1; }
else
front++; }
void queue::display()
{ int temp;
if(front==-1)
{ cout<<"\nQueue is empty";
return; }
temp=front;
while(temp<=rear)
{ cout<<data[temp++]<<" "; } }
void main()
{ queue Q;
int ch;
clrscr();
do
{ cout<<"\nEnter your choice(1-4):";
cout<<"\n1. Insert \n2.Delete \n3.Display \n4.Exit";
cin>>ch;
switch(ch)
{ case 1 : Q.insert(); break;
case 2 : Q.Delete(); break;
case 3 : Q.display(); break;
case 4 : exit(0);
default : cout<<"\nWrong choice";
} } while(ch!=4);
getch();
}
Program no.20
Program to insert and delete from linked queue.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int data;
node *next; };
class queue
{ node *rear, *front;
public:
queue()
{ rear=NULL;
front=NULL; }
void qinsert();
void qdisplay();
void qdelete();
~queue(); };
void queue::qinsert()
{ node *temp= new node;
cout<<"\nEnter data:";
cin>>temp->data;
temp->next=NULL;
if (rear==NULL)
{ rear=temp;
front=temp; }
else
{ rear->next=temp;
rear=temp; } }
void queue::qdelete()
{ if(front!=NULL)
{ node *temp=front;
cout<<front->data<<" deleted\n";
front=front->next;
delete temp;
if(front==NULL)
rear=NULL; }
else
cout<<"\nQueue empty"; }
void queue::qdisplay()
{ node*temp=front;
if(front==NULL)
cout<<"\Queue empty";
while(temp!=NULL)
{ cout<<temp->data<<" ";
temp=temp->next; } }
queue::~queue()
{ while(front!=NULL)
{ node *temp=front;
front=front->next;
delete temp; } }
void main()
{ queue Q;
int ch;
clrscr();
do
{ cout<<"\nEnter your choice(1-4):";
cout<<"\n1. Insert \n2. Delete \n3.Display \n4. Exit\n";
cin>>ch;
switch(ch)
{ case 1 : Q.qinsert(); break;
case 2 : Q.qdelete(); break;
case 3 : Q.qdisplay(); break;

case 4 : exit(0);
default : cout<<"\nWrong choice";
} }while(ch!=4);
getch();
}
Program no. 21
Program to show linear search.
#include<conio.h>
#include<iostream.h>

int Linear_Search (int x[],int N,int data)


{
int i, found=0;
for(i=0;i<N;i++)
{
if(x[i]==data)
found=1;
}
return(found);
}
int main()
{
clrscr();
int x[10],data, i;
cout<<"\n Enter 10 numbers:";
for(i=0;i<10;i++)
{
cin>>x[i];
}
cout<<"\n Enter No. to be searched:";
cin>>data;
int res=Linear_Search(x,10,data);
if(res==1)
cout<<"\n found";
else
cout<<"\n not found";
getch();
return 0;
}

Program no.22
Program to show selection sort.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>

void main()
{
clrscr();
int x[10],i,j,temp,low,pos;
// input
cout<<"\n Enter 10 numbers to be sort:";
for(i=0;i<10;i++)
cin>>x[i];
// Processing
for(i=0;i<9;i++)
{
low=x[i];
pos=i;
for(j=i+1;j<10;j++)
{
if(low>x[j])
{
low=x[j];
pos=j;
}
}
temp=x[i];
x[i]=x[pos];
x[pos]=temp;
} // end of i for loop
// output phase
cout<<"\n Sorted Array:";
for(i=0;i<10;i++)
cout<<setw(4)<<x[i];

getch();
return ;
}

Program no.23
Program to show insertion sort.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>

void main()
{
clrscr();
int x[10],i,j,temp;
// input
cout<<"Enter 10 numbers to be sort:";
for(i=0;i<10;i++)
cin>>x[i];

// Processing
for(i=1;i<10;i++)
{temp=x[i];
j=i-1;
while(temp<x[j] && j>=0)
{
x[j+1]=x[j];
j=j-1;
}
x[j+1]=temp;
}
// output
cout<<"\n sorted Array:";
for(i=0;i<10;i++)
cout<<setw(4)<<x[i];

getch();
return;}

Program no.24
Program to show how copy constructor works.
#include<conio.h>
#include<iostream.h>
#include<string.h>
class student
{
int roll;
char name[30];
public:
student()
{
cout<<"\n Constructor:";
roll=10;
strcpy(name,"Rahul");
}
student(student &s)
{
cout<<"\n Copy constructor:";
roll=s.roll;
strcpy(name,s.name);
}
void input_void()
{
cout<<"\n Enter roll no:";
cin>>roll;
cout<<"\n Enter name:";
cin>>name;
}
void show_data()
{
cout<<"\n Roll no:";
cout<<roll;
cout<<"\n Name:";
cout<<name;
}};
int main()
{
student s;
s.show_data();
cout<<"\n";
student A(s);
A.show_data();
getch();
return 0;
}

Program no.25
Program to show how parametrised
constructor.
#include<conio.h>
#include<iostream.h>

class read_constructor
{ int x;
public:
read_constructor(int a)
{
x=a;
}
void read_data()
{
cin>>x;
}
void show_data()
{
cout<<"Value of x:"<<x;
}
};

int main()
{
read_constructor obj(10);
obj.show_data();
getch();
return 0;
}

Program no.26
Program to show lowest matrix.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>

int main()
{
clrscr();
int x[4][4]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int i,j;
// output phase
cout<<"Lower matrix:";
for(i=0;i<4;i++)
{
cout<<endl;
for(j=0;j<4;j++)
if(i>=j)
cout<<setw(6)<<x[i][j];
else
cout<<setw(6)<<" ";
}
getch();
return 0;
}
Program no.27
Program to show nesting.
#include<conio.h>
#include<iostream.h>

class area
{
int b, h;
double ar;
public:
void input_data()
{
cout<<"\n Enter base:";
cin>>b;
cout<<"\n Enter height:";
cin>>h;
}
void calculate()
{
input_data();
ar=0.5*b*h;
}
void output()
{
calculate();
cout<<"\n Area of triangle:"<<ar;
}
};

int main()
{
clrscr();
area A;
A.output();
getch();
return 0;
}
Program no.28
Program to show dereferencing by using
pointer.
#include<conio.h>
#include<iostream.h>

int* read()
{
int x;
x=45;
return(&x);
}

int main()
{
clrscr();
int *res;
res=read();
cout<<"\n Value at res:"<<res;
getch();
return 0;
}

Program no.29
Program to show linear queue.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>

//function prototype
void add(int &front, int &rear, int x[], int N, int value);
void deletion(int &front, int &rear);
void display(int front, int rear, int x[]);

void main()
{
clrscr();
int x[100],front,rear,choice,value;
front=-1;
rear=-1;
do
{
cout<<"\n Queue Menu";
cout<<"\n 1. Addition";
cout<<"\n 2. Deletion";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter value";
cin>>value;
add(front,rear,x,100,value);
break;
case 2:
deletion(front,rear);
break;
case 3:
display(front,rear,x);
getch();
break;
case 4:
break;
default:
cout<<" wrong choice";
getch();
}
}while(choice!=4);
getch();
return ;
}

void add(int &front, int &rear, int x[], int N, int value)
{
if(rear==-1)
{
front=0;
rear=0;
x[rear]=value;
}
else
{
if(rear>=(N-1))
cout<<"full";
else
{
rear=rear+1;
x[rear]=value;
}
}
}

void deletion (int &front, int &rear)


{
if(front==-1)
cout<<"Empty";
else
{
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=front+1;
}
return;
}

void display (int front, int rear, int x[])


{
int i;
if(front==-1)
cout<<"Empty";
else
{
for(i=front;i<=rear;i++)
cout<<setw(4)<<x[i];
}
return;
}

Program no.30
Program to show circular queue.
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
//Function prototype
void push(int x[], int N, int &M, int data);
void pop(int &m);
void display(int x[], int &m);

void main()
{
clrscr();
int x[100],m,data,choice;
m=0;
do
{
cout<<"\n STACK MENU";
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n 4. Exit";
cout<<"\n Enter choice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<" Enter value:";
cin>>data;
push(x,100,m,data);
break;
case 2:
pop(m);
break;
case 3:
display(x,m);
getch();
break;
case 4:
break;
default:
cout<<"\n Wrong choice";
getch();
}
}while(choice!=4);
}

void push(int x[], int N, int &M, int data)


{
if(M>=N)
cout<<"STACK full";
else
{
x[M]=data;
M=M+1;
}
return;
}

void pop(int &m)


{
if(m<1)
cout<<"\n STACK Empty";
else
m=m-1;
return;
}

void display(int x[],int &m)


{
int i;
if(m<1)
cout<<"\n STACK Empty";
else
for(i=0;i<m;i++)
cout<<setw(6)<<x[i];
return;
}
Program no.31
Program to read binary file.
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>

struct student{
int roll;
char name[30];
char address[80];
};

int main()
{
clrscr();
ifstream obj("student.dat");
student s;
cout<<"\n Reading student.dat file";
while(obj.read((char*)&s, sizeof(student)))
{
cout<<"\nRoll no:"<<s.roll;
cout<<"\n Name:"<<s.name;
cout<<"\n Address:"<<s.address;
}
obj.close();
getch();
return 0;
}

Program no.32
A class student has three data members and
few member functions:
·0 Name
·1 Rollno
·2 Marks
·3 input()
·4display()
write a menu driven program
·5 to create a file
·6 to print the stream according to the total
marks of the students:
1. 96 or more Computer sc.
2. 91 and 95 Electronics
3. 86 and 90 Mechanical
4. 81 and 85 Electrical
5. 76 and 80 Chemical
6. 71 and 75 Civil
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student[20];
int roll;
float marks[5];
public:
void enter(){
cout<<"Enter the name";
gets(name);
cout<<"Enter the Roll Number";
cin>>roll;
cout<<"Enter the marks in 5 subjects";
for(int i=0;i<5;i++)
cin>>Marks[i];
}
void Assign(){
int avg;
avg=(marks[0]+marks[1]+marks[2]+marks[3]+marks[4])/5;
if(avg >= 96)
cout<<"Computer Sc";
if(avg <96 && avg>= 91)
cout<<"Electronics";
if(avg <91 && avg>=86)
cout<<"Mechanical";
if(avg <86 && avg>=81)
cout<<"Electrical";
if(avg <81 && avg>=76)
cout<<"Chemical";
if(avg <76 && avg>=71)
cout<<"Civil";
}}
void main()
{
student stud;
stud.enter();
stud.Assign();
}

Você também pode gostar