Anda di halaman 1dari 55

Q1.

WRITE A PROGRAM USING MULTIPLE INHERITANCE FOR THE CLASSES STUDENT, GAME AND
PERSON.
#include<iostream.h> // Header files declared

#include<stdio.h>

#include<conio.h>

class person{ char name[21];

int age;

public:

void indata()

{cout<<"\n\nEnter the name of Student: " ;

gets(name);

cout<<"\n\nEnter the age : ";

cin>>age;

void outdata();

};

void person::outdata()

cout<<"\n\n";

for(int i=0; i<79; i++)

cout<<"-";

cout<<"\n\nName of the student is: "<<name;

cout<<"\n\nAge of the student is : "<<age;

class game {

char game_name[20];

Page | 1
public:

void input()

cout<<"\n\nEnter the game name : ";

cin.get();cin.getline(game_name,20);

void output()

cout<<"\n\nGame opted by the student is : "<<game_name;

};

class student: public person, public game

{ float Tmarks; // Multiple inheritance

int rollno;

public:

char calgrade()

{if(Tmarks>90)

return 'A';

else if(Tmarks>80&&Tmarks<=90)

return 'B';

else if(Tmarks>70&&Tmarks<=80)

return 'C';

else if(Tmarks>60&&Tmarks<=70)

return 'D';

else

Page | 2
return 'E';

void enter()

indata();

cout<<"\n\nEnter the roll number: "; cin>>rollno;

input();

cout<<"\n\nEnter total marks (out of 100) : ";

cin>>Tmarks;

void display()

outdata();

cout<<"\n\nRoll number : "<<rollno;

OUTPUT();

cout<<"\n\nTotal marks are : "<<Tmarks;

cout<<"\n\nGrade = "<<calgrade();

};

void main()

{ clrscr();

student A;

A.enter();

cout<<”\n\nstudent details are : \n\n”;

A.display();

Page | 3
getch();

Q2.WRITE A PROGRAM USING FUNCTION OVERLOADING TO CALCULATE AREA


OF CIRCLE, SQUARE, RECTANGLE, RIGHT TRIANGLE AND TRIANGLE USING
HERON’S FORMULAE
#include<iostream.h>

#include<math.h> // Header files declared

#include<conio.h>

float area(float p)

return 3.14*p*p;

float area(int p)

return p*p;

} // Function overloading of ‘area’

float area(float p,float q)

return p*q;

float area(int p, int q)

return (0.5*p*q);

float area(float p,float q, float r)

Page | 4
float s;

s=(p+q+r)/2;

return sqrt(s*(s-p)*(s-q)*(s-r));

void main()

{clrscr();

int p,q,ch;

float a,b,r;

char choice;

do{

cout<<"\n\nChoose from the following : ";

cout<<"\n\n1. Area of square ";

cout<<"\n\n2. Area of circle "; // Main Menu

cout<<"\n\n3. Area of rectangle ";

cout<<"\n\n4. Area of right triangle ";

cout<<"\n\n5. Area of triangle ";

cout<<"\n\nEnter your choice : "; cin>>ch;

switch(ch)

case 1: cout<<"\n\nEnter side of square : ";

cin>>p;

cout<<"\n\narea of square is : "<<area(p);

break;

case 2: cout<<"\n\nEnter radius of circle : ";

cin>>a;

Page | 5
cout<<"\n\narea of circle is : "<<area(a);

break;

case 3: cout<<"\n\nEnter length of rectangle : ";

cin>>a;

cout<<"\n\nEnter breadth of rectangle : ";

cin>>b;

cout<<"\n\narea of rectangle is : "<<area(a,b);

break;

case 4: cout<<"\n\nEnter base and altitude of right triangle : ";

cin>>p;

cin>>q;

cout<<"\n\narea of right triangle is : "<<area(q,p);

break;

case 5: cout<<"\n\nEnter sides of triangle : ";

cin>>a;

cin>>b;

cin>>r;

cout<<"\n\narea of triangle is : "<<area(a,b,r);

break;

cout<<"\n\nWant to choose again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

getch();

Page | 6
Q3.WRITE A PROGRAM USING FUNCTION OVERLOADING TO CALCULATE A^B

#include<iostream.h> // Header files declared

#include<math.h>

#include<conio.h>

int calc(int a, int b)

return pow(a,b);

float calc(float a, float b)

return pow(a,b);

float calc(int a, float b)

return pow(a,b);

float calc(float a, int b)

return pow(a,b);

void main()

{clrscr();

int P,Q;

float R,S;

cout<<"Enter int P : "; cin>>P;

Page | 7
cout<<"Enter int Q : "; cin>>Q;

cout<<"Enter real R : "; cin>>R;

cout<<"Enter real S : "; cin>>S;

cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n"; // Using ‘pow’ from math.h

cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n";

cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";

cout<<R<<"^"<<S<<" = "<<pow(R,S);

getch();

Q4.WRITE A PROGRAM TO CREATE A TEXT FILE TO INPUT ROLL NO AND MARKS


OF TEN STUDENTS AND DISPLAY THEM ON SCREEN AFTER READING FROM THE
TEXT FILE USING DATA FILE HANDLING.
#include<fstream.h> // Header files declared

#include<conio.h>

void main()

clrscr();

int a[10]; float b[10];

for(int i=0; i<10; i++)

cout<<"\n\nEnter the roll no. of student "<<i+1<<" : "; cin>>a[i];

cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i];

ofstream student;

student.open("stud.txt");

for(i=0; i<10; i++)

Page | 8
{

student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i];

student.close();

i=0;

cout<<”\n\nStudent details are :\n\n”;

char str[80][80];

ifstream stude;

stude.open("stud.txt",ios::in);

stude.seekg(0); // Pointing to beginning of the text

while(!stude.eof())

{stude.getline(str[i],80);

cout<<str[i]<<"\n\n";

i++;

getch();

Q5.PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER USING SELECTION


SORT SELECTION
#include<iostream.h>

#include<conio.h> // Header files declared

void main()

{clrscr();

int A[80],n;

lb: cout<<"Enter the size of array : ";

Page | 9
cin>>n;

if(n<=0||n>80)

{cout<<"\n\nEnter size of array less than or equal to 80.";

goto lb;

cout<<"\n\nEnter the elements of array : \n\n";

for(int i=0; i<n; i++)

cin>>A[i];

void selection_sort(int A[],int n);

selection_sort(A,n);

cout<<"\n\n\n\n\nSorted array is : \n\n";

for(i=0; i<n; i++)

cout<<A[i]<<" ";

getch();

void selection_sort(int A[], int n) // Function to sort using selection sort

{int small; int k,count=0;

for(int i=0; i<n; i++)

{ small=A[i]; count++;

for(int j=i+1; j<n; j++)

if(A[j]<small)

{small=A[j];

A[j]=A[i];

A[i]=small;

Page | 10
}

cout<<"\n\nArray after iteration "<<count<<" is :\n\n";

for(k=0; k<n; k++)

cout<<A[k]<<" ";

Q6.WRITE A PROGRAM USING POINTERS TO FIND THE LENGTH OF A STRING


AND PRINT THE REVERSED STRING
#include<iostream.h>

#include<string.h> // Header files declared

#include<conio.h>

void main()

clrscr();

char *str= new char[256];

cout<<"\n\nEnter the string : ";

cin.getline(str,256);

cout<<"\n\nThe string length => "<<strlen(str);

cout<<"\n\nThe reverse string is : ";

char intermediate;

for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++)

intermediate = *(str+j);

//Loop to reverse the string

*(str+j)=*(str+i);

Page | 11
*(str+i)=intermediate;

cout<<str;

getch();

Q7.WRITE A PROGRAM USING POINTERS TO SWAP TWO INTEGERS.


#include<iostream.h> // Header files declared

#include<conio.h>

void sWrite a program_using_pointers(int *, int *);

void main()

clrscr();

int a,b;

cout<<"\n\nEnter first integer : "; cin>>a;

cout<<"\n\nEnter second integer : "; cin>>b;

sWrite a program_using_pointers(&a,&b);

cout<<"\n\nNow value of first integer = "<<a;

cout<<"\n\nNow value of second integer = "<<b;

getch();

void sWrite a program_using_pointers(int *a,int *b)

int temp;

temp=*a;

*a=*b;

*b=temp;

Page | 12
}

Q8.WRITE A PROGRAM IN C++ USING POINTERS TO FIND THE SMALLEST AND


THE LARGEST ELEMENT IN AN ARRAY
#include<iostream.h> // Header files declared

#include<conio.h>

void main()

clrscr();

int *array, smallest, largest, n;

cout<<"\n\nEnter the number of elements : ";

cin>>n;

array=new[n];

cout<<"\n\nEnter the elements : \n\n";

for(int i=0; i<n; i++)

cin>>array[i];

i=0;

cout<<"\n\nThe array formed is : \n\n";

while(i!=n)

cout<<array[i]<<" ";

i++;

smallest=array[0];

for(i=0; i<n; i++)

Page | 13
if(array[i]<=smallest)

smallest=array[i]; //Comparing the elements

largest=array[0];

for(i=0; i<n; i++)

if(array[i]>=largest)

largest=array[i];

cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is :

"<<largest;

getch();

Q9.WRITE A PROGRAM IN C++ TO PRINT AND FIND THE SUM OF EVEN OR ODD
NUMBERS USING RECURSION.
#include<iostream.h> // Header files declared

#include<conio.h>

int sum_even(int );

int sum_odd(int );

int sum=0;

int num=0;

void main()

{ clrscr();

int n;

int s_e, s_o;

cout<<"\n\nEnter the number upto which you want the sum of even/odd numbers : ";

Page | 14
cin>>n;

cout<<"\n\n The list of integers is : \n\n";

for(int l=1; l<=n; l++)

cout<<l<<"\n";

s_e=sum_even(n);

s_o=sum_odd(n);

cout<<"\n\nThe sum of even numbers upto "<<n<<" = "<<s_e;

cout<<"\n\nThe sum of odd numbers upto "<<n<<" = "<<s_o;

getch();

int sum_even(int n)

if(num%2==0)

sum=sum+num;

if(num!=n && num<n)

++num;

sum_even(n);

return sum;

int sum_2=0;

int num_2=0;

int sum_odd(int n)

Page | 15
if(num_2%2!=0)

sum_2=sum_2+num_2;

if(num_2!=n)

num_2++;

sum_odd(n);

return sum_2;

Q10.WRITE A PROGRAM TO PRINT AND FIND THE SUM OF FIBONACCI SERIES


USING RECURSION
#include<iostream.h> // Header files declared

#include<conio.h>

int fibonacci(int n);

void main()

clrscr();

int n;

cout<<"\n\n Enter the number of terms upto which you want the sum of fibonnaci series :

";

cin>>n;

cout<<"\n\nThe fibonacci series generated is : \n\n";

cout<<"1 1 ";

int s=fibonacci(n);

cout<<"\n\nThe sum of fibonacci series for "<<n<<" terms = "<<s;

getch();

Page | 16
}

int first=1;

int second=1;

int third;

int i=2;

int sum=0;

int fibonacci(int n)

{ if(n==1)

sum=1;

else if(n==2)

sum=2;

// n = 1 2 3 4 5

else if(n>1 && i!=n) // num = 1 1 2 3 5

third=first+second;

cout<<third<<” “;

if(i==2)

sum+=first+second+third;

else

sum+=third;

first=second;

second=third;

++i;

fibonacci(n);

Page | 17
return sum;

Q11.WRITE A PROGRAM IN C++ TO READ FILE “SPORTS.DAT” AND COPY ONLY


THOSE RECORDS WHERE EVENT NAME IS ATHLETIC USING THE CONCEPT OF
DATA FILE HANDLING.
#include<fstream.h>

#include<string.h> // Header files declared

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

struct sports {

char event[20];

char participants[10][30];

int no_of_participants;

} s[20], s2[20];

void copy(fstream &ob);

int i=0;

void main()

clrscr();

char choice;

fstream ob("sports.dat",ios::binary|ios::in|ios::out);

do

if(i>0)

cin.get();

Page | 18
cout<<"\n\nEnter the name of Event : ";

cin.getline(s[i].event,20);

cout<<"\n\nEnter the total number of participants in Event "<<s[i].event<<" : ";

cin>>s[i].no_of_participants;

cout<<"\n\nEnter the name of Praticipants : \n";

cin.get();

for(int j=0; j<s[i].no_of_participants; j++)

cin.getline(s[i].participants[j],30);

ob.write((char*)&s[i], sizeof(sports)); //To write into the file

cout<<"\n\n\nWant to Enter Details of Another Event (Y/N) : ";

cin>>choice;

i++;

} while(choice=='y'||choice=='Y');

cout<<"\n\n\n\n\n*********************************************************************
*********

******************************\n\n";

copy(ob);

cout<<"\n\n***************************************************************************
*********

*****************************\n\n\n";

getch();

void copy(fstream &o)

sports s[20];

o.seekg(0);

Page | 19
ofstream file;

file.open("athletic.dat",ios::binary);

file.seekp(0);

int j,n=0;

int c=0;

while(o)

o.read((char*)&s[c], sizeof(sports));

if(strcmp("athletic",s[c].event)==0)

file.write((char*)&s[c], sizeof(sports));

n=1;

break;

c++;

if(n==0)

cout<<"\n\nUnsuccessful Search.";

getch();

exit(0);

o.close();

file.close();

sports sp;

Page | 20
ifstream oo;

oo.open("athletic.dat",ios::binary);

while(oo)

oo.read((char*)&sp, sizeof(sports));

cout<<"\n\nThe Records of file are : \n\n";

cout<<"\n\nEvent = "<<sp.event;

cout<<"\n\n\n\nThe Participants are : \n\n";

for(int i=0; i<sp.no_of_participants; i++)

cout<<sp.participants[i]<<"\n\n";

oo.close();

remove("athletic.dat");

remove("sports.dat");

Q12.WRITE A PROGRAM IN C++ TO CREATE A LINKED LIST OF INTEGERS AND


PERFORM SOME BASIC OPERATION ON IT.
#include<iostream.h> // Header files declared

#include<process.h>

#include<conio.h>

struct node {

int info;

node*next;

}*rear,*start,*newptr,*save,*ptr;

Page | 21
void delnode()

if(start==NULL)

cout<<"underflow!!";

else

ptr=start;

start=start->next;

delete ptr;

cout<<"\n\nFirst node deleted";

void insert_beg(node*np)

//To insert at the beginning

if(start==NULL)

start=np;

else

save=start;

start=np;

np->next=save;

void display(node*np)

Page | 22
{

while(np!=NULL)

cout<<np->info<<"->";

np=np->next;

cout<<"\n";

node*create_new_node(int n)

ptr=new node;

ptr->info=n;

ptr->next=NULL;

return ptr;

void insert_end(node*np)

if(start==NULL)

start=rear=np;

else

rear->next=np;

rear=np;

Page | 23
void search(node*np,int a)

int flag=0;

while(np!=NULL)

if(np->info==a)

flag=1;

break;

else

np=np->next;

if(flag)

cout<<a<<"\n\nFound";

else

cout<<"\n\nNot found";

void main()

clrscr();

start=rear=NULL;

char ans='y',choice;

int ch,info,n;

do //Main Menu

Page | 24
{

cout<<"\n\nChoose from the menu :\n1.Insert at the end\n2.Insert at

beginning\n3.Searching\n4.Deletion from begining \n5.Display \n";

cout<<"\n\nEnter your choice";

cin>>ch;

switch(ch)

case 1: do

cout<<"\n\nEnter information for the new node : ";

cin>>info;

newptr=create_new_node(info);

if(newptr!=NULL)

cout<<"\n\nNode has been created";

else

cout<<"\n\nNode cannot be created;";

exit(1);

insert_end(newptr);

cout<<"\n\nThe Linked list is : ";

display(start);

cout<<"\n\nWant to insert again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

Page | 25
break;

case 2: do

cout<<"\n\nEnter info for new node: ";

cin>>info;

newptr=create_new_node(info);

if(newptr!=NULL)

cout<<"\n\nNode has been created ";

else

cout<<"\n\nNode cannot be created ";

exit(1);

insert_beg(newptr);

cout<<"\n\nThe Linked list is : ";

display(start);

cout<<"\n\nWant to insert again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

break;

case 3: do

cout<<"\n\nEnter info to be searched : ";

cin>>n;

search(start,n);

Page | 26
cout<<"\n\nWant to search again : ";

cin>>choice;

}while(choice=='y'||choice=='Y');

break;

case 4: delnode();

break;

case 5: display(start);

break;

}// end of do switch

cout<<"\n\nWant to choose again : ";

cin>>ans;

while(ans=='y'||ans=='Y');

getch();

Q13. WRITE A PROGRAM IN C++ TO IMPLEMENT QUEUE AS A LINKED LIST


#include<iostream.h> // Header files declared

#include<conio.h>

struct node {

int roll;

node* next;

}*front,*rear,*ptr,*newptr,*np;

node *create(int a)

ptr=new node; //To create a new node

Page | 27
ptr->roll=a;

ptr->next=NULL;

return ptr;

void insert(node *np)

if(front==NULL)

front=rear=np;

else

rear->next=np;

rear=np;

void delet()

if(front==NULL)

cout<<"\n Underflow!!!!";

else

ptr=front;

front=front->next;

delete ptr;

Page | 28
void display(node *np)

while(np!=NULL)

cout<<np->roll<<"-> ";

np=np->next;

void main()

clrscr();

front=rear=NULL;

int n,m;

char ans,ch;

do

{ cout<<"\nChoose from the menu : "

<<"\n 1) Insert." //Main Menu

<<"\n 2) Delete

<<"\n 3) Display."

<<"\n\n Enter your choice : ";

cin>>n;

switch(n)

case 1: ans='y';

while(ans=='y'||ans=='Y')

Page | 29
{

cout<<"\n Enter element to be inserted .";

cin>>m;

newptr=create(m);

if(newptr==NULL)

cout<<"\n Cannot create !!!!";

insert(newptr);

cout<<"\n The Queue formed is : ";

display(front);

cout<<"\n Want to enter more nodes ?: ";

cin>>ans;

break;

case 2: ans='y';

while(ans=='y'||ans=='Y')

delet();

cout<<"\n Queue : ";

display(front);

cout<<"\n Want to delete more ?: ";

cin>>ans;

break;

case 3: cout<<"\n Queue : ";

display(front);

Page | 30
break;

default: cout<<"\n You entered wrong choice...";

cout<<"\n Want to return to main menu ? : ";

cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

Q14.WRITE A PROGRAM TO IMPLEMENT STACK AS A LINKED LIST


#include<iostream.h> // Header files declared

#include<conio.h>

#include<process.h>

struct node {

int roll;

node* next;

}*top,*save,*ptr,*newptr,*np;

node *create(int a)

ptr=new node; //To create a new node

ptr->roll=a;

ptr->next=NULL;

return ptr;

void push(node *np)

Page | 31
if(top==NULL)

top=np;

else

save=top;

top=np;

np->next=save;

void pop()

if(top==NULL)

cout<<"\n Underflow!!!!";

else

ptr=top;

top=top->next;

delete ptr;

void display(node *np)

while(np!=NULL)

cout<<np->roll<<" -> ";

Page | 32
np=np->next;

void main()

clrscr();

top=NULL;

int n,m;

char k,ch;

do {

cout<<"\nChoose from the menu :\n"

<<"\n 1. Push."

<<"\n 2. Pop."

<<"\n 3. Display." //Main Menu

<<"\n 4. Quit."

<<"\n\nEnter your choice : ";

cin>>n;

switch(n)

case 1: k='y';

while(k=='y'||k=='Y')

cout<<"\n Enter element to be inserted .";

cin>>m;

Page | 33
newptr=create(m);

if(newptr==NULL)

cout<<"\n Cannot create !!!!";

push(newptr);

cout<<"\n The Stack formed is : ";

display(top);

cout<<"\n\n Want to enter again ?: ";

cin>>k;

break;

case 2: k='y';

while(k=='y'||k=='Y')

pop();

cout<<"\n The Stack formed is : \n\n";

display(top);

cout<<"\n\n Want to delete again ?: ";

cin>>k;

break;

case 3: cout<<"\n The Stack formed is : ";

display(top);

break;

case 4: exit(0);

break;

Page | 34
default: cout<<"\n Please enter desired keyword : ";

cout<<"\n Do you want to continue..? : ";

cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

Q15.WRITE A PROGRAM IN C++ TO IMPLEMENT QUEUE AS AN ARRAY


#include<iostream.h> // Header files declared

#include<conio.h>

#include<process.h>

const int size=50;

int q[size],rear=-1,front=-1;

int insert(int q[],int ele)

if(rear==size-1)

return(-1);

else if(rear==-1)

front=rear=0;

q[rear]=ele;

else

rear++;

Page | 35
q[rear]=ele;

return(0);

int delet(int q[])

int r ;

if(front==-1)

return(-1);

else

r=q[front];

if(rear==front)

front=rear=-1;

else

front++;

return r;

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

if(front==-1)

return;

else

Page | 36
{

for(int i=front;i<=rear;i++)

cout<<q[i]<<" ";

void main()

clrscr();

int n,u,k,m;

char ch,ans;

do

cout<<"\nChoose from the menu :\n"

<<"\n 1. Insert"

<<"\n 2. Delete" //Main Menu

<<"\n 3. Display"

<<"\n\n Enter your choice : "; cin>>n;

switch(n)

case 1: ans='y';

while(ans=='y'||ans=='Y')

cout<<"\n Enter element to be inserted :"; cin>>m;

k=insert(q,m);

Page | 37
if(k==-1)

cout<<"\n Overflow !!!!";

cout<<"\n The resultant Queue is : ";

display(q,front,rear);

cout<<"\n\n Want to enter again ?: ";

cin>>ans;

break;

case 2: ans='y';

while(ans=='y'||ans=='Y')

u=delet(q);

if(u==-1)

cout<<"\n Underflow !!!!";

break;

else

cout<<"\n The deleted element is "<<u<<"\n";

cout<<"\n The resultant Queue is : \n\n";

display(q,front,rear);

Page | 38
cout<<"\n\n Want to delete again ?: ";

cin>>ans;

break;

case 3: cout<<"\n The Queue is : \n\n";

display(q,front,rear);

break;

default: cout<<"\n Please enter desired keyword : ";

cout<<"\n Choose from the menu again ? : ";cin>>ch;

}while(ch=='y'||ch=='Y');

getch();

Q16.WRITE A PROGRAM IN C++ TO IMPLEMENT STACK AS AN ARRAY


#include<iostream.h>

#include<conio.h> // Header files declared

#include<process.h>

int pop(int[],int&);

int push(int[],int&,int);

void display(int[],int);

const int size=50;

void main()

clrscr();

Page | 39
char m,ch;

int k,stack[size],item,top=-1,res;

do

{ cout<<"\nChoose from the following : \n\n"

<<"\n 1. Push"

<<"\n 2. Pop"

<<"\n 3. Display" //Main Menu

<<"\n 4. Exit"

<<"\n\nEnter your choice : "; cin>>k;

switch(k)

case 1: ch='y';

while(ch=='y'||ch=='Y')

{ cout<<"\nEnter the element : ";

cin>>item;

res=push(stack,top,item);

if(res==-1)

{cout<<"\nOverflow !!!!";

exit(1); }

cout<<"\nThe stack formed is : \n\n";

display(stack,top);

cout<<"\n\n\nWant to enter again ?: ";

cin>>ch;

break;

Page | 40
case 2: ch='y';

while(ch=='y'||ch=='Y')

{ res=pop(stack,top);

if(res==-1)

cout<<"\nUnderflow !!!!";

exit(1);

else

cout<<"\nThe deleted Element is : "<<res<<endl;

cout<<"\nThe resultant stack is : \n\n";

display(stack,top); }

cout<<"\nWant to delete again ? : ";

cin>>ch;

break;

case 3: cout<<"\nThe resultant stack is : ";

display(stack,top);

break;

case 4: exit(0);

break;

default: cout<<"\nPlease enter desired keyword : ";

} // end of switch

cout<<"\n\nChoose from the menu again ? : ";

Page | 41
cin>>m;

}while(m=='y'||m=='Y'); // end of do-while loop

getch();

} // end of main()

int push(int stack[],int &top,int el)

if(top==size-1)

return -1;

else

top++;

stack[top]=el;

return 0;

int pop(int stack[],int &top)

int ret;

if(top==-1)

return -1;

else

ret=stack[top];

top--;

return ret;

Page | 42
}

void display(int stack[],int top)

cout<<stack[top]<<"<--";

for(int i=top-1;i>=0;i--)

cout<<stack[i]<<"<--";

Q16.PROGRAM TO SEARCH AN ELEMENT OF AN ARRAY USING LINEAR SEARCH


METHOD
#include<iostream.h> // Header files declared

#include<conio.h>

void main()

{ clrscr();

int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/

cout<<"Enter the array size : ";

cin>>n; // n=size upto which user wants to insert values in array

cout<<"\n\nEnter elements of array : \n";

for(int i=0; i<n; i++)

cin>>A[i];

cout<<"\n\nThe array formed = ";

for(i=0; i<n; i++)

cout<<A[i]<<" ";

cout<<"\n\nEnter the element to be searched : ";

cin>>p; // p=element to be searched

Page | 43
void linear_search ( int A[], int n, int p); //function declaration

linear_search(A,n,p);

getch();

void linear_search(int A[], int n, int p)

{ int count=0; int B[50]; int flag=0;

for(int i=0; i<n; i++)

{ flag=count;

if( A[i]==p)

B[count]=i;

count++; //Linear Search..

flag=count;

if(flag==0)

cout<<"\n\nRequested element not found.";

else

for(i=0; i<=n; i++)

{ if(A[i]==p)

cout<<"\n\nElement "<<p<<" is found";

cout<<"\n\nSubscript = "<<i<<"\n\nPosition = "<<i+1<<"\n\n\n";

Page | 44
}

Q17.WRITE A PROGRAM IN C++ TO PERFORM BASIC OPERATIONS ON THE TWO


COMPLEX NUMBERS USING STRUCTURES.
#include<iostream.h> // Header files declared

#include<math.h>

#include<conio.h>

struct complex {

int i,r;

}x,y;

void add(complex a, complex b)

cout<<"\n\n Resultant of addition of complex numbers = "<<a.r+b.r<<" + ("<<a.i+b.i<<")i

";

void subtract(complex a,complex b)

{int ch;

l:

cout<<"\n\n\t1. Subtract complex 2 from complex 1 : ";

cout<<"\n\n\t12.Subtract complex 1 from complex 2 : ";

cout<<"\n\n\tEnter your choice : "; cin>>ch;

switch(ch)

case 1 : cout<<"\n\nResultant of subtraction of complex number => "<<"("<<a.rb.r<<")"<<"

+ ("<<a.i-b.i<<") i"; break;

case 2 : cout<<"\n\nResultant of subtraction of complex number => "<<"("<<b.ra.r<<")"<<"

Page | 45
+ ("<<b.i-a.i<<") i"; break;

default : cout<<"\n\nPlease enter desired keyword. "; goto l;

void multiply(complex a, complex b)

cout<<"\n\nResultant of multiplication of complex number => "<<"("<<a.r*b.ra.i*b.i<<")"<<"

+ ("<<a.r*b.i+a.i*b.r<<") i";

void divide(complex a,complex b)

{int ch;

l: cout<<"\n\n\t1. Divide complex 2 from complex 1 : ";

cout<<"\n\n\t2.Divide complex 1 from complex 2 : ";

cout<<"\n\n\tEnter your choice : "; cin>>ch;

switch(ch)

case 1 : cout<<"\n\nResultant of division of complex number =>

"<<((a.r*b.r)+(a.i*b.i))/(pow(b.r,2)+pow(b.i,2))

<<" + ("<<((a.i*b.r)-(a.r*b.i))/(pow(b.r,2)+pow(b.i,2))<<") i";

break;

case 2 : cout<<"\n\nResultant of division of complex number =>

"<<((b.r*a.r)+(b.i*a.i))/(pow(a.r,2)+pow(a.i,2))

<<" + ("<<((b.i*a.r)-(b.r*a.i))/(pow(a.r,2)+pow(a.i,2))<<") i";

break;

default : cout<<"\n\nPlease enter desired keyword. "; goto l;

Page | 46
}

void main()

clrscr();

int choice;

char ch;

cout<<"Complex number 1 : ";

cout<<"\n\n\tEnter the real part : "; cin>>x.r;

cout<<"\n\n\tEnter the imaginary part : "; cin>>x.i;

cout<<"\n\n";

cout<<"Coplex number 1 => "<<x.r<<"+("<<x.i<<")i";

cout<<"\n\n\n\nComplex number 2 : ";

cout<<"\n\n\tEnter the real part : "; cin>>y.r;

cout<<"\n\n\tEnter the imaginary part : "; cin>>y.i;

cout<<"\n\n";

cout<<"\n\nComplex number 2 => "<<y.r<<"+("<<y.i<<")i";

do

{cout<<"\n\n\nChoose from the folowing : ";

cout<<"\n\n1. Add two complex numbers ";

cout<<"\n\n2. Subtract two complex numbers "; //Main Menu

cout<<"\n\n3. Multiply two complex numbers ";

cout<<"\n\n4. Divide two complex numbers ";

cout<<"\n\nEnter your choice : "; cin>>choice;

switch(choice)

Page | 47
{

case 1:add(x,y); break;

case 2:subtract(x,y); break;

case 3:multiply(x,y); break;

case 4:divide(x,y); break;

cout<<"\n\nWant to Choose again => ";

cin>>ch;

while(ch=='y'||ch=='Y');

getch();

Q18.PROGRAM TO SORT THE GIVEN ARRAY IN ASCENDING ORDER USING


BUBBLE SORT METHOD
#include<iostream.h> // Header files declared

#include<conio.h>

void main()

{clrscr();

int A[80],n;

cout<<"Enter desired size of array (<80): "; cin>>n;

cout<<"\n\nEnter the array : \n";

for(int i=0; i<n; i++)

cin>>A[i];

cout<<"\n\nArray of elements is as shown below : \n\n";

for(i=0; i<n; i++)

cout<<A[i]<<" ";

Page | 48
cout<<"\n\nNow Elements will be arranged in ascending order using bubble sort :\n\n";

void bubble_sort(int A[],int n);

bubble_sort(A,n);

getch();

void bubble_sort (int A[], int n)

{ int temp; int count=0;

for(int i=0; i<n; i++)

for(int j=0; j<n-1; j++) //Bubble sorting..

{ if(A[j+1]<A[j])

{ count++;

temp=A[j+1];

A[j+1]=A[j];

A[j]=temp;

cout<<"\n\nArray for iteration "<<count<<" is : \n\n";

for(int k=0; k<n; k++)

cout<<A[k]<<" ";

Q19.WRITE A PROGRAM IN C++ TO CREATE A BINARY FILE AND WRITE OBJECTS


OF CLASS BOOK TO IT AND DISPLAY THEM ON SCREEN AFTER READING FROM
THE FILE.
#include<fstream.h> // Header files declared

Page | 49
#include<conio.h>

int p;

class book {

int book_no;

char book_title[80];

float price;

float total_cost(int n)

float p;

p=n*price;

return p;

public :

void input()

cout<<"\n\nEnter Book no. : "; cin>>book_no;

cout<<"\n\nEnter book title : ";

cin.get();

cin.getline(book_title,80);

cout<<"\n\nEnter book price : "; cin>>price;

void purchase()

cout<<"\n\nEnter number of copies to be purchased : "; cin>>p;

cout<<"\n\nTotal cost (in Rs ) = "<<total_cost(p)<<"\n\n\n";

Page | 50
}

void display()

cout<<"\n\nBook title is : "<<book_title;

cout<<"\n\nBook number is : "<<book_no;

cout<<"\n\nPrice of book is : "<<price;

cout<<"\n\nTotal cost of "<<p<<" books => "<<total_cost(p)<<"

Rs\n\n\n\n";

};

void main()

{clrscr();

book b[3],p[3];

fstream bo;

cout<<"---------------------------------------------------------Enter Details----------------------------------\n";

for(int i=0; i<3; i++)

b[i].input();

b[i].purchase();

bo.open("xy.txt",ios::out|ios::binary); // Writing to the binary file

bo.write((char *)&b[i],sizeof(b[i]));

bo.close();

bo.open("xy.txt",ios::in |ios::binary); //Reading from the binary file.

bo.read((char *)&p[i], sizeof(p[i]));

bo.close();

Page | 51
}

cout<<"\n\n--------------------------------------------------- Book Details ------------------------------------“;

for(i=0; i<3; i++)

p[i].display();

getch();

Q20.PROGRAM TO SEARCH AN ELEMENT OF AN ARRAY USING BINARY SEARCH


#include<iostream.h> // Header files declared

#include<process.h>

#include<conio.h>

void main()

{ clrscr();

int A[50], n,p;

cout<<"Enter the Size of array : ";

cin>>n;

cout<<"\n\nEnter the elements : \n\n";

for(int i=0; i<n; i++)

cin>>A[i];

cout<<"\n\nArray formed is : ";

for(i=0; i<n; i++)

cout<<A[i]<<" ";

cout<<"\n\nEnter the element to be searched : ";

cin>>p;

Page | 52
void binary_search(int A[], int n, int p); //declaration

binary_search(A,n,p);

getch();

void binary_search(int A[], int n, int p)

{ int L,U,mid; char ch;

lb: L=0; U=n-1;

while(L<=U) //i.e loop will continue if L<=u. if L>U loop will end

{ mid=(L+U)/2;

if(A[mid]==p)

{ cout<<"\n\nElement "<<p<<" found. Search Successful.";

cout<<"\n\nSubscript = "<<mid<<" \n\nPosition = "<<mid+1;

break;

else if(p<=A[mid])

U=mid-1;

else

L=mid+1;

}//end of while loop

if(L>U)

{cout<<"\n\nUnsuccessful search.";

cout<<"\n\n\n\nWant to search again. : "; cin>>ch;

if(ch=='y'||ch=='Y')

{cout<<"\n\n\n\nEnter the element to be searched : ";

cin>>p;

Page | 53
goto lb;}

else

exit(1);

Q21.WRITE A PROGRAM TO CREATE A TEXT FILE TO INPUT ROLL NO. AND


MARKS OF FIVE STUDENTS AND DISPLAY THEM ON SCREEN AFTER READING
FROM THE TEXT FILE USING DATA FILE HANDLING.

#include<fstream.h> // Header files declared

#include<conio.h>

void main()

clrscr();

int a[5]; float b[5];

for(int i=0; i<5; i++)

cout<<"\n\nEnter the roll no. of student "<<i+1<<" : "; cin>>a[i];

cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i];

ofstream student;

student.open("stud.txt");

for(i=0; i<5; i++) // writing to “stud.txt”

student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i];

Page | 54
student.close();

i=0;

cout<<”\n\nStudent details are :\n\n”;

char str[80][80];

ifstream stude;

stude.open("stud.txt",ios::in);

stude.seekg(0);

while(!stude.eof())

{stude.getline(str[i],80); //reading from “stud.txt”

cout<<str[i]<<"\n\n";

i++;

getch(

Page | 55