Download as pdf or txt
Download as pdf or txt
You are on page 1of 83

ASSIGNMENT - 1

Write a program in C++ to add two distance type objects into third one.
Structure distance contains following components1.
feet ( integer type )
2.
inches ( integer type )
(Program must have following general functions1.
To input a distance type object.
2.
To add two distance type objects (Pass two distance type arguments and return a distance type
object.)
3.
to display distance type object.)

====================
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
struct distance
{
public:
int feet;
int inches;
};
distance input()
{
distance temp;
lb:
cout<<"\n Enter feet: \t";
cin>>temp.feet;
cout<<"\n Enter inches: \t";
cin>>temp.inches;
if(temp.inches>=12)
{
cout<<"\n\n\t\t !!!INVALID DATA!!!\n\t\t ENTER AGAIN";
goto lb;
}
return temp;
}
distance add(distance temp1,distance temp2)
{
distance temp3;
temp3.feet=temp1.feet+temp2.feet;
if(temp1.inches+temp2.inches>=12)
temp3.feet++;
temp3.inches=(temp1.inches+temp2.inches)%12;
return temp3;
}
Computer Science Assignment

void display(distance temp)


{
cout<<"\n\t FEET : "<<temp.feet<<"\n\t INCHES : "<<temp.inches<<"\n";
}
void main()
{
distance d[3];
clrscr();
int i=0;
for(i=0;i<2;++i)
{
cout<<"\n Enter Distance - "<<i+1<<"\n";
d[i]=input();
}
clrscr();
for(i=0;i<2;++i)
{ cout<<"\n DISTANCE "<<i+1;
display(d[i]);
}
d[3]=add(d[1],d[2]);
cout<<"\n THE Sum of two distances is :";
display(d[3]);
getch();
}

====================
OUTPUT
----------------------------------------------------------------------------Enter Distance - 1
Enter feet:

24

Enter inches: 13
!!!INVALID DATA!!!
ENTER AGAIN
Enter feet: 24
Enter inches: 10
Enter Distance - 2
Enter feet:

20

Enter inches: 4
Computer Science Assignment

----------------------------------------------------------------------------DISTANCE 1
FEET : 24
INCHES : 10
DISTANCE 2
FEET : 20
INCHES : 4
THE Sum of two distances is :
FEET : 21
INCHES : 9
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 2
Imagine a ticket selling booth at a fair. People passing by are requested to purchase a ticket. A ticket is
priced Rs.2.50/- The booth keeps the track of the number of people that have visited the booth, and of the
total amount of money collected.
Model this ticket selling booth with a class ticbooth including following members :
Data Members :
Number of people visited.
Total amount of money collected.
Member Functions :
To assign initial values (assign 0 to both data members)
To increment only people total in case ticket is not sold out.
To increment people total as well as amount total if a ticket is sold out.
To display the two totals
To display the number of tickets sold out (a tricky one)
Test above-mentioned class in main() function.

====================
#include<iostream.h>
#include<conio.h>
class ticbooth
{
int no;
float amount;
public:
ticbooth()
{
no=0;
amount=0;
}
void tic_nt_sold()
{
++no;
}
void tic_sold()
{
++no;
amount+=2.50;
}
void disp1()
{
cout<<"\n\n\t No. of People Visited: "<<no;
cout<<"\n\t Total amount :"<<amount;
Computer Science Assignment

}
void disp2()
{
cout<<"\n\n\n\t Total No. Tickets Sold = "<<(int)(amount/2.50);
}
};
void main()
{
clrscr();
ticbooth tic;
int choice=0;
cout<<"\n\n \t\t\t *** WELCOME ***";
while(choice>=0&&choice<=3)
{
clrscr();
cout<<"\n\t\t\t MENU"
<<"\n\n 1.Update Tickets"
<<"\n 2.View Totals"
<<"\n 3.View tickets Sold"
<<"\n 4.EXIT";
cout<<"\n Enter Choice \n\t";
cin>>choice;
switch(choice)
{
case 1: clrscr();
int opt=1;
while(opt==1||opt==2)
{
cout<<"\n Enter Options as below "
<<"\n 1. If Ticket sold"
<<"\n 2. If Ticket not sold"
<<"\n\n\t * else any key ="
<<"Back to Main Menu\n\t";
cin>>opt;
switch(opt)
{
case 1:tic.tic_sold();
break;
case 2:tic.tic_nt_sold();
break;
};
}
break;
case 2: clrscr();
Computer Science Assignment

cout<<"\n\n\n";
tic.disp1();
getch();
break;
case 3:
clrscr();
cout<<"\n\n\n";
tic.disp2();
getch();
break;
case 4:break;
default: choice=0;
cerr<<"\n\n\n\t\t\t !!!! WRONG CHOICE SELECTION ";
getch();
};
}
}

====================
OUTPUT
----------------------------------------------------------------------------MENU
1.Update Tickets
2.View Totals
3.View tickets Sold
4.EXIT
Enter Choice
1
----------------------------------------------------------------------------Enter Options as below
1. If Ticket sold
2. If Ticket not sold
* else any key =Back to Main Menu
1
Enter Options as below
1. If Ticket sold
2. If Ticket not sold
* else any key =Back to Main Menu
1
Enter Options as below
Computer Science Assignment

1. If Ticket sold
2. If Ticket not sold
* else any key =Back to Main Menu
2
Enter Options as below
1. If Ticket sold
2. If Ticket not sold
* else any key =Back to Main Menu
2
Enter Options as below
1. If Ticket sold
2. If Ticket not sold
* else any key =Back to Main Menu
1
Enter Options as below
1. If Ticket sold
2. If Ticket not sold
* else any key =Back to Main Menu
0
----------------------------------------------------------------------------MENU
1.Update Tickets
2.View Totals
3.View tickets Sold
4.EXIT
Enter Choice
2
-----------------------------------------------------------------------------

No. of People Visited: 5


Total amount :7.5
----------------------------------------------------------------------------MENU
1.Update Tickets
2.View Totals
3.View tickets Sold
4.EXIT
Enter Choice
Computer Science Assignment

3
-----------------------------------------------------------------------------

Total No. Tickets Sold = 3


----------------------------------------------------------------------------MENU
1.Update Tickets
2.View Totals
3.View tickets Sold
4.EXIT
Enter Choice
4
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 3
Define a class to represent batsmen in a cricket team. Include the following members:
Data Members :
First name
Last name
Runs made
Number of fours
Number of sixes
Member Functions :
To assign the initial values
To update runs made
(It should simultaneously update fours and sixes, if required)
To display the batsmans information.
Test this class in main() function. Make 11 objects to represent a team.
Make a menu with following options.
1.
Assign the basic values to all of the team members.
2.
Update the information of any batsman on the basic of his first name
3.
Display the information of any batsman on the basis of his first name
4.
Quit

====================
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
class batsman
{
char firstname[25];
char lastname[25];
int runs;
int fours;
int sixes;
public:
void initialise()
{
cout<<"\n\t\t";
cout<<"\n\tFIRST NAME:\t";
gets(firstname);
cout<<"\n\tLAST NAME:\t";
gets(lastname);
cout<<"\n\tRuns Made(Till Yet):\t";
cin>>runs;
cout<<"\n\t\tFOURS:\t";
Computer Science Assignment

cin>>fours;
cout<<"\n\t\tSIXES:\t";
cin>>sixes;
}
void update_runs(int temp)
{
runs+=temp;
if(temp==4)
++fours;
if(temp==6)
++sixes;
}
void display()
{
cout<<"\n\t\t Details Of Batsman are as follows ";
cout<<"\n\tFIRST NAME:\t"
<<firstname;
cout<<"\n\tLAST NAME:\t"
<<lastname;
cout<<"\n\tRuns Made:\t"
<<runs;
cout<<"\n\t\tFOURS:\t"
<<fours;
cout<<"\n\t\tSIXES:\t"
<<sixes;
}
int name(char* name)
{
if(strcmp(name,firstname))
return 1;
else
return 2;
}
}batsman[11];
void main()
{ clrscr();
int choice,temp,i;
char name[25];
char ctrl;
cout<<"\n\n\n\t\t ****** WELCOME ******";
do{
menu:
cout<<"\n\n\n \tx-x MENU x-x"
<<"\n 1. Assign Basic Values to all Team Members"
<<"\n 2. Update information of any batsman"
<<"\n 3. Display information of any batsman"
<<"\n 4. Quit\n\t";
Computer Science Assignment

cin>>choice;
switch(choice)
{
case 1: clrscr();
for(i=0;i<11;++i)
{
cout<<"\n\n\t Enter Details of Batsman "<<i+1;
batsman[i].initialise();
clrscr();
}break;
case 2: clrscr();
cout<<"\n Enter First-Name Of Batsman \t ";
gets(name);
temp=0;
for(i=0;i<11;++i)
{
temp=batsman[i].name(name);
if(temp==1)
break;
}
--i;
if(temp!=1)
{
cout<<"\n Batsman NOT FOUND";
goto menu;
}
cout<<"\n Batsman FOUND";
batsman[i].display();
for(;;)
{
cout<<"\n Enter Runs To be Updated (7 for out)";
cin>>temp;
if(temp<7)
batsman[i].update_runs(temp);
if(temp==7)
break;
if(temp>7)
cout<<"\n\t\t * Invalid Runs Enter Again\n";
}
clrscr();
cout<<"\n Batsman Information after Update\n";
batsman[i].display();
break;
case 3: clrscr();
cout<<"\n Enter First-Name Of Batsman\t";
gets(name);
temp=0;
for(i=0;i<11;++i)
{
temp=batsman[i].name(name);
Computer Science Assignment

if(temp==1)
break;
}
--i;
if(temp==0)
{
cout<<"\n Batsman NOT FOUND \n";
break;
}
cout<<"\n Batsman FOUND";
batsman[i].display();
break;
case 4: cout<<"\n\n\n\n\t\t Confirm Exit(Y/N)??\t";
cin>>ctrl;
if(ctrl=='y'||ctrl=='Y')
exit(1);
default: cout<<"\n Invalid Choice\n";
};
} while(1);
}

====================
OUTPUT
----------------------------------------------------------------------------x-x MENU x-x
1. Assign Basic Values to all Team Members
2. Update information of any batsman
3. Display information of any batsman
4. Quit
1
----------------------------------------------------------------------------Enter Details of Batsman 1
FIRST NAME:

Sachin

LAST NAME:

Tendulkar

Runs Made(Till Yet):

185

FOURS: 6
SIXES: 4
----------------------------------------------------------------------------Enter Details of Batsman 2
FIRST NAME:

Yuvraj

LAST NAME:

Singh

Computer Science Assignment

Runs Made(Till Yet):

148

FOURS: 4
SIXES: 2
----------------------------------------------------------------------------Enter Details of Batsman 3
FIRST NAME:

MS

LAST NAME:

Dhoni

Runs Made(Till Yet):

142

FOURS: 3
SIXES: 2
----------------------------------------------------------------------------Enter Details of Batsman 4
FIRST NAME:

Virender

LAST NAME:

Sehwag

Runs Made(Till Yet):

258

FOURS: 4
SIXES: 3
----------------------------------------------------------------------------Enter Details of Batsman 5
FIRST NAME:

Gautam

LAST NAME:

Gambhir

Runs Made(Till Yet):

148

FOURS: 4
SIXES: 5
----------------------------------------------------------------------------Enter Details of Batsman 6
FIRST NAME:

Sanath

LAST NAME:

Jayasuriya

Runs Made(Till Yet):

153

Computer Science Assignment

FOURS: 24
SIXES: 12
----------------------------------------------------------------------------Enter Details of Batsman 7
FIRST NAME:

Shahid

LAST NAME:

Afridi

Runs Made(Till Yet):

14

FOURS: 2
SIXES: 1
----------------------------------------------------------------------------Enter Details of Batsman 8
FIRST NAME:
Saurav
LAST NAME:
Ganguly
Runs Made(Till Yet):

178

FOURS: 2
SIXES: 1
----------------------------------------------------------------------------Enter Details of Batsman 9
FIRST NAME:

Adam

LAST NAME:

Gilchrist

Runs Made(Till Yet):

147

FOURS: 2
SIXES: 3
----------------------------------------------------------------------------Enter Details of Batsman 10
FIRST NAME:

Suresh

LAST NAME:

Raina

Runs Made(Till Yet):

128

FOURS: 4
SIXES: 3
----------------------------------------------------------------------------Computer Science Assignment

Enter Details of Batsman 11


FIRST NAME:

Gaurav

LAST NAME:

Arora

Runs Made(Till Yet):

125

FOURS: 2
SIXES: 1
----------------------------------------------------------------------------x-x MENU x-x
1. Assign Basic Values to all Team Members
2. Update information of any batsman
3. Display information of any batsman
4. Quit
2

Enter First-Name Of Batsman

Sachin

Batsman FOUND
Details Of Batsman are as follows
FIRST NAME:
Sachin
LAST NAME:
Tendulkar
Runs Made:
185
FOURS: 6
SIXES: 4
Enter Runs To be Updated (7 for out)1
Enter Runs To be Updated (7 for out)1
Enter Runs To be Updated (7 for out)1
Enter Runs To be Updated (7 for out)2
Enter Runs To be Updated (7 for out)12
* Invalid Runs Enter Again
Enter Runs To be Updated (7 for out)1
Enter Runs To be Updated (7 for out)2
Enter Runs To be Updated (7 for out)4
Enter Runs To be Updated (7 for out)6

Computer Science Assignment

Enter Runs To be Updated (7 for out)4


Enter Runs To be Updated (7 for out)6
Enter Runs To be Updated (7 for out)7
----------------------------------------------------------------------------Batsman Information after Update
Details Of Batsman are as follows
FIRST NAME:
Sachin
LAST NAME:
Tendulkar
Runs Made:
213
FOURS: 8
SIXES: 6

x-x MENU x-x


1. Assign Basic Values to all Team Members
2. Update information of any batsman
3. Display information of any batsman
4. Quit
3
----------------------------------------------------------------------------Enter First-Name Of Batsman Sachin
Batsman FOUND
Details Of Batsman are as follows
FIRST NAME:
Sachin
LAST NAME:
Tendulkar
Runs Made:
213
FOURS: 8
SIXES: 6

1.
2.
3.
4.

x-x MENU x-x


Assign Basic Values to all Team Members
Update information of any batsman
Display information of any batsman
Quit
4

Confirm Exit(Y/N)?? y
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=
Computer Science Assignment

ASSIGNMENT - 6
Imagine a publishing company that markets both books and audio cassette versions of its works. Create a
class Publication that stores the title (a string) and price (type float) of a publication. From this class
derive two classes : book, which adds a page count (type int); and tape, which adds a playing time in
minutes (type float). Each of these three classes should have a getdata() function to get its data from the
user at the keyboard, and a putdata() function to display its data.
Write a main() program to test the book and tape classes by creating instances of them asking the user to
fill in their data with getdata(), and then displaying the data with putdata().
Test classes book and tape from main function. Implement the concept of member function over-riding.

====================
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Publication
{
private:
char Title[20];
float Price;
public:
void Getdata()
{
cout<<"\t\t Title: ";
gets(Title);
cout<<"\t\t Price: ";
cin>>Price;
}
void Putdata()
{
cout<<"\n\t\tTitle: "<<Title
<<"\n\t\tPrice: "<<Price;
}
};
class Book: public Publication
{
private:
int Page_Count;
public:
void Getdata()
{
cout<<"\n Enter Book Details \n";
Publication::Getdata();
cout<<"Enter No. Of Pages: ";
cin>>Page_Count;
}
void Putdata()
Computer Science Assignment

{
gotoxy(0,10);
cout<<"\n\n\t\t BOOK DETAILS";
Publication::Putdata();
cout<<"\nNo. Of Pages: "<<Page_Count;
}
};
class Tape: public Publication
{
private:
float Play_Time;
public:
void Getdata()
{
cout<<"\n Enter Tape Details \n";
Publication::Getdata();
cout<<"Enter Playing Time: ";
cin>>Play_Time;
}
void Putdata()
{
cout<<" \n\t\t TAPE DETAILS";
Publication::Putdata();
cout<<"\nPlaying Time: "<<Play_Time;
}
};
void main()
{
clrscr();
Book bks;
Tape Tps;
bks.Getdata();
Tps.Getdata();
clrscr();
bks.Putdata();
Tps.Putdata();
getch();
}

====================
OUTPUT
Enter Book Details
Title: 3 Mistakes Of Life
Price: 250
Enter No. Of Pages: 475
Enter Tape Details
Computer Science Assignment

Title: Lakshya
Price: 75
Enter Playing Time: 26
-----------------------------------------------------------------------------

BOOK DETAILS
Title: 3 Mistakes Of Life
Price: 250
No. Of Pages: 475
TAPE DETAILS
Title: Lakshya
Price: 75
Playing Time: 26

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 7
Write a menu-driven program in C++ with following options1.
Creation of a text file (file must be created, character by character until a particular character is
typed , like as you type # the file creation task gets stopped)
2.
Reading of a created text file. (To display the contents of a text file)
3.
Quit.

====================
#include<fstream.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char ch,filename[13];
int choice;
main_menu:
ch=0;
clrscr();
gotoxy(5,3);
cout<<"* < - MAIN MENU - > *\n"
<<"\n1. Create Text File"
<<"\n2. Read A Text File"
<<"\n3. Exit"
<<"\nEnter Your Choice: ";
cin>>choice;
switch(choice)
{
case 1: clrscr();
cout<<"Enter Filename Of Text File: ";
gets(filename);
ofstream fout(filename);
cout<<"Enter The Data In File(Enter '|' To End Typing): \n";
while(ch!='|')
{
cin.get(ch);
fout<<ch;
}
fout.close();
goto main_menu;
case 2: clrscr();
cout<<"Enter Filename Of File To Be Read: ";
gets(filename);
ifstream fin(filename);
if(!fin)
{
cout<<"Error";
Computer Science Assignment

return;
}
cout<<"The File Contents Are\n";
fin.get(ch);
while(ch!='|')
{
cout<<ch;
fin.get(ch);
}
getch();
fin.close();
goto main_menu;
case 3: gotoxy(15,10);
cout<<"Exitting\n";
for(int i=0;i<15;++i)
{
cout<<"-|-";
delay(500);
}
break;
default: cout<<"Enter Correct Option";
}
}

====================
OUTPUT
* < - MAIN MENU - > *
1. Create Text File
2. Read A Text File
3. Exit
Enter Your Choice: 1
----------------------------------------------------------------------------Enter Filename Of Text File: Gaurav
Enter The Data In File(Enter '|' To End Typing):
Hi!!
Welcome!!
this booklet consists of 20 ASSIGNMENTs..
|
----------------------------------------------------------------------------* < - MAIN MENU - > *
1. Create Text File
2. Read A Text File
3. Exit
Computer Science Assignment

Enter Your Choice: 2


----------------------------------------------------------------------------Enter Filename Of File To Be Read: Gaurav
The File Contents Are
Hi!!
Welcome!!
this booklet consists of 20 ASSIGNMENTs..
-----------------------------------------------------------------------------

* < - MAIN MENU - > *


1. Create Text File
2. Read A Text File
3. Exit
Enter Your Choice: 4
Enter Correct Option
-----------------------------------------------------------------------------

* < - MAIN MENU - > *


1. Create Text File
2. Read A Text File
3. Exit
Enter Your Choice: 3
Exitting

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 8
Write a menu-driven program in C++ with following options1.
Creation of a text file (say file is mixed.txt, it contains alphabets, digits or special characters)
2.
Make separate files (This option creates three files- alpha.txt (contains alphabets only), digit.txt
(contains digits only) and special.txt (contains special characters only) taking data from mixed.txt)
3.
Display a file (This option displays the contents of a particular file out of above-mentioned four
files, by asking its name, if not found reports file not found)
4.
Quit

====================
#include<fstream.h>
#include<dos.h>
#include<conio.h>
void main()
{
int choice,opt;
char ch,filename[15];
ofstream fout,fout1,fout2;
{ main_menu:
clrscr();
cout<<"\t\t\t\tMAIN MENU"
<<"\n\t1. Create Text File"
<<"\n\t2. Make Separate Files"
<<"\n\t3. Display File"
<<"\n\t4. Exit"
<<"\n\n\t\tEnter Your Choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1: clrscr();
cout<<"\n\n\t";
fout.open("MIXED.TXT");
cout<<"Enter The Text(To End Typing Put A '|' At End): ";
do
{
cin.get(ch);
fout<<ch;
}while(ch!='|');
fout.close();
goto main_menu;
case 2: clrscr();
gotoxy(25,10);
cout<<"PROCESSING";
for(int i=0;i<3;i++)
{
for(int j=0;j<5;++j)
Computer Science Assignment

{
gotoxy(35+j,10);
cout<<". ";
delay(500);
}
gotoxy(35,10);
cout<<"\t\t\t";
}
fout.open("ALPHA.TXT");
fout1.open("DIGIT.TXT");
fout2.open("SPECIAL.TXT");
ifstream fin("MIXED.TXT");
fin.get(ch);
while(ch!='|')
{
if(ch>='0'&&ch<='9')
fout1<<ch;
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
fout<<ch;
else
fout2<<ch;
fin.get(ch);
}
fout<<"|";
fout1<<"|";
fout2<<"|";
fout.close();
fout1.close();
fout2.close();
fin.close();
goto main_menu;
case 3: menu:
clrscr();
cout<<"\n\n\t";
cout<<"The Files Available Are "
<<"\n1. MIXED.TXT"
<<"\n2. ALPHA.TXT"
<<"\n3. DIGIT.TXT"
<<"\n4. SPECIAL.TXT"
<<"\n5. Back To MAIN MENU"
<<"\nEnter File to Be Printed: ";
cin>>opt;
switch(opt)
{
case 1:ifstream fin1("MIXED.TXT");
fin1>>ch;
while(ch!='|')
{
cout<<ch;
fin1>>ch;
Computer Science Assignment

}
getch();
fin1.close();
goto menu;
case 2:ifstream fin2("ALPHA.TXT");
fin2>>ch;
while(ch!='|')
{
cout<<ch;
fin2>>ch;
}
getch();
fin2.close();
goto menu;
case 3:ifstream fin3("DIGIT.TXT");
fin3>>ch;
while(ch!='|')
{
cout<<ch;
fin3>>ch;
}
getch();
fin3.close();
goto menu;
case 4:ifstream fin4("SPECIAL.TXT");
fin4>>ch;
while(ch!='|')
{
cout<<ch;
fin4>>ch;
}
getch();
fin4.close();
goto menu;
case 5:goto main_menu;
default : cout<<"\n INAVALID CHOICE";
goto menu;
}
case 4: gotoxy(25,10);
cout<<"Exitting\n\t";
for(int m=10;m<20;++m)
{
cout<<" ! ";
delay(500);
}
}
}
}

====================
Computer Science Assignment

OUTPUT
1.
2.
3.
4.

MAIN MENU
Create Text File
Make Separate Files
Display File
Exit

Enter Your Choice(1-4): 1


----------------------------------------------------------------------------Enter The Text(To End Typing Put A '|' At End): HI!!!
Welcome to C++ Assignment BOOKLEt.
Prepared For The CBSE PRACTICAL EXAM FOR THE SESSION -2012-2013..
Thank You..!!
|
----------------------------------------------------------------------------MAIN MENU
1. Create Text File
2. Make Separate Files
3. Display File
4. Exit
Enter Your Choice(1-4): 2
----------------------------------------------------------------------------PROCESSING...
----------------------------------------------------------------------------PROCESSING.....
----------------------------------------------------------------------------MAIN MENU
1. Create Text File
2. Make Separate Files
3. Display File
4. Exit
Enter Your Choice(1-4): 3
----------------------------------------------------------------------------The Files Available Are
1. MIXED.TXT
2. ALPHA.TXT
3. DIGIT.TXT
4. SPECIAL.TXT
5. Back To MAIN MENU
Enter File to Be Printed: 1
-----------------------------------------------------------------------------

The Files Available Are


1. MIXED.TXT
2. ALPHA.TXT
3. DIGIT.TXT
Computer Science Assignment

4. SPECIAL.TXT
5. Back To MAIN MENU
Enter File to Be Printed: 2
HIWelcometoCAssignmentBOOKLEtPreparedForTheCBSEPRACTICALEXAMFORTHE
SESSIONThankYo
u
----------------------------------------------------------------------------The Files Available Are
1. MIXED.TXT
2. ALPHA.TXT
3. DIGIT.TXT
4. SPECIAL.TXT
5. Back To MAIN MENU
Enter File to Be Printed: 3
20122013
----------------------------------------------------------------------------The Files Available Are
1. MIXED.TXT
2. ALPHA.TXT
3. DIGIT.TXT
4. SPECIAL.TXT
5. Back To MAIN MENU
Enter File to Be Printed: 4
!!!++.--....!!
-----------------------------------------------------------------------------

1.
2.
3.
4.

MAIN MENU
Create Text File
Make Separate Files
Display File
Exit

Enter Your Choice(1-4): 4


-----------------------------------------------------------------------------

1.
2.
3.
4.

MAIN MENU
Create Text File
Make Separate Files
Display File
Exit
Enter Your Choice(1-4): 4
Exitting

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 9
Write a menu-driven program in C++ with following options1.
To add records (Name, Rollno, Marks ) of students (Add records till the user wants yes)
2.
Display all the records
3.
Display a particular record on the basis of Roll No.
4. Quit

====================
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
class Student
{
char Name[20];
int RollNo;
int Marks;
public:
void Display();
void Input();
int ret_rollno()
{
return RollNo;
}
};
void Student::Display()
{
cout<<"\n Name : "<<Name
<<"\n Roll No.: "<<RollNo
<<"\n Marks : "<<Marks;
}
void Student::Input()
{
cout<<" Enter Name: ";
gets(Name);
cout<<" Enter Roll No.: ";
cin>>RollNo;
cout<<" Enter Marks: ";
cin>>Marks;
}
void main()
{
int ch,i=0,j;
Student Obj;
Computer Science Assignment

fstream file("student.dat",ios::out|ios::in);
if(!file)
cout<<"\nCANNOT LINK TO FILE";
do
{ menu:
j=0;
clrscr();
cout<<"\t\t\t\tMain Menu"
<<"\n\t1. Add Records "
<<"\n\t2. Display All Records"
<<"\n\t3. Display Particular Record"
<<"\n\t4. Quit"
<<"\n\n\t\tEnter Your Choice:\t ";
cin>>ch;
switch(ch)
{
case 1: char choice;
do
{ i++;
cout<<"\n\tEnter Record ->"<<i<<"\n";
Obj.Input();
file.write((char*)&Obj,sizeof(Obj));
cout<<"\nEnter Another Record(y/n): ";
cin>>choice;
}while(choice=='Y'||choice=='y');
break;
case 2: file.seekp(0);
while(j<i)
{
j++;
file.read((char*)&Obj,sizeof(Obj));
cout<<"\n\nRecord -"<<j<<"\n";
Obj.Display();
getch();
}
j=0;
break;
case 3: file.seekp(0);
int Roll;
j=0;
clrscr();
cout<<"\n\n\nEnter Roll No. of Record To Be Viewed: ";
cin>>Roll;
while(j<i)
{
file.read((char*)&Obj,sizeof(Obj));
++j;
if(Obj.ret_rollno()==Roll)
{
Computer Science Assignment

Obj.Display();
getch();
goto menu;
}
}
cout<<"Record Is Not There in List";
getch();
goto menu;
case 4: gotoxy(15,10);
cout<<"SAVING DATA & QUITING\n\t";
for(int m=10;m<20;++m)
{
cout<<" | ";
delay(500);
}
}
}while(ch!=4);
file.close();
}

====================
OUTPUT
Main Menu
1.
2.
3.
4.

Add Records
Display All Records
Display Particular Record
Quit
Enter Your Choice:

Enter Record ->1


Enter Name:
Gaurav Arora
Enter Roll No.: 1
Enter Marks: 90
Enter Another Record(y/n): Y
Enter Record ->2
Enter Name:
Aman Arora
Enter Roll No.: 2
Enter Marks: 75
Enter Another Record(y/n): Y
Enter Record ->3
Enter Name:
Mukul Arora
Enter Roll No.: 3
Computer Science Assignment

Enter Marks:

99

Enter Another Record(y/n): Y


Enter Record ->4
Enter Name:
Deeoak Arora
Enter Roll No.: 4
Enter Marks: 85
Enter Another Record(y/n): Y
Enter Record ->5
Enter Name:
Bharat Arora
Enter Roll No.: 5
Enter Marks: 97
Enter Another Record(y/n): N
-----------------------------------------------------------------------------

----------------------------------------------------------------------------Main Menu
1. Add Records
2. Display All Records
3. Display Particular Record
4. Quit
Enter Your Choice:

Record -1
Name : Gaurav Arora
Roll No.: 1
Marks : 90
Record -2
Name : Aman Arora
Roll No.: 2
Marks : 75
Record -3
Name : Mukul Arora
Roll No.: 3
Marks : 99
Record -4

Computer Science Assignment

Name : Deeoak Arora


Roll No.: 4
Marks : 85
Record -5
Name : Bharat Arora
Roll No.: 5
Marks : 97
----------------------------------------------------------------------------Main Menu
1.
2.
3.
4.

Add Records
Display All Records
Display Particular Record
Quit

Enter Your Choice:


3
----------------------------------------------------------------------------Enter Roll No. of Record To Be Viewed: 1
Name : Gaurav Arora
Roll No.: 1
Marks : 90
----------------------------------------------------------------------------Main Menu
1. Add Records
2. Display All Records
3. Display Particular Record
4. Quit
Enter Your Choice:

SAVING DATA & QUITING


| | | | | | | | | |

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 10
Write a program in C++ with the help of following class employee
Member Data :
Employee name
Employee code
Basic Salary
Da
Hra
Total salary
Member Functions:
1. To input employee detail (name, code and basic)
2. To calculate following
Code Da
Hra
1
50% of basic 30% of basic
2
40% of basic 25% of basic
Any other
30% of basic 20% of basic
Total Salary=Basic + Da + Hra
This function must be a private function and must be called from input detail function
3. A function which returns the address of that object which has got higher basic salary
(This function compares calling object and passing object)
4. To display the detail of the student
In main() function make 10 objects of employee. Input and calculate detail of every employee .
Finally display the detail of that employee who draws the highest salary.

====================
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Employee
{
private:
char Emp_Name[20];
int Emp_Code;
double Basic;
double DA;
double HRA;
double Tot_Salary;
void Calculate();
public:
Employee()
{
Tot_Salary = 0;
}
void Input();
Computer Science Assignment

Employee Compare(Employee);
void Display();
double Return()
{
return Tot_Salary;
}
};
void Employee::Calculate()
{
switch(Emp_Code)
{
case 1: DA = Basic * 0.5;
HRA = Basic * 0.3;
break;
case 2: DA = Basic * 0.4;
HRA = Basic * 0.25;
break;
default: DA = Basic * 0.3;
HRA = Basic * 0.2;
}
Tot_Salary = Basic + DA + HRA;
}
void Employee::Input()
{
getch();
gotoxy(0,10);
cout<<"\nEnter Employee's Name: ";
gets(Emp_Name);
cout<<"\nEnter Employee's Code: ";
cin>>Emp_Code;
cout<<"\nEnter The Basic Salary: ";
cin>>Basic;
this->Calculate();
}
Employee Employee::Compare(Employee Obj)
{
if(Tot_Salary>Obj.Return())
return (*this);
else
return Obj;
}
void Employee::Display()
{
clrscr();
cout<<"\n\nEmloyee's Name: \t"<<Emp_Name
Computer Science Assignment

<<"\nEmloyee's Code:\t\t "<<Emp_Code


<<"\nBasic:\t\t "<<Basic
<<"\nDA:\t\t "<<DA
<<"\nHRA:\t\t "<<HRA
<<"\nTotal Salary:\t\t "<<Tot_Salary;
}
void main()
{ const int size=100;
int n;
clrscr();
Employee Emp[size], Highest;
cout<<"\n\n\n\t\t Enter No. Of EMPLOYEES (MAX.100)\t\t:";
do
{cin>>n;}
while(n>100);
cout<<"\nEnter Details Of"<<n<<" Employees: \n";
for(int i=0;i<n;i++)
{
gotoxy(0,9);
cout<<"Enter Record #"<<i+1;
Emp[i].Input();
clrscr();
}
for(i=0;i<n;i++)
Highest=Emp[i].Compare(Highest);
cout<<"\n\nThe Employee Having Highest Salary Is ";
Highest.Display();
getch();
}

====================
OUTPUT
----------------------------------------------------------------------------Enter No. Of EMPLOYEES (MAX.100)
----------------------------------------------------------------------------Enter No. Of EMPLOYEES (MAX.100)
Enter Details Of8 Employees:
Enter Record #1
Enter Employee's Name: Gaurav
Enter Employee's Code: 1
Enter The Basic Salary: 100000
----------------------------------------------------------------------------Enter Record #2
Enter Employee's Name: Mukul
Computer Science Assignment

:6
:6

Enter Employee's Code: 2


Enter The Basic Salary: 50000
----------------------------------------------------------------------------Enter Record #3
Enter Employee's Name: Rishabh
Enter Employee's Code: 3
Enter The Basic Salary: 75000
----------------------------------------------------------------------------Enter Record #4
Enter Employee's Name: Saurabh
Enter Employee's Code: 1
Enter The Basic Salary: 60000
----------------------------------------------------------------------------Enter Record #5
Enter Employee's Name: Mohit
Enter Employee's Code: 1
Enter The Basic Salary: 8
----------------------------------------------------------------------------Enter Record #6
Enter Employee's Name: Aman
Enter Employee's Code: 1
Enter The Basic Salary: 11000
-----------------------------------------------------------------------------

Emloyee's Name:
Aman
Emloyee's Code:
1
Basic:
110000
DA:
55000
HRA:
33000
Total Salary:
198000
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 11
Write a program in C++ with the help of class array_search with following membersMember data :
An array ar of integer with 100 elements
No .of elements actually used by user (int n)
Member Functions1.
To input n, and n number of elements
2.
Linear serch
3.
Binary search (If array is in ascending order)
4.
To display n elements of the array
Make a menu in C++ with following options1.
Array Input
2.
Linear Search
3.
Binary Search
4.
Array display
6.
Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
class Array_Search
{
int arr[100];
unsigned int n;
public:
void Input();
void Linear_search(int);
void Binary_search(int);
void Display();
};
void Array_Search::Input()
{
int i;
A:
clrscr();
cout<<"\t\tEnter NO Of ELEMENTS FOR Array \t ";
cin>>n;
if(n>100)
{
Computer Science Assignment

cout<<"\n INVALID ENTRY ";


getch();
cout<<"\n\t Enter Again";
getch();
goto A;
}
cout<<"\tEnter ELEMENTS of Array:\n";
for(i=0;i<n;i++)
{ gotoxy(5*(i%5)+10,i/5+3);
cin>>arr[i];
}
}
void Array_Search::Linear_search(int item)
{
for(int i=0;i<n;i++)
if(arr[i]==item)
{
cout<<"The Item Is At Position: "<<i+1;
return;
}
cout<<"The Item Is Not Present In The List";
}
void Array_Search::Binary_search(int item)
{
for(int k=0;k<n;++k)
if(arr[k]>arr[k+1])
{cout<<"\n ARRAY IS NOT IN ASCENDING ORDER NINARUY
SEARCH NOT APPLICABLE";
return;
}
int beg=0,end=n-1,mid;
while(beg<=end)
{
mid=(beg+end)/2;
if(arr[mid]==item)
{
cout<<"The Element Is At Position "<<mid+1;
return;
}
else if(arr[mid]>item)
Computer Science Assignment

end=mid-1;
else beg=mid+1;
}
cout<<"The Element Is Not In The List";
}
void Array_Search::Display()
{
cout<<"The Array Is ";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
void main()
{
Array_Search obj;
int ch,item;
do
{
clrscr();
cout<<"\t\t\t\tMain Menu"
<<"\n\t1. Array Input"
<<"\n\t2. Linear Search"
<<"\n\t3. Binary Search"
<<"\n\t4. Array Display"
<<"\n\t5. Exit"
<<"\n\t\tInput Your Choice: ";
cin>>ch;
switch(ch)
{
case 1: obj.Input();
getch();
break;
case 2: cout<<"Enter item To Be Searched: ";
cin>>item;
obj.Linear_search(item);
getch();
break;
case 3: cout<<"Enter item To Be Searched: ";
cin>>item;
Computer Science Assignment

obj.Binary_search(item);
getch();
break;
case 4: obj.Display();
getch();
break;
case 5: cout<<"\t\tEXITING \n\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
}
}while(ch!=5);
}

====================
OUTPUT
Main Menu
Array Input
Linear Search
Binary Search
Array Display
Exit
Input Your Choice: 1
----------------------------------------------------------------------------Enter NO Of ELEMENTS FOR Array 10
----------------------------------------------------------------------------Enter NO Of ELEMENTS FOR Array 10
Enter ELEMENTS of Array:
1 2 3 4 5
9 8 7 4 25
----------------------------------------------------------------------------Main Menu
1. Array Input
2. Linear Search
3. Binary Search
4. Array Display
5. Exit
Input Your Choice: 2
Enter item To Be Searched: 10
1.
2.
3.
4.
5.

Computer Science Assignment

The Item Is Not Present In The List


----------------------------------------------------------------------------Main Menu
1. Array Input
2. Linear Search
3. Binary Search
4. Array Display
5. Exit
Input Your Choice: 2
Enter item To Be Searched: 25
The Item Is At Position: 10
----------------------------------------------------------------------------Main Menu
1. Array Input
2. Linear Search
3. Binary Search
4. Array Display
5. Exit
Input Your Choice: 3
Enter item To Be Searched: 12
ARRAY IS NOT IN ASCENDING ORDER NINARUY SEARCH NOT APPLICABLE
----------------------------------------------------------------------------Main Menu
1. Array Input
2. Linear Search
3. Binary Search
4. Array Display
5. Exit
Input Your Choice: 4
The Array Is 1 2 3 4 5 9 8 7 4 25
-----------------------------------------------------------------------------

Main Menu
1. Array Input
2. Linear Search
3. Binary Search
4. Array Display
5. Exit
Input Your Choice: 5
EXITING
SHUTTING DOWN PROGRAM . . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 12
Write a program in C++ with the help of class array_update with following membersMember data :
An array ar of integer with 100 elements
No .of elements actually used by user (int n)
Member Functions1.
To input n, and n number of elements
2.
To insert an item in the array if array is in ascending order
4.
To delete an item from the array
5.
To display n elements of the array
Make a menu in C++ with following options1.
Array Input
2.
Insertion
3.
Deletion
4.
Array display
6.
Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
class Array_Update
{
private:
int *Arr, N;
public:
void Input();
void Insertion();
void Deletion();
void Display();
};
void Array_Update::Input()
{
cout<<"\n\n\t\tEnter Size Of Array: ";
cin>>N;
Arr=new int[N];
clrscr();
cout<<"Enter Array: \n";
for(int i=0;i<N;i++)
{ gotoxy(3*(i%5)+5,i/5+3);
Computer Science Assignment

cin>>Arr[i];
}

void Array_Update::Insertion()
{
int pos,item;
cout<<"\n\n\t\tEnter The Element To Be Inserted: ";
cin>>item;
for(int m=0;m<N-1;++m)
if(Arr[m]>Arr[m+1])
{
cout<<"\n ARRAY IS NOT IN ASCENDING ORDER !!"
<<"\n So, Element is BEING INSERTED To The End Of ARRAY";
Arr[N]=item;
++N;
getch();
return;
}
if(Arr[0]>item)
pos=0;
else
for(int i=0;i<N-1;i++)
if(Arr[i]<=item && Arr[i+1]>item)
{
pos=i+1;
cout<<"\n ITEM INSERTED";
break;
}
else
pos=N-1;
for(i=N;i>pos;i--)
Arr[i]=Arr[i-1];
Arr[pos]=item;
N++;
}
void Array_Update::Deletion()
{
int pos=-1,item;
cout<<"Enter The Element To Be Deleted: ";
cin>>item;
Computer Science Assignment

for(int i=0;i<N;i++)
if(Arr[i]==item)
pos=i;
if(pos==-1)
{
cout<<"Element Does Not Exist In The List";
getch();
return;
}
else
for(i=pos;i<N-1;i++)
Arr[i]=Arr[i+1];
cout<<"\n Element Found & Deleted !!";
getch();
Arr[N-1]=0;
N--;
}
void Array_Update::Display()
{
cout<<"\n\n\tThe Array Is\n";
for(int i=0;i<N;i++)
cout<<"\t"<<Arr[i];
getch();
}
void main()
{
Array_Update Obj;
int choice;
do
{
clrscr();
cout<<"\t\t\t\tMAIN MENU"
<<"\n1. Array Input"
<<"\n2. Insertion"
<<"\n3. Deletion"
<<"\n4. Array Display"
<<"\n5. Quit"
<<"\nEnter Your Choice(1-5): ";
cin>>choice;
switch(choice)
Computer Science Assignment

{
case 1: clrscr();
Obj.Input();
break;
case 2: clrscr();
Obj.Insertion();
break;
case 3: clrscr();
Obj.Deletion();
break;
case 4: clrscr();
Obj.Display();
break;
case 5: cout<<"\t\tEXITING \n\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
}
}while(choice!=5);
}

====================
OUTPUT
MAIN MENU
1. Array Input
2. Insertion
3. Deletion
4. Array Display
5. Quit
Enter Your Choice(1-5): 1
----------------------------------------------------------------------------Enter Size Of Array: 12
----------------------------------------------------------------------------Enter Array:
1

11

13
Computer Science Assignment

18 21 19 23 25
27 30
----------------------------------------------------------------------------MAIN MENU
1. Array Input
2. Insertion
3. Deletion
4. Array Display
5. Quit
Enter Your Choice(1-5): 2
----------------------------------------------------------------------------Enter The Element To Be Inserted: 7
ARRAY IS NOT IN ASCENDING ORDER !!
So, Element is BEING INSERTED To The End Of ARRAY
----------------------------------------------------------------------------Enter The Element To Be Deleted: 30
Element Found & Deleted !!
----------------------------------------------------------------------------MAIN MENU
1. Array Input
2. Insertion
3. Deletion
4. Array Display
5. Quit
Enter Your Choice(1-5): 4
----------------------------------------------------------------------------The Array Is
1
5
9
11
13
18
19
21
23
25
27
7
----------------------------------------------------------------------------MAIN MENU
1. Array Input
2. Insertion
3. Deletion
4. Array Display
5. Quit
Enter Your Choice(1-5): 5
EXITING
SHUTTING DOWN PROGRAM . . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 14
Write a program in C++ with the help of class array_merge with following membersMember data :
An array a of integer with 100 elements
No .of elements actually used by user in array a (int l)
An array b of integer with 100 elements
No .of elements actually used by user in array b (int m)
An array c of integer with 200 elements
No .of elements actually used by user in array c (int n)
Member Functions1.
To input array a and b with size l, m respectively
2.
merge1(), it merges array a and b to form c, if a and b are sorted in ascending and
descending order respectively
3.
merge2(), it merges array a and b to form c, if a and b are sorted in descending and ascending
order respectively
4.
merge3(), it merges array a and b to form c, if a and b are sorted in ascending order
5.
merge4(), it merges array a and b to form c, if a and b are sorted in descending order
(Note :Array c is required in ascending order.)
6.
display(), To display all the arrays a, b and c.
Make a menu in C++ with following options1.
To input array a, b
2.
To merge array a and b to form c, if a and b are sorted in ascending and descending order
respectively
3.
To merge array a and b to form c, if a and b are sorted in descending and ascending order
respectively
4.
To merge array a and b to form c, if a and b are sorted in ascending order
5.
To merge array a and b to form c, if a and b are sorted in descending order
6.
To display all the arrays.
7. Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
class Array_Merge
{
private:
int A[100], L, B[100], M, C[100], N;
public:
Array_Merge()
{ L=M=N=0; }
void Input();
void Merge1();
void Merge2();
void Merge3();
Computer Science Assignment

void Merge4();
void Display();
};
int IsSorted(int a[100],int n);
void Array_Merge::Input()
{ int i;
A:
clrscr();
cout<<"\t\tEnter Size Of Array A:\t ";
cin>>L;
if(L>100)
{
cout<<"\n INVALID ENTRY ";
getch();
cout<<"\n\t Enter Again";
getch();
goto A;
}
cout<<"\tEnter Array A:\n";
for(i=0;i<L;i++)
{ gotoxy(5*(i%5)+10,i/5+3);
cin>>A[i];
}
int FlagA = IsSorted(A,L);
switch(FlagA)
{
case -1:cout<<"\n The ARRAY is Entered In DESCENDING ORDER" ;
getch();
break;
case 0:cout<<"\n The Array is Entered In UN-SORTED ORDER" ;
getch();
break;
case 1:cout<<"\n The Array is Entered In ASCENDING ORDER" ;
getch();
break;
}
B:
clrscr();
cout<<"\t\tEnter Size Of Array B:\t ";
cin>>M;
if(M>100)
{
cout<<"\n\t INVALID ENTRY ";
getch();
cout<<"\n\t Enter Again";
getch();
goto B;
}
cout<<"\tEnter Array B:\n";
for(i=0;i<M;i++)
Computer Science Assignment

{ gotoxy(5*(i%5)+10,i/5+3);
cin>>B[i];
}
int FlagB = IsSorted(B,L);
switch(FlagB)
{
case -1:cout<<"\n The ARRAY is Entered In DESCENDING ORDER" ;
getch();
break;
case 0:cout<<"\n The Array is Entered In UN-SORTED ORDER" ;
getch();
break;
case 1:cout<<"\n The Array is Entered In ASCENDING ORDER" ;
getch();
break;
}
}
int IsSorted(int a[100],int n)
{
int flag=-1,i;
for(i=0;i<n-1;i++)
{
if((a[i]>=a[i+1])&&(flag))
flag=-1;
else
{ flag=0;
break; }
}
if(flag)
return flag;
else
{
flag=1;
for(i=0;i<n-1;i++)
{
if((a[i]<=a[i+1])&&(flag))
flag=1;
else
{
flag=0;
break; }
}
}
return flag;
}
void Array_Merge::Merge1()
{
int FlagA = IsSorted(A,L);
int FlagB = IsSorted(B,M);
int a=0,b=M-1,c=0;
if(FlagA==1 &&FlagB==-1)
Computer Science Assignment

{
while(a<L && b>=0)
if(A[a]<B[b])
C[c++]=A[a++];
else
C[c++]=B[b--];
while(a<L)
C[c++]=A[a++];
while(b>=0)
C[c++]=B[b--];
N=L+M;
clrscr();
gotoxy(20,10);
cout<<"MERGING ";
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Task Accomplished";
getch();
}
else if(FlagA==0||FlagB==0)
{ cout<<"\n\n\n\t\t THE ARRAYS ARE NOT IN SORTED MANNER";
getch();
return;
}
else
cout<<"\n\n\n\tMERGING ON THIS KIND OF SORTED ARRAY IS NOT POSSIBLE";
getch();
}
void Array_Merge::Merge2()
{
int FlagA = IsSorted(A,L);
int FlagB = IsSorted(B,M);
int a=L-1,b=0,c=0;
if(FlagA==-1&&FlagB==1)
{
while(a>=0 && b<M)
if(A[a]<B[b])
C[c++]=A[a--];
else
C[c++]=B[b++];
while(a>L)
C[c++]=A[a--];
while(b<M)
C[c++]=B[b++];
N=L+M;
clrscr();
Computer Science Assignment

gotoxy(20,10);
cout<<"MERGING ";
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Task Accomplished";
}
else if(FlagA==0||FlagB==0)
{
cout<<"\n\n\n\t\t THE ARRAYS ARE NOT IN SORTED MANNER";
getch();
return;
}
else
cout<<"\n\n\n\tMERGING ON THIS KIND OF SORTED ARRAY IS NOT POSSIBLE";
getch();
}
void Array_Merge::Merge3()
{
int FlagA = IsSorted(A,L);
int FlagB = IsSorted(B,M);
int a=0,b=0,c=0;
if(FlagA==1 &&FlagB==1)
{
while(a<L && b<M)
if(A[a]<B[b])
C[c++]=A[a++];
else
C[c++]=B[b++];
while(a<L)
C[c++]=A[a++];
while(b<M)
C[c++]=B[b++];
N=L+M;
clrscr();
gotoxy(20,10);
cout<<"MERGING ";
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Task Accomplished";
}
else if(FlagA==0||FlagB==0)
{
Computer Science Assignment

cout<<"\n\n\n\t\t THE ARRAYS ARE NOT IN SORTED MANNER";


getch();
return;
}
else
cout<<"\n\n\n\tMERGING ON THIS KIND OF SORTED ARRAY IS NOT POSSIBLE";
getch();
}
void Array_Merge::Merge4()
{
int FlagA = IsSorted(A,L);
int FlagB = IsSorted(B,M);
int a=L-1,b=M-1,c=0;
if(FlagA==-1 &&FlagB==-1)
{
while(a>=0 && b>=0)
if(A[a]<B[b])
C[c++]=A[a--];
else
C[c++]=B[b--];
while(a>=0)
C[c++]=A[a--];
while(b>=0)
C[c++]=B[b--];
N=L+M;
clrscr();
gotoxy(20,10);
cout<<"MERGING ";
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Task Accomplished";
}
else if(FlagA==0||FlagB==0)
{
cout<<"\n\n\n\t\t THE ARRAYS ARE NOT IN SORTED MANNER";
getch();
return;
}
else
cout<<"\n\n\n\tMERGING ON THIS KIND OF SORTED ARRAY IS NOT POSSIBLE";
getch();
}
void Array_Merge::Display()
{ int i;
Computer Science Assignment

cout<<"\nThe Array A Is \n";


for(i=0;i<L;i++)
{
if(i%5==0&&i!=0)
cout<<"\n";
cout<<"\t"<<A[i];
}
getch();
cout<<"\nThe Array B Is \n";
for(i=0;i<M;i++)
{
if(i%5==0&&i!=0)
cout<<"\n";
cout<<"\t"<<B[i];
}
getch();
cout<<"\nThe Array C Is \n";
for(i=0;i<N;i++)
{
if(i%5==0&&i!=0)
cout<<"\n";
cout<<"\t"<<C[i];
}
getch();
}
void main()
{
int choice;
Array_Merge Obj;
do
{
clrscr();
cout<<"\n\n\t\t\t\tMAIN MENU"
<<"\n1. Input Arrays A And B"
<<"\n2. A And B Are Merged To Get C, If A And B Are Sorted In Asc And Dec Order"
<<"\n3. A And B Are Merged To Get C, If A And B Are Sorted In Dec And Asc Order"
<<"\n4. A And B Are Merged To Get C, If A And B Are Both Sorted In Asc Order"
<<"\n5. A And B Are Merged To Get C, If A And B Are Both Sorted In Dec Order"
<<"\n6. Display All Arrays"
<<"\n7. Quit"
<<"\n\n * C is formed in ASCENDING ORDER"
<<"\n\n\tEnter Your Choice(1-7):\t ";
cin>>choice;
switch(choice)
{
case 1: clrscr();
Obj.Input();
break;
case 2: clrscr();
Computer Science Assignment

Obj.Merge1();
break;
case 3: clrscr();
Obj.Merge2();
break;
case 4: clrscr();
Obj.Merge3();
break;
case 5: clrscr();
Obj.Merge4();
break;
case 6: clrscr();
Obj.Display();
break;
case 7: cout<<"\n\n\t\t\tEXITING \n\t\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
default:cout<<"\n INVALID CHOICE"
<<"\n TRY AGAIN" ;
break;
}
}while(choice!=7);
}

====================
OUTPUT
1.
2.
3.
4.
5.
6.
7.

MAIN MENU
Input Arrays A And B
A And B Are Merged To Get C, If
A And B Are Merged To Get C, If
A And B Are Merged To Get C, If
A And B Are Merged To Get C, If
Display All Arrays
Quit

A
A
A
A

And
And
And
And

B
B
B
B

Are
Are
Are
Are

Sorted In Asc And Dec Order


Sorted In Dec And Asc Order
Both Sorted In Asc Order
Both Sorted In Dec Order

* C is formed in ASCENDING ORDER


Enter Your Choice(1-7): 1
----------------------------------------------------------------------------Computer Science Assignment

Enter Size Of Array A:


Enter Array A:
1 5 8 9 11
13 15

The Array is Entered In ASCENDING ORDER


----------------------------------------------------------------------------Enter Size Of Array B: 6
Enter Array B:
15 14 13 12 11
10
The ARRAY is Entered In DESCENDING ORDER
----------------------------------------------------------------------------MAIN MENU
1. Input Arrays A And B
2. A And B Are Merged To Get C, If A And B Are Sorted In Asc And Dec Order
3. A And B Are Merged To Get C, If A And B Are Sorted In Dec And Asc Order
4. A And B Are Merged To Get C, If A And B Are Both Sorted In Asc Order
5. A And B Are Merged To Get C, If A And B Are Both Sorted In Dec Order
6. Display All Arrays
7. Quit
* C is formed in ASCENDING ORDER
Enter Your Choice(1-7): 2
----------------------------------------------------------------------------MERGING . . . . .
Task Accomplished
----------------------------------------------------------------------------MAIN MENU
1. Input Arrays A And B
2. A And B Are Merged To Get C, If A And B Are Sorted In Asc And Dec Order
3. A And B Are Merged To Get C, If A And B Are Sorted In Dec And Asc Order
4. A And B Are Merged To Get C, If A And B Are Both Sorted In Asc Order
5. A And B Are Merged To Get C, If A And B Are Both Sorted In Dec Order
6. Display All Arrays
7. Quit
* C is formed in ASCENDING ORDER
Enter Your Choice(1-7): 4
----------------------------------------------------------------------------MERGING ON THIS KIND OF SORTED ARRAY IS NOT POSSIBLE
----------------------------------------------------------------------------MAIN MENU
1. Input Arrays A And B
2. A And B Are Merged To Get C, If A And B Are Sorted In Asc And Dec Order
3. A And B Are Merged To Get C, If A And B Are Sorted In Dec And Asc Order
Computer Science Assignment

4.
5.
6.
7.

A And B Are Merged To Get C, If A And B Are Both Sorted In Asc Order
A And B Are Merged To Get C, If A And B Are Both Sorted In Dec Order
Display All Arrays
Quit

* C is formed in ASCENDING ORDER


Enter Your Choice(1-7): 6
----------------------------------------------------------------------------The Array A Is
1
5
8
9
11
13 15
The Array B Is
15
14
13
12
11
10
The Array C Is
1
5
8
9
10
11 11 12 13
13
14 15
15
-----------------------------------------------------------------------------

1.
2.
3.
4.
5.
6.
7.

MAIN MENU
Input Arrays A And B
A And B Are Merged To Get C, If
A And B Are Merged To Get C, If
A And B Are Merged To Get C, If
A And B Are Merged To Get C, If
Display All Arrays
Quit

A
A
A
A

And
And
And
And

B
B
B
B

Are
Are
Are
Are

Sorted In Asc And Dec Order


Sorted In Dec And Asc Order
Both Sorted In Asc Order
Both Sorted In Dec Order

* C is formed in ASCENDING ORDER


Enter Your Choice(1-7): 1
----------------------------------------------------------------------------Enter Size Of Array A:
Enter Array A:
1 3 5 9 11

The Array is Entered In ASCENDING ORDER


----------------------------------------------------------------------------Enter Size Of Array B: 5
Enter Array B:
4 8 6 1 5
The Array is Entered In UN-SORTED ORDER
----------------------------------------------------------------------------MAIN MENU
1. Input Arrays A And B
2. A And B Are Merged To Get C, If A And B Are Sorted In Asc And Dec Order
Computer Science Assignment

3.
4.
5.
6.
7.

A And B Are Merged To Get C, If A And B Are Sorted In Dec And Asc Order
A And B Are Merged To Get C, If A And B Are Both Sorted In Asc Order
A And B Are Merged To Get C, If A And B Are Both Sorted In Dec Order
Display All Arrays
Quit

* C is formed in ASCENDING ORDER


Enter Your Choice(1-7): 3
----------------------------------------------------------------------------THE ARRAYS ARE NOT IN SORTED MANNER
----------------------------------------------------------------------------MAIN MENU
1. Input Arrays A And B
2. A And B Are Merged To Get C, If A And B Are Sorted In Asc And Dec Order
3. A And B Are Merged To Get C, If A And B Are Sorted In Dec And Asc Order
4. A And B Are Merged To Get C, If A And B Are Both Sorted In Asc Order
5. A And B Are Merged To Get C, If A And B Are Both Sorted In Dec Order
6. Display All Arrays
7. Quit
* C is formed in ASCENDING ORDER
Enter Your Choice(1-7): 7

EXITING
SHUTTING DOWN PROGRAM . . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 15
Write a program in C++ with the help of class linked_list with following members(Nodes of the linked list are created by self-referential structure nodestruct node
{
int info;
node *next;
};
)
Member data :
A pointer start which keeps the address of first node.
Member Functions1.
A constructor which keeps NULL in start.
2.
To add a node in a linked list
3.
For traversal (Display info. of each node)
4.
To search a particular node, on the basis of information of nodes given.
Make a menu in C++ with following options1.
To create a linked list.
2.
For linked list traversal.
3.
To search a particular node
4.
Quit.

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
struct Node
{
int info;
Node *Next;
}*ptr=NULL;
class Linked_List
{
private:
Node *Start;
public:
Linked_List()
{ Start=NULL; }
void AddNode(int);
void Display();
void Search(int);
};
void Linked_List::AddNode(int n)
{
ptr = new Node;
Computer Science Assignment

ptr->info = n;
ptr->Next = Start;
Start = ptr;
}
void Linked_List::Display()
{
int i=0;
cout<<"\n\n\tThe Contents Of Linked List Are: \n";
ptr = Start;
while(ptr)
{
if(i%5==0&&i!=0)
cout<<"\n";
cout<<"\t"<<ptr->info;
ptr = ptr->Next;
++i;
}
getch();
}
void Linked_List::Search(int n)
{
int i=0;
ptr = Start;
while(ptr)
{ ++i;
if(ptr->info==n)
{
cout<<"\nThe Data Is There In The List @ "<<i<<" Node";
getch();
return;
}
ptr = ptr->Next;
}
cout<<"The Data is Not There In The List";
getch();
}
void main()
{
Linked_List obj;
int choice;
static int k=0;
do
{
clrscr();
cout<<"\t\t\t\tMAIN MENU"
<<"\n1. Create Linked List"
<<"\n2. Linked List"
<<"\n3. Search A Node"
Computer Science Assignment

<<"\n4. Quit"
<<"\nEnter Your Choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1: clrscr();
char ch;
int n;
do
{
k++;
cout<<"Enter Data In Node #"<<k<<": ";
cin>>n;
obj.AddNode(n);
cout<<"Do You Want To Add More Nodes(Y/N): ";
cin>>ch;
}while(ch=='Y'||ch=='y');
break;
case 2: clrscr();
obj.Display();
break;
case 3: clrscr();
cout<<"Enter The Data To Be Searched: ";
cin>>n;
obj.Search(n);
break;
case 4: cout<<"\n\n\t\t\tEXITING \n\t\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
default:cout<<"\n INVALID CHOICE"
<<"\n TRY AGAIN" ;
break;
}
}while(choice!=4);
}

====================
OUTPUT
MAIN MENU
1. Create Linked List
2. Linked List
Computer Science Assignment

3. Search A Node
4. Quit
Enter Your Choice(1-4): 1
----------------------------------------------------------------------------Enter Data In Node #1: 12
Do You Want To Add More Nodes(Y/N): Y
Enter Data In Node #2: 10
Do You Want To Add More Nodes(Y/N): Y
Enter Data In Node #3: 68
Do You Want To Add More Nodes(Y/N): Y
Enter Data In Node #4: 45
Do You Want To Add More Nodes(Y/N): Y
Enter Data In Node #5: 85
Do You Want To Add More Nodes(Y/N): Y
Enter Data In Node #6: 47
Do You Want To Add More Nodes(Y/N): Y
Enter Data In Node #7: 45
Do You Want To Add More Nodes(Y/N): N
----------------------------------------------------------------------------MAIN MENU
1. Create Linked List
2. Linked List
3. Search A Node
4. Quit
Enter Your Choice(1-4): 2
----------------------------------------------------------------------------The Contents Of Linked List Are:
45
47
85
45
68
10
12
----------------------------------------------------------------------------MAIN MENU
1. Create Linked List
2. Linked List
3. Search A Node
4. Quit
Enter Your Choice(1-4): 3
----------------------------------------------------------------------------Enter The Data To Be Searched: 78
The Data is Not There In The List
----------------------------------------------------------------------------Enter The Data To Be Searched: 10
The Data Is There In The List @ 6 Node
----------------------------------------------------------------------------MAIN MENU
1. Create Linked List
2. Linked List
3. Search A Node
4. Quit
Enter Your Choice(1-4): 4
Computer Science Assignment

EXITING
SHUTTING DOWN PROGRAM . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 16
Write a program in C++ with the help of class array_stack with following membersMember data :
An array of integer ar[MAX]
(Where MAX is a globally-declared symbolic-constant (integer) with size according to the requirement of
user)
top (It keeps the index of top most array element)
Member Functions1.
A constructor that initialize 1 in top (Makes array_stack ready to push)
2.
To push an item in stack.
3.
To pop an item from stack
4.
To display the content of stack without popping
Make a menu in C++ with following options1. A initialize the stack( top must be -1)
2. To push an item in stack.
3. To pop an item from stack
4. To display the content of stack without popping
5. Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
const int MAX=500;
class Array_Stack
{
private:
int Stack[MAX];
int Top;
public:
Array_Stack()
{
Top=-1;
}
void Push(int);
void Initialize();
void Pop();
void Display();
};
void Array_Stack::Initialize()
{ int i;
A:
clrscr();
cout<<"\t\tEnter NO Of ELEMENTS FOR STACK_Array \t ";
cin>>Top;
Computer Science Assignment

if(Top>MAX)
{
cout<<"\n INVALID ENTRY ";
getch();
cout<<"\n\t Enter Again";
getch();
goto A;
}
cout<<"\tEnter ELEMENTS of STACK_Array:\n";
for(i=0;i<Top;i++)
{ gotoxy(5*(i%5)+10,i/5+3);
cin>>Stack[i];
}
}
void Array_Stack::Push(int n)
{
clrscr();
gotoxy(20,10);
if(Top==MAX-1)
cout<<" Overflow. . . . . .";
else
{
cout<<"PUSHING ELEMENT "<<n<<" INTO THE STACK";
Stack[Top]=n;
++Top;
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Element PUSHED IN Seccesfully";
}
getch();
}
void Array_Stack::Pop()
{
clrscr();
gotoxy(20,10);
if(Top==-1)
cout<<"Underflow. . . . .";
else
{
cout<<"POPPING ELEMENT "<<Stack[0]<<" OUT OF THE STACK";
--Top;
for(int i=0;i<Top;++i)
Stack[i]=Stack[i+1];
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
Computer Science Assignment

}
cout<<"\n Element POPPED OUT Seccesfully";
}
getch();
}
void Array_Stack::Display()
{
cout<<"The Elements In the Stack Are\n";
for(int j=0;j<Top;++j)
{
if(j%5==0&&j!=0)
cout<<"\n";
cout<<"\t"<<Stack[j];
}
getch();
}
void main()
{
Array_Stack obj;
int choice;
do
{
clrscr();
cout<<"\n\n\t\t\t\tMAIN MENU"
<<"\n1. Initialize"
<<"\n2. Push"
<<"\n3. Pop"
<<"\n4. Display Stack"
<<"\n5. Quit"
<<"\n\nEnter Your Choice(1-5):\t ";
cin>>choice;
switch(choice)
{
case 1:obj.Initialize();
break;
case 2: clrscr();
int n;
cout<<"Enter The Element To Be Pushed: ";
cin>>n;
obj.Push(n);
break;
case 3: clrscr();
obj.Pop();
break;
case 4: clrscr();
obj.Display();
break;
Computer Science Assignment

case 5: cout<<"\n\n\t\t\tEXITING \n\t\t SHUTTING DOWN PROGRAM";


for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
default:cout<<"\n INVALID CHOICE"
<<"\n TRY AGAIN" ;
getch();
break;
}
}while(choice!=5);}

====================
OUTPUT
MAIN MENU
1.
2.
3.
4.
5.

Initialize
Push
Pop
Display Stack
Quit

Enter Your Choice(1-5): 1


----------------------------------------------------------------------------Enter NO Of ELEMENTS FOR STACK_Array
Enter ELEMENTS of STACK_Array:
1
5
8
66
58
4
3
25
21
21
25 31 54
----------------------------------------------------------------------------MAIN MENU
1. Initialize
2. Push
3. Pop
4. Display Stack
5. Quit

13

Enter Your Choice(1-5): 2


----------------------------------------------------------------------------PUSHING ELEMENT 25 INTO THE STACK . . . . .
Element PUSHED IN Seccesfully
----------------------------------------------------------------------------MAIN MENU
1. Initialize
2. Push
Computer Science Assignment

3. Pop
4. Display Stack
5. Quit
Enter Your Choice(1-5): 3
----------------------------------------------------------------------------POPPING ELEMENT 1 OUT OF THE STACK . . . . .
Element POPPED OUT Seccesfully
----------------------------------------------------------------------------MAIN MENU
1. Initialize
2. Push
3. Pop
4. Display Stack
5. Quit
Enter Your Choice(1-5): 4
----------------------------------------------------------------------------The Elements In the Stack Are
5
8
66
58
4
3
25
21
21
25
31
54
25
----------------------------------------------------------------------------MAIN MENU
1.
2.
3.
4.
5.

Initialize
Push
Pop
Display Stack
Quit

Enter Your Choice(1-5): 5

EXITING
SHUTTING DOWN PROGRAM . . . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 17
Write a program in C++ with the help of class linked_stack with following members(Nodes of the linked stack are created by self-referential structure nodestruct node
{
int info;
node *next;
};
)
Member data :
A pointer top which keeps the address of top-most node.
Member Functions1.A constructor that initialize NULL in top (Makes array_stack ready to push)
2. To push an item on stack.
3. To pop an item from stack
4. To display the content of stack without popping
Make a menu in C++ with following options1. A initialize the stack( top must be NULL)
2. To push an item in stack.
3. To pop an item from stack
4. To display the content of stack without popping
5. Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
struct Node
{
int info;
Node *Next;
}*ptr;
class Linked_Stack
{
private:
Node *Top;
public:
Linked_Stack()
{
Top=NULL;
}
void Initialize();
void Push(int);
void Pop();
void Display();
Computer Science Assignment

};
void Linked_Stack::Initialize()
{ int n,x;
clrscr();
cout<<"\t\t Enter No. of Elements ";
cin>>n;
cout<<"\tEnter ELEMENTS of LINKED_STACK:\n";
for(int i=0;i<n;i++)
{ gotoxy(5*(i%5)+10,i/5+3);
cin>>x;
ptr=new Node;
ptr->info=x;
ptr->Next=Top;
Top=ptr;
}
}
void Linked_Stack::Push(int n)
{
clrscr();
gotoxy(20,10);
cout<<"PUSHING ELEMENT "<<n<<" INTO THE STACK";
ptr=new Node;
ptr->info=n;
ptr->Next=Top;
Top=ptr;
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Element PUSHED IN Seccesfully";
getch();
}
void Linked_Stack::Pop()
{
clrscr();
gotoxy(20,10);
if(Top==NULL)
cout<<"Underflow. . . . .";
else
{
cout<<"POPPING ELEMENT "<<Top->info<<" OUT OF THE STACK";
ptr=Top;
Top=Top->Next;
delete ptr;
for(int m=0;m<5;++m)
{
cout<<" . ";
Computer Science Assignment

delay(500);
}
cout<<"\n Element POPPED OUT Seccesfully";
}
getch();
}
void Linked_Stack::Display()
{
int i=0;
cout<<"\n\nThe Contents Of Stack Are\n";
ptr=Top;
while(ptr)
{
if(i%5==0&&i!=0)
cout<<"\n";
cout<<"\t"<<ptr->info;
ptr = ptr->Next;
++i;
}
}
void main()
{
Linked_Stack obj;
int choice;
do
{
clrscr();
cout<<"\t\t\t\tMAIN MENU"
<<"\n1. Initialize Stack"
<<"\n2. Push"
<<"\n3. Pop"
<<"\n4. Display Stack"
<<"\n5. Exit"
<<"\nEnter Your Choice(1-5): ";
cin>>choice;
switch(choice)
{
case 1: obj.Initialize();
break;
case 2: clrscr();
int n;
cout<<"Enter element To Be Added: ";
cin>>n;
obj.Push(n);
break;
case 3: clrscr();
obj.Pop();
break;
Computer Science Assignment

case 4: clrscr();
obj.Display();
getch();
break;
case 5: cout<<"\n\n\t\t\tEXITING \n\t\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
default:cout<<"\n INVALID CHOICE"
<<"\n TRY AGAIN" ;
getch();
break;
}
}while(choice!=5);
}

====================
OUTPUT
MAIN MENU
1. Initialize Stack
2. Push
3. Pop
4. Display Stack
5. Exit
Enter Your Choice(1-5): 1
----------------------------------------------------------------------------Enter No. of Elements 12
Enter ELEMENTS of LINKED_STACK:
1 5 9 13 17
21 25 29 33 37
41 45
----------------------------------------------------------------------------MAIN MENU
1. Initialize Stack
2. Push
3. Pop
4. Display Stack
5. Exit
Enter Your Choice(1-5): 2
----------------------------------------------------------------------------Enter element To Be Added: 58
----------------------------------------------------------------------------Computer Science Assignment

PUSHING ELEMENT 58 INTO THE STACK . . . . .


Element PUSHED IN Seccesfully
----------------------------------------------------------------------------MAIN MENU
1. Initialize Stack
2. Push
3. Pop
4. Display Stack
5. Exit
Enter Your Choice(1-5): 4
----------------------------------------------------------------------------The Contents Of Stack Are
58
45
41
37
33
29
25
21
17
13
9
5
1
----------------------------------------------------------------------------MAIN MENU
1. Initialize Stack
2. Push
3. Pop
4. Display Stack
5. Exit
Enter Your Choice(1-5): 3
----------------------------------------------------------------------------POPPING ELEMENT 58 OUT OF THE STACK . . . . .
Element POPPED OUT Seccesfully
----------------------------------------------------------------------------MAIN MENU
1. Initialize Stack
2. Push
3. Pop
4. Display Stack
5. Exit
Enter Your Choice(1-5): 5

EXITING
SHUTTING DOWN PROGRAM . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

ASSIGNMENT - 18
Write a program in C++ with the help of class array_queue with following membersMember data :
An array of integer ar[MAX]
(Where MAX is a globally-declared symbolic-constant (integer) with size according to the requirement of
user)
front and rear ( They keep the index of front-most and rear-most array element)
Member Functions1. A constructor that initialize 1 in front and rear (Makes array_queue ready for insertion)
2. To insert an item in queue.
3. To delete an item from queue
4. To display the content of queue without deletion.
Make a menu in C++ with following options1. To initialize the queue( front and rear must be -1)
2. To insert an item in queue
3. To delete an item from queue
4. To display the contents of queue without deletion
5. Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
const int MAX=500;
class Array_Queue
{
private:
int Queue[MAX];
int Front,Rear;
public:
Array_Queue()
{ Front = Rear = -1; }
void Enter(int);
void Delete();
void Display();
void Initialize();
};
void Array_Queue::Initialize()
{ int i;
A:
clrscr();
cout<<"\t\tEnter NO Of ELEMENTS FOR STACK_Array \t ";
cin>>Rear;
--Rear;
Front=0;
Computer Science Assignment

if(Rear>MAX)
{
cout<<"\n INVALID ENTRY ";
getch();
cout<<"\n\t Enter Again";
getch();
goto A;
}
cout<<"\tEnter ELEMENTS of Array_Queue:\n";
for(i=0;i<=Rear;i++)
{ gotoxy(5*(i%5)+10,i/5+3);
cin>>Queue[i];
}
}
void Array_Queue::Enter(int n)
{
clrscr();
gotoxy(20,10);
if(Rear==MAX)
{ cout<<" Overflow. . . . . .";
getch();
return;
}
else
{ if(Rear==-1)
Front=Rear=0;
else
Rear++;
}
Queue[Rear]=n;
cout<<"ADDING ELEMENT TO QUEUE";
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\nThe Element "<<n<<" Has Been Added To The Queue";
getch();
}
void Array_Queue::Delete()
{
clrscr();
gotoxy(20,10);
if(Front==-1)
{
cout<<"Underflow. . . . .";
getch();
return;
}
Computer Science Assignment

cout<<"DELETING ELEMENT FROM QUEUE";


for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
if(Front==Rear)
{
cout<<"\nThe Element Being Deleted Is "<<Queue[Front];
getch();
Rear = Front =-1;
}
else
{
cout<<"\nThe Element Being Deleted Is "<<Queue[Front];
getch();
Front++;
}
getch();
}
void Array_Queue::Display()
{
cout<<"\n\n\t\tThe contents Of Queue Are\n\n";
for(int j=0,i=Front;i<=Rear;++j,++i)
{
if(j%5==0&&j!=0)
cout<<"\n";
cout<<"\t"<<Queue[i];
}
getch();
}
void main()
{
Array_Queue obj;
int choice;
do
{
clrscr();
cout<<"\t\t\t\tMAIN MENU"
<<"\n1. Initialize Queue"
<<"\n2. Insert"
<<"\n3. Delete"
<<"\n4. Display Queue"
<<"\n5. Exit"
<<"\nEnter Your Choice(1-5): ";
cin>>choice;
switch(choice)
{
case 1: obj.Initialize();
Computer Science Assignment

break;
case 2: clrscr();
int n;
cout<<"Enter element To Be Added: ";
cin>>n;
obj.Enter(n);
break;
case 3: clrscr();
obj.Delete();
break;
case 4: clrscr();
obj.Display();
getch();
break;
case 5: cout<<"\n\n\t\t\tEXITING \n\t\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
default:cout<<"\n INVALID CHOICE"
<<"\n TRY AGAIN" ;
getch();
break;
}
}while(choice!=5);
}

====================
OUTPUT
MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5): 1
----------------------------------------------------------------------------Enter NO Of ELEMENTS FOR STACK_Array
Enter ELEMENTS of Array_Queue:
54 68 69 58 25
14 25 36
----------------------------------------------------------------------------Computer Science Assignment

MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5): 2
----------------------------------------------------------------------------ADDING ELEMENT TO QUEUE . . . . .
The Element 58 Has Been Added To The Queue
----------------------------------------------------------------------------MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5): 3
----------------------------------------------------------------------------The Element Being Deleted Is 68
----------------------------------------------------------------------------MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5): 4
----------------------------------------------------------------------------The contents Of Queue Are
69
58
25
14
25
36
58
58
----------------------------------------------------------------------------MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5): 5

EXITING
SHUTTING DOWN PROGRAM . . . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=
Computer Science Assignment

ASSIGNMENT - 19
Write a program in C++ with the help of class linked_queue with following members(Nodes of the linked queue are created by self-referential structure nodestruct node
{
int info;
node *next;
};
)
member Data :
front and rear ( They keep the memory address of front-most and rear-most linked queue)
Member Functions1. A constructor that initialize NULL in front and rear (Makes linked__queue ready for insertion)
2. To insert an item in queue.
3. To delete an item from queue
4. To display the content of queue without deletion.
Make a menu in C++ with following options1. To initialize the queue( front and rear must be NULL)
2. To insert an item in queue
3. To delete an item from queue
4. To display the contents of queue without deletion
5. Quit

====================
#include<iostream.h>
#include<conio.h>
#include<dos.h>
struct Node
{
int info;
Node *Next;
}*ptr;
class Linked_Queue
{
private:
Node *Front;
Node *Rear;
public:
Linked_Queue()
{
Front=NULL;
Rear=NULL;
}
void Initialize();
void Insert(int);
Computer Science Assignment

void Delete();
void Display();
};
void Linked_Queue::Initialize()
{ int n,x;
clrscr();
Front=Rear=NULL;
cout<<"\t\t Enter No. of Elements ";
cin>>n;
cout<<"\tEnter ELEMENTS of LINKED_STACK:\n";
for(int i=0;i<n;i++)
{ gotoxy(5*(i%5)+10,i/5+3);
cin>>x;
ptr=new Node;
ptr->info=x;
ptr->Next=NULL;
if(Rear==NULL)
Front=Rear=ptr;
else
{
Rear->Next=ptr;
Rear=ptr;
}
}
}
void Linked_Queue::Insert(int n)
{
clrscr();
gotoxy(20,10);
cout<<"INSERTING ELEMENT "<<n<<" INTO THE QUEUE";
ptr=new Node;
ptr->info=n;
ptr->Next=NULL;
if(Rear==NULL)
Front=Rear=ptr;
else
{
Rear->Next=ptr;
Rear=ptr;
}
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
cout<<"\n Element INSERTED Seccesfully";
getch();
}
void Linked_Queue::Delete()
{
clrscr();
Computer Science Assignment

gotoxy(20,10);
if(Front==NULL)
{
cout<<"Underflow. . . . . .";
getch();
return;
}
cout<<"DELETING ELEMENT FROM QUEUE";
for(int m=0;m<5;++m)
{
cout<<" . ";
delay(500);
}
if(Rear==Front)
{
cout<<"\n Element "<<Front->info<< " Deleted !!";
getch();
ptr=Front;
Front=Rear=NULL;
delete ptr;
}
else
{
cout<<"\n Element"<<Front->info<< " Deleted !!";
getch();
ptr=Front;
Front=Front->Next;
delete ptr;
}
}
void Linked_Queue::Display()
{ int i=0;
cout<<"The Contents Of Queue Are\n";
ptr=Front;
while(ptr)
{
if(i%5==0&&i!=0)
cout<<"\n";
cout<<"\t"<<ptr->info;
ptr = ptr->Next;
++i;
}
getch();
}
void main()
{
Linked_Queue obj;
int choice;
do
{
Computer Science Assignment

clrscr();
cout<<"\t\t\t\tMAIN MENU"
<<"\n1. Initialize Queue"
<<"\n2. Insert"
<<"\n3. Delete"
<<"\n4. Display Queue"
<<"\n5. Exit"
<<"\n\nEnter Your Choice(1-5): \t";
cin>>choice;
switch(choice)
{
case 1: obj.Initialize();
break;
case 2: clrscr();
int n;
cout<<"Enter element To Be Added: ";
cin>>n;
obj.Insert(n);
break;
case 3: clrscr();
obj.Delete();
break;
case 4: clrscr();
obj.Display();
getch();
break;
case 5: cout<<"\n\n\t\t\tEXITING \n\t\t SHUTTING DOWN PROGRAM";
for(int k=0;k<5;++k)
{
cout<<" . ";
delay(500);
}
break;
default:cout<<"\n INVALID CHOICE"
<<"\n TRY AGAIN" ;
getch();
break;
}
}while(choice!=5);
}

====================
OUTPUT
Computer Science Assignment

1.
2.
3.
4.
5.

MAIN MENU
Initialize Queue
Insert
Delete
Display Queue
Exit

Enter Your Choice(1-5):


1
----------------------------------------------------------------------------Enter No. of Elements 8
Enter ELEMENTS of LINKED_STACK:
1 5 9 7 8
6 5 4
----------------------------------------------------------------------------MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5):
2
----------------------------------------------------------------------------Enter element To Be Added: 52
----------------------------------------------------------------------------INSERTING ELEMENT 52 INTO THE QUEUE . . . . .
Element INSERTED Seccesfully
----------------------------------------------------------------------------DELETING ELEMENT FROM QUEUE . . . . .
Element1 Deleted !!
----------------------------------------------------------------------------MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Enter Your Choice(1-5):
4
----------------------------------------------------------------------------The Contents Of Queue Are
5
9
7
8
6
5
4
52
----------------------------------------------------------------------------MAIN MENU
1. Initialize Queue
2. Insert
3. Delete
4. Display Queue
5. Exit
Computer Science Assignment

Enter Your Choice(1-5):

EXITING
SHUTTING DOWN PROGRAM . . . .
-----------------------------------------------------------------------------

=X=X=X=X=X=X=X=X=X=X=

Computer Science Assignment

You might also like