Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 133

C Programs

++
Documentation-1
_______________________________________
Aim –
To store price list of 50 items and to print the
largest price as well as the sum of all prices.

Header files used –


iostream.h , conio,h

Functions used –
ITEM::initialize()

ITEM::largest()

ITEM::sum()

ITEM::display_items()

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1.Define a class named ITEM with following
specification:-

Data Members:

Integer itemcode[50]

-to store codes of 50 item

Floating it_price[50]

-to store price of 50 items

Member functions:

void initialize()-to initialize data members

void largest()-to get price of costly item

void sum()-to get the sum of all item’s price

void display_items()-to display the data members


Source code -1
#include<iostream.h>

#include<stdlib.h>

class ITEM{ int itemcode[50];

float it_price[50];

public:

void initialize(void);

float largest(void);

float sum(void);

void display_items(void);

};

void ITEM::initialize(void)

{ for(int i=0;i<50;i++)

{ cout<<"\n"<<"item No:"<<(i+1);

cout<<"\n"<<"Enter item code:";

cin>>itemcode[i];

cout<<"\n"<<"Enter item price:";

cin>>it_price[i];

cout<<"\n";

float ITEM::largest(void)
{ float large= it_price[0];

for(int i=1;i<50;i++)

{ if(large<it_price[i])

large=it_price[i];

return large;

float ITEM::sum(void)

{ float sum=0;

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

{ cout<<"\n"<<itemcode[i];

cout<<" "<<it_price[i];

cout<<"\n";

int main()

{ ITEM order;

order.initialize();

float total,biggest;

int ch=0;

system("cls");

do

{ cout<<"\nMain Menu.\n";
cout<<"\n1.Display largest price.";

cout<<"\n2.Display sum of price.";

cout<<"\n3.Display item list.";

cout<<"\nEnter your choice(1-3):";

cin>>ch;

switch(ch)

{ case 1 :biggest=order.largest();

cout<<"The Largest price


is"<<biggest<<"\n;

break;

case 2 :total=order.sum();

cout<<"The sum of price is"<<total<<"\n";

break;

case 3 :order.display_item();

break;

default:cout<<"\nWrong choice!\n";

break;

}while(ch>=1&&ch<=3);

return 0;

}
Output-1
Main Menu.

1. Display largest price.


2. Display sum of price
3. Display item list

Enter your choice(1-3):1

The largest price is 98

Code price

101 23

102 44

103 98

104 67

105 66
Documentation-2
_______________________________________
Aim –
Program to illustrate the order of constructor
invocation.

Header files used –


iostream.h , stdlib,h

Functions used –
Student::printsub()

Student::getval()

Student::print()

Student::print()

clrscr()

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
Student stud

-object of class student to store the amount of


fees to be paid

Public data members:

Admission()

~constructor to initialize its object

void print()

-to print the values of its data member

 Admission adm
 Print back in main
Source code -2
#include<iostream.h>

#include<stdlib.h>

class Subject{ int days;

int subjectno;

public:

subject(int d=123,int sn=101);

void printsub(void)

{ cout<<"Subject No:"<<subjectno;

cout<<"\n"<<"Days:"<<days<<"\n"; }

};

Subject::Subject(int d,int sn)

{ cout<<"Constructing Subject\n";

days=d;

subjectno=sn;

class Student{ int rollno;

float marks;

public:

Student()

{ cout<<"Constructing Students\n;

rollno=0;
marks=0.0;

void getval(void)

{ cout<<"Enter roll number and marks:";

cin>>rollno>>marks;

cout<<"\n";

void print(void)

{ cout<<"Roll no:"<<rollno;

cout<<"\n"<<"Marks:"<<marks<<"\n";

};

class Admission { Subject sub;

Student stud;

float fees;

public:

Admission()

{ cout<<"Constructing Admission\n";

fees=0.0;

void print(void)

{ stud.print();

sub.printsub();
cout<<"Fees:"<<fees<<"\n";

};

int main()

System("cls";

Admission adm;

cout<<"\nBack in main()\n";

return 0;

}
Output-2
Constructing Subject

Constructing Student

Constructing Admission

Back in main ()
Documentation-3
_______________________________________
Aim –
To illustrate access control of inherited members
in the privately derived class.

Header files used –


iostream.h , stdlib,h

Functions used –
employee::getdata()

employee::putdata()

employee::getbasic()

manager::getdata()

manager::getdata()

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1.Define class employee which will act as a base
class of class manager with following
specification:

Private data members and functions:

Character name[25]

-to store the name of employee

Unsigned long enumb

-to store employee member

Protected data members and function:

Floating basic

-to store the basic pay of the employee

Void getbasic()

-to display the values of data functions:

Public data members and functions:

Void getdata()

-to get values of data members from users

Void putdata()

-to display the values of data members

Void putdata ()

-to display the values of data members

2.Credit class manager which inherits class


employee as private with following specification:
Private data members and functions:

Character title[25]

-to store the title of the employee

Public data members and functions:

Void getdata()

- get values for both class and derived class data


members

Void putdata()

-display the values of data members class and


derived class.

3.Manager m1,m2

4.print” manager1”m1. getdata()

5. print” manager2”m2. Getdata()

Print” manager1” details m1. Putdata()

Print” manager2” details m2. putdata()

End
Source code -3
#include<iostream.h>

#include<stdio.h>

#include<stdlib.h>

const int LEN=25;

class Employee{ private:

char name[LEN];

unsigned long enumb;

public:

void getdata()

{ cout<<"Enter Name:";

gets(name);

cout<<"Enter Employee Number:";

cin>>enumb;

void putdata()

{ cout<<"Name:"<<name<<"\t";

cout<<"Emp.Number:"<<enumb<<"\t";

cout<<"Basic salary:"<<basic;

protected:

float basic;
void getbasic()

{ cout<<"Enter Basic:"; cin>>basic;}

};

class Manager:private Employee

private:

char title[LEN];

public:

void getdata()

Employee::getdata();

getbasic();

cout<<"Enter Title:";

get(title);

cout<<"\n";

void putdata()

{ Employee::putdata();

cout<<"\tTitle:"<<title<<"\n";

};

int main()

{ Manager m1,m2;
cout<<"Manager1\n";

m1.getdata();

cout<<"Manager2\n";

m2.getdata();

cout<<"\t\tManager1 Details\n";

m1.putdata();

cout<<"\t\tManager2 Details\n";

m2.putdata();

return 0;

}
Output -3
Manager 1

Enter name : vaidehi Sharma

Enter employee number : 5192

Enter basic : 12700

Enter title : Mrs.

Manager 2

Enter name : Irshad khan

Enter employee number : Irshad khan

Enter basic : 11000

Enter title : Mr.

Manager 1 Details

Name : vaidehi Sharma Emp number : 5192


Basic salary : 12700 Title : Mrs.

Manager 2 Details

Name : Irshad khan Emp number : 3222


Basic salary : 11000 Title : Mr.
Documentation-4
_______________________________________
Aim – To illustrate the use of multilevel
programming

Header files used –


iostream.h , conio.h

Functions used –
Void main()
Void getdata()
Void putdata()
accept()
display()
clrscr()
input()
output()
Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. Create a class person.

2. Derive another class student from person.

3. Derive class subject from student.

4. Function input is used to enter details .

5. Function output is used to display details.

6. END.
Source code-4
#include<iostream.h>

#include<conio.h>

class person{ char nm[40];

int age;

char add[40];

public:

void getdata()

{ cout<<"ENTER NAME:"; cin>>nm;

cout<<"\n"<<"ENTER AGE:";
cin>>age;

cout<<"\n"<<"ENTER ADDRESS:"; cin>>add; }

void putdata()

{ cout<<"\n"<<"NAME:"<<nm<<"\n"

<<"AGE:"<<age<<"\n"

<<"ADDRESS:"<<add<<"\n";

};

class student:public person

{ int rno;

int cl;

char sec;
public:

void accept()

{ getdata();
cout<<"\n"<<"ENTER ROLL
NUMBER:"; cin>>rno;

cout<<"\n"<<"ENTER
CLASS:"; cin>>cl;

cout<<"\n"<<"ENTER
SECTION:"; cin>>sec;

void display()

{ putdata();
cout<<"ROLL
NUMBER:"<<rno<<"\n"

<<"CLASS:"<<cl<<"\n"

<<"SECTION:"<<sec<<"\n";

};

class subject:public student

{ char sub1[10],sub2[10];

float m1,m2;

public:

void input()

{ accept();
cout<<"\n"<<"ENTER
SUBJECT 1:"; cin>>sub1;

cout<<"\n"<<"ENTER
MARKS OF SUBJECT 1:"; cin>>m1;

cout<<"\n"<<"ENTER
SUBJECT 2:"; cin>>sub2;

cout<<"\n"<<"ENTER
MARKS OF SUBJECT 2:"; cin>>m2;

void output()

{ display();

cout<<"SUBJECT
1:"<<sub1<<"\n"

<<"MARKS:"<<m1<<"\n"

<<"SUBJECT:"<<sub2<<"\n"

<<"MARKS:"<<m2<<"\n"; }

};

void main()

{clrscr();

subject s1;

s1.input();

s1.output();
}
Output -4
ENTER NAME:DEV

ENTER AGE:17

ENTER ADDRESS:BHOPAL

ENTER ROLL NUMBER:12

ENTER CLASS:12

ENTER SECTION:C

ENTER SUBJECT 1:ENGLISH

ENTER MARKS OF SUBJECT 1:60

ENTER SUBJECT 2:COMPUTER

ENTER MARKS OF SUBJECT 2:65

NAME:DEV

AGE:17

ADDRESS:BHOPAL

ROLL NUMBER:12

CLASS:12
SECTION:C

SUBJECT 1:ENGLISH

MARKS:60

SUBJECT:COMPUTER

MARKS:65
Documentation-5
_______________________________________
Aim – Program to create a single file and then
display its contents.

Header files used –


iostream.h , stdlib.h

fstream.h

Functions used –
fin.close(),fin.open()

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. Open file using ofstream.

2. Enter details into it.

3. Transfer the details to file using fout.

4. Open file using ifstream.

5. Extract the contents of file using fin

6. Using cout print the details.

7. END.
Source code-5
#include<iostream.h>

#include<fstream.h>

#include<stdlib.h>

int main()

{ system ("cls");

ofstream fout("student",ios::out);

char name[30],ch;

float marks=0.0;

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

{ cout<<"student"<<(i+1)<<":\tName:";

cin.get(name,30);

cout<<"\t\Marks:";

cin>>marks; cin.get(ch);

fout<<name<<'\n'<<marks<<'\n';

fout.close();

ifstream fin("student",ios::in);

fin.seekg(0);

cout<<"\n";

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

{ fin.get(name,30);
fin.get(ch);

fin>>marks;

fin.get(ch);

cout<<"Student Name:"<<name;

fin.close();

return 0;

}
Output -5
Student 1 : Name : Raju shah

Marks : 88

Student 2 : Name : Neil sarkar

Marks : 90

Student 3 : Name : Nick Robert

Marks : 78

Student 4 : Name : M.ramesh

Marks : 67

Student 5 : Name : S.Anis

Marks : 73

Student Name : Raju shah Marks : 88

Student Name : Neil sarkar Marks : 90

Student Name : Nick Robert Marks : 78

Student Name : M.ramesh Marks : 67

Student Name : S.Anis Marks : 73


Documentation-6
_______________________________________
Aim –
Program to use multiple files simultaneously.

Header files used –


iostream.h , stdlib.h

fstream.h

Functions used –
filin1.close();, filin2.close();,
filin2.open, filin1.open

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. Create file stunames .

2. Create file stumarks.

3. Read first content of file stunames.

4. Print it.

5. Read first content of stumarks.

6. Print it.

7. Follow steps 3,4,5,6 for second content of


a. each file.

8. END.
Source code- 6
#include<iostream.h>

#include<fstream.h>

#include<stdlib.h>

int main()

{ system ("cls");

ifstream filin1,filin2;

filin1.open("Stunames",ios::in);

filin2.open("Stumarks",ios::in);

char line[80];
cout<<"the contents of Stunames and Stumarks are
given below.\n";

filin1.getline(line,80);

cout<<line<<"\n";

filin2.getline(line,80);

cout<<line<<"\n";

filin1.getline(line,80);

cout<<line<<"\n";

filin2.getline(line,80);

cout<<line<<"\n";

filin1.getline(line,80);

cout<<line<<"\n";
filin2.getline(line,80);

cout<<line<<"\n";

filin1.close();

filin2.close();

return 0;

}
Output -6
The contents of Stunames and Stumarks are given below.

Devyani

78.92

Monica Patrick

72.17

Neil banarjee

69.33
Documentation-7
_______________________________________
Aim –
Program to write and read a structure using write()
and read() function using a binary files.

Header files used –


iostream.h , stdlib.h

string.h ,fstream.h

Functions used –
Void selsort(int[],int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
The is a structure named customer contains name and
balance

1.Declare savac as customer


2. Copy the value to structure[customer]

3. Declare fout as ofstream

4. Open file[saving ios::out,binary]

5. If ! fout then “File can’t be Opened”

End of if

6.Write data into the file

7.Close the file

//Read file back

8.Declare file as ifstream

9. Open file [saving ios::in,binary]

10.Read the value from file

11.Display the values

12.Close file

13.EXIT
Source code -7
#include<iostream.h>

#include<fstream.h>

#include<string.h>

#include<stdlib.h>

struct customer { char name[51];

float balance; };

int main()

{ system("cls" );

customer savac;

strcpy( savac.name,"tina marshall");

savac.balance=21310.75;

ofstream fout;

fout.open("saving",ios::out|ios::binary);

if(!fout)

{ cout<<"file can't be opened \n ";

return 1;

fout.write((char*)&savac,sizeof(customer));

fout.close();

ifstream fin;

fin.open("saving",ios::in|ios::binary);
fin.read((char*)&savac,sizeof(customer));

cout<<savac.name;

cout<<"\thas the balance amount of


Rs."<<savac.balance<<"\n";

fin.close();

return 0 ;

}
Output-7
Tina Marshall has the balance amount of Rs.21310.75
Documentation-8
_______________________________________
Aim –
Program for reading and writing class objects.

Header files used –


iostream.h , stdlib.h

string.h ,fstream.h

Functions used –
Void selsort(int[],int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. Create a class student.

2. Create an object s1[3] to make an array of


i. objects , enter data in it using member
function
ii. getdata.

3. Open binary file stu.dat.

4. Using fout.write((char*)&s1[i],sizeof(s1[i]))
i. write the information to file.

5. Using fin.write((char*)&s1[i],sizeof[i]))
i. read the information from the file.

6. Print the information using member function


i. display.

7. END.
Source code-8
#include<iostream.h>

#include<fstream.h>

#include<conio.h>

#include<process.h>

class student{ char nm[40];

char grade;

float marks;

public:

void getdata();

void display();

};

void student::getdata()

{cout<<"\n"<<"ENTER NAME:"; cin>>nm;

cout<<"\n"<<"ENTER GRADE:"; cin>>grade;

cout<<"\n"<<"ENTER MARKS:"; cin>>marks; }

void student::display()

{cout<<"\n"<<"NAME:"<<nm;

cout<<"\n"<<"GRADE:"<<grade;

cout<<"\n"<<"MARKS:"<<marks;

void main()
{clrscr();

student s1[3];

fstream filin;

filin.open("stu.dat",ios::in|ios::out);

if(!filin)

{ cout<<"CANNOT OPEN FILE"<<"\n";

getch();

exit(0); }

for(int i=0;i<3;++i)
{cout<<"\n"<<"ENTER DETAILS OF
STUDENT:"<<i+1<<"\n";

s1[i].getdata();

filin.write((char*)&s1[i],sizeof(s1[i]));

filin.seekg(0);

cout<<"\n"<<"CONTENTS OF FILE ARE"<<"\n";

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

{cout<<"\n"<<"\n"<<"STUDENT:"<<i+1<<"\n";

filin.read((char*)&s1[i],sizeof(s1[i]));

s1[i].display(); }

filin.close();

getch();

}
Output-8
ENTER DETAILS OF STUDENT:1

ENTER NAME:DARPAN
ENTER GRADE:A
ENTER MARKS:95

ENTER DETAILS OF STUDENT:2


ENTER NAME:MANIT
ENTER GRADE:B
ENTER MARKS:66

ENTER DETAILS OF STUDENT:3


ENTER NAME:DEV
ENTER GRADE:B
ENTER MARKS:67

CONTENTS OF FILE ARE


STUDENT:1
NAME:DARPAN
GRADE:A
MARKS:95

STUDENT:2
NAME:MANIT
GRADE:B
MARKS:66

STUDENT:3
NAME:DEV
GRADE:B
MARKS:67
Documentation-9

_______________________________________
Aim –
Program to append data in a file.

Header files used –


fstream.h,conio.h

Functions used –
Void main()
Void getdata()
Void putdata()
close()
clrscr()

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
/* the class with required specifications is
already defined */

/*the link to file is memory to file */

1. Open the data file in append mode.

2. Write the data in it using write function

till while loop works.

3. END.
Source code -9
#include<fstream.h>

#include<conio.h>

class stu{ int rno;

char nm[40];

int cl;

char sec;

float mks;

char grade;

public:

void getdata()

{ cout<<"ENTER ROLL NUMBER:"; cin>>rno;

cout<<"\n"<<"ENTER NAME:"; cin>>nm;

cout<<"\n"<<"ENTER CLASS:"; cin>>cl;

cout<<"\n"<<"ENTER MARKS:"; cin>>mks;

if(mks>=75) grade='A';

else if(mks<75&&mks>=60) grade='B';

else if(mks<60&&mks>=50) grade='C';

else grade='D';

void putdata()

{ cout<<"\n"<<"ROLL NUMBER:"<<rno<<"\n";
cout<<"\n"<<"NAME:"<<nm<<"\n";

cout<<"\n"<<"CLASS:"<<cl<<"\n";

cout<<"\n"<<"MARKS:"<<mks<<"\n";

cout<<"\n"<<"GRADE:"<<grade<<"\n";

}s1;

void main()

{ clrscr();

ofstream fout("stu.dat",ios::app);

char ans='y';

while(ans=='y')

{ s1.getdata();

fout.write((char*)&s1,sizeof(s1));

cout<<"\n"<<"RECORD ADDED TO FILE"<<"\n";

cout<<"\n"<<"WANT TO ENTER MORE


RECORDS(y/n)"<<"\n";

cin>>ans;

fout.close();

}
Output-9
ENTER ROLL NUMBER:10

ENTER NAME:RAJU SHAH

ENTER CLASS:12

ENTER MARKS:80

RECORD ADDED TO FILE

WANT TO ENTER MORE RECORDS(y/n)

N
Documentation -10
_______________________________________
Aim –
Linear search in array.

Header files used –


iostream.h,conio.h

Functions used –
Lsearch(int [] ,int,int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
/* Initialise counter by assigning lower bound
value of array */

Step.1: Set ctr = L //In c++,L(Lower bound) is 0

/* Now search for the ITEM */

Step.2: Repeat steps 3 through 4 until ctr>u.

//In c++ (Upper bound is ) size-1.

Step.3 : if ar[ctr]==ITEM then


{ print “Search Successful” print ctr,” is the
location of “,ITEM

break

/* go out of loop */

Step.4 : ctr=ctr+1

/* end of repeat */

Step.5 : if ctr>u then

Print” Search unsuccessful!”

Step.6 : END
Source code-10
#include<iostream.h>

#include<conio.h>

int lsearch(int[],int,int);

void main()

{ clrscr();

int ar[50],item,N,index;

cout<<"ENTER DESIRED ARRAY SIZE(max.50):";

cin>>N;

cout<<"\n"<<"ENTER ARRAY ELEMENTS"<<"\n";

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

{cin>>ar[i];}

cout<<"\n"<<"ENTER ARRAY ELEMENT TO BE SEARCHED


FOR:";cin>>item;

index=lsearch(ar,N,item);

if(index==-1)
{cout<<"\n"<<"SORRY GIVEN ELEMENT COULD NOT
BE FOUND"<<"\n";}

else

cout<<"\n"<<"ELEMENT FOUND AT INDEX:"<<index

<<",POSITION:"<<index+1<<"\n";

}
int lsearch(int ar[],int size,int ITEM)

{ for(int i=0;i<size;++i)

{ if(ar[i]==ITEM) return i;

return-1;

}
Output -10
ENTER DESIRED ARRAY SIZE(max.50):5

ENTER ARRAY ELEMENTS

ENTER ARRAY ELEMENT TO BE SEARCHED FOR:2

ELEMENT FOUND AT INDEX:1,POSITION:2


Documentation-11

_______________________________________
Aim –
Binary search in an array.

Header files used –


iostream.h

Functions used –
Void selsort(int[],int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. Let us assume that given array is AR[n]

2. Read array,searched element.

Beg=0, last=n.

3. Repeat steps 4,5,6 until beg ≤ last

4. mid= (beg+last)/2

5. if AR[mid]==value

Pos=mid

Print pos

Break

6. if x[mid], value

Beg=mid+1

Else last=mid-1

7. if element is not found

Print element is not available in the list.

8. end
Source code 11
#include<iostream.h>

int Bsearch(int[],int,int);

int main()

{ intAE[50],ITEM,N,index;

cout<<"Enter desired array size(max.50)....";

cin>>N;

cout<<"\nEnter Array elements(must be sorted in Asc


order)\n";

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

cin>>AR[i];

cout<<"\nEnter element to be searched for....";

cin>>ITEM;

index=Bsearch(AR,N,ITEM);

if(index==-1)

cout<<"\nSorry!!Given element could not be


found.\n";

else

cout<<"\nElement found at index :


"<<index<<",position:"<<index+1<<endl;

return 0 ;

int Bsearch(int AR[],int size,int item)


{ int beg,last,mid;

beg=0; last=size-1;

while(beg<=last)

{ mid=(beg+last)/2;

if(item==AR[mid])retun mid ;

else if(itm>AR[mid]) beg=mid+1;

else lasrt=mid-1;

return -1;

}
Output-11
Enter desired array size(max.50)……7

Enter Array elements(must be sorted in


ASC order)

2 5 7 8 9 10 15

Enter Element to be searched for ….8

Element found at index:3, position:4


Documentation -12
_______________________________________
Aim –
Insertion in array .

Header files used –


iostream.h,conio.h,process.h

Functions used –
findpos(int [] ,int,int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
/* First the appropriate position for ITEM is to be
determined i.e.,if the appropriate position is i+1
then ar[i]<= ITEM <=ar[i+1],LST specifies maximum
possible index in array,u specifies index of last
element in array */

1. ctr =L /*initialize the counter*/

2. if LST =U then

{ print”overflow:”

Exit from program

3. if ar[ctr]>ITEM then

Pos = 1

else

4. Repeat steps 5 and 6 untitl ctr>=U

5. if ar[ctr]<=ITEM and ITEM<= ar[ctr+1]


then

{ pos = ctr+1
break

6. ctr = ctr + 1
/*end of repeat here*/

7. if ctr = U then
pos = U+1

} /*end of if step 3*/

/*shift the elements to create space*/

8. ctr=U /*initialize the counter*/

1. while ctr>=pos perform steps 10 through 11

10. { ar[ctr+1]=ar[ctr]

11. ctr=ctr-1
}

12. ar[pos]=ITEM /*insert the element*/

13. END
Source code-12
#include<iostream.h>

#include<process.h>

#include<conio.h>

int findpos(int[],int,int);

void main()

{ int ar[50],item,N,index;

cout<<"ENTER NUMBER OF ELEMENTS OF


ARRAY(MAX.50....";

cin>>N;

cout<<"\n"<<"ENTER ARRAY ELEMENTS(MUST BE SORTED


IN ASC.ORDER)"<<"\n";

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

cin>>ar[i];

char ch='y';

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

{ cout<<"\n"<<"ENTER ELEMENT TO BE
INSERTED...";

cin>>item;

if(N==50)

{ cout<<"OVERFLOW"<<"\n";

exit(1);}

index=findpos(ar,N,item);
for(i=N;i>index;--i)

ar[i]=ar[i-1];

ar[index]=item;

N+=1;
cout<<"\n"<<"WANT TO INSERT MORE
ELEMENTS(y/n)...";

cin>>ch;

cout<<"\n"<<"THE ARRAY NOW IS AS SHOWN


BELOW..."<<"\n";

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

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

int findpos(int ar[],int size,int ITEM)

{ int pos;

if(ITEM<ar[0]) pos=0;

else

{ for(int i=0;i<size;++i)

{ if(ar[i]<=ITEM&&ITEM<ar[i+1])

{ pos=i+1; break;

} }

if(i==size-1) pos=size; }

return pos;

}
Output-12
ENTER NUMBER OF ELEMENTS OF ARRAY(MAX.50....6

ENTER ARRAY ELEMENTS(MUST BE SORTED IN ASC.ORDER)

10

12

15

ENTER ELEMENT TO BE INSERTED...11

WANT TO INSERT MORE ELEMENTS(y/n)...y

ENTER ELEMENT TO BE INSERTED...4

WANT TO INSERT MORE ELEMENTS(y/n)...n

THE ARRAY NOW IS AS SHOWN BELOW...

10

11

12

15
Documentation -13
_______________________________________
Aim –
selection sort in array .

Header files used –


iostream.h

Functions used –
Void selsort(int[],int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
Small=AR[L]

For I=L TO do

Small=AR[I],pos=I

/*loop to find smallest element and its


position */

For J=I TO U do

if AR[J]<small then

{ small=AR[I]

Pos=J

J=J+1

/* swap the smallest element with I element*/

temp=AR[I]

AR[I]=small

AR[pos}=temp

END.
Source code -13
#include<iostream.h>

Void selsort(int[],int);

int main()

{int AR[50],ITEM,N,index;

cout<<”How many elements do U want to create


array with ?( maximum = 50)---- “;

cin>>N;

cout<<”\n Enter Array elements…”;

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

cin>>AR[i];

selsort(AR,N);

cout<<”\n\n The sorted array is as shownbelow--


--\n\n”;

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

cout<<AR[i]<<” “;

cout<<endl;

Void selsort(int AR[],int size)

{ int small,pos,tmp;

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

{ small=AR[i];

for(int j=i+1;j<size;j++)
{ if(AR[j]<small)

{ small=AR[j];

Pos=j;}

tmp=AR[i];

AR[i]=AR[pos];

AR[pos]=tmp;

Cout<<”Array after pass :”<<i+1<<” is “;

For(j=0;j<size;j++)

Cout<<AR[j]<<” “;

}
Output-13
How many element do U want to create array with
?( maximum = 50)----6

Enter array elements…6 8 5 2 3 4

Array after pass :1 is 2 8 5 6 3 4

Array after pass :2 is 2 3 5 6 8 4

Array after pass :3 is 2 3 4 6 8 5

Array after pass :4 is 2 3 4 5 8 6

Array after pass :5 is 2 3 4 5 6 8

Array after pass :6 is 2 3 4 5 6 8

The sorted array is as shown below----

1 2 3 4 5 6 8
Documentation-14
_______________________________________
Aim –
Bubble sort in array.

Header files used –


iostream.h

Functions used –
Void Bubblesort(int[],int)

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
for I=L TO U

{ for J=L TO[(U-1)]

{ if AR[J]>AR[J+1]then

Temp= AR[J]

AR[J]=AR[J+1]

AR[J+1]=temp

END.
Source code -14
#include<iostream.h>

Void Bubblesort(int[],int);

int main()

{ int AR[50],ITEM,N,index;

cout<<”How many elements do U want to create


array with ?( maximum.50)---- “;

cin>>N;

cout<<”\n Enter Array elements…\n”;

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

cin>>AR[i];

Bubblesort(AR,N);

cout<<”\n\n The sorted array is as shownbelow--


--\n\n”;

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

cout<<AR[i]<<” “;

cout<<endl;

return0 ;

Void Bubblesort(int[],int size)

{ int tmp,crt=0;

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

{for(int j=0;j<(size-1),--i,j++)
{ if(AR[j]>AR[j+1])

{tmp=AR[j];

AR[j]=AR[j+1];

AR[j+1]=tmp;}

Cout<<”Array after insertion “<<++ctr<<”-is:”;

For(int k=0;k<size;k++)

Cout<<AR[k]<<” “;

Cout<<endl;

}
Output-14
How many element do U want to create array with
?( maximum = 50)----5

Enter array elements…

9 7 4 6 1

Array after iteration-1-is: 7 4 6 1 9

Array after iteration-2-is: 4 6 1 7 9

Array after iteration-3-is: 4 1 6 7 9

Array after iteration-4-is: 1 4 6 7 9

Array after iteration-5-is: 1 4 6 7 9

The sorted array is as shown below….

1 4 6 7 9
Documentation- 15
_______________________________________
Aim –
Insertion in the beginning of the list.

Header files used –


iostream.h

process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
/* this algorithm deals with insertion in the
beginning of linked list */

Ptr =START

/*allocate memory for the nem node*/

NEWPTR=new Node

If NEWPTR=NULL

Print”No space Available! Aborting!!”

Else

NEMPTR->INFP=ITEM

NEMPTR->LINK=NULL

If START=NEWPTR

Else

Save=START

*/

START=NEWPTR

NEWPTR->LINK=Save

*/ }

END.
Source code- 15
#include<iostream.h>

#include<process.h>

Struct Node { int info;

Node*next;

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

Node*create_New_Node(int);

Void Insert_Beg(Node*);

Int main()

{ start=NULL;

int inf; char ch=’y’;

while(ch==’y’||ch==’y’)

{ system(“cls”);

Cout<<”\n Enter INFOrmationfor the new node…”;

Cin>>inf;

Cout<<”\n Creating New Node(inf);

if( newptr!=NULL)

{ cout<<”\n\n New Node Created Successfully.Press


Enter to continue…”;

System(“pause”);

else
{cout<<”\n Cannot create new node!!!\n”;
system(“pause”);

exit(1);

Cout<<”\n\n Now inserting this node in the


beginning of list…\n”;

Cout<<” Press Enter to Continue…\n”;

System(“pause”);

Insert_beg(newptr);

Cout<<”\n Now the list is :\n”;

Display(start);

cout<<”\nPress Y to enter more nodes,N to exit…\n”;

cin>>ch;

return 0;

Node*Create_New _Node(int n)

{ ptr = new Node;

Ptr->info=n;

Ptr->next=NULL;

return ptr ;

Void Insert_Beg(Node*np)

{if(start==NULL)
Start=np;

else

{ save=start;

Start=np;

np ->next=save;

Void Display(Node*np)

{while(np!=NULL)

{cout<<np->info<<”->“;

np=np->next;

cout<<”!!!\n”;

}
Output-15
ENTER INFOrmation for the new node…9

Creating New Node!! Press Enter to continue…

New Node Created Successfully.Press Enter to


continue…

Now inserting this node in the beginning of list…

Press Enter to continue…

Now the list is :

9 -> !!!

Press Y to enter more nodes,N to exit… Y

ENTER INFOrmation for the new node…3

Creating New Node!! Press Enter to continue…

New Node Created Successfully.Press Enter to


continue…

Now inserting this node in the beginning of list…

Press Enter to continue…

Now the list is :

3 -> 9 -> !!!

Press Y to enter more nodes,N to exit…

N
Documentation-16
_______________________________________
Aim –
Insertion in the End of the list.

Header files used –


iostream.h,stdlib.h

process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. Declare pointers START,PTR,NEWPTR REAR

2. ptr=START

3. NEWPTR=new Node

4. IF NEWPTR = NULL

5. Print”no space available ! aborting !!!”

6. Exit

7. ELSE

8. NEWPTR-> LINK = NULL

9. IF START=NULLTHEN

10. START=NEWPTR

11. REAR=NEWPTR

12. REAR-> LINK = NEWPTR

13. REAR=NEWPTR

14. END
Source code- 16
#include<iostream.h>

#include<stdlib.h>

#include<process.h>

Struct Node { int info;

Node*next;

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

Node*create_New_Node(int);

Void Insert_End(Node*);

Void Display(Node*);

int main()

{ start=rear=NULL;

int inf; char ch=’y’;

while(ch==’y’||ch==’y’)

{ system(“cls”);

cout<<”\n Enter INFOrmation for the new node…”;

cin>>inf;

cout<<”\n Creating New Node(inf);

if( newptr!=NULL)

{ cout<<”\n\n New Node Created Successfully.Press


Enter to continue…”;

System(“pause”);
}

else

{ cout<<”\n Cannot create new node!!!\n”;


system(“pause”);

exit(1);

Cout<<”\n\n Now inserting this node in the end of


list…\n”;

Cout<<” Press Enter to Continue…\n”;

System(“pause”);

Insert_End(newptr);

Cout<<”\n Now the list is :\n”;

Display(start);

cout<<”\n Press Y to enter more nodes,N to


exit…\n”;

cin>>ch;

return 0;

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;

Void Display(Node*np)

{ while(np!=NULL)

{ cout<<np->info<<”->“;

np=np->next;

cout<<”!!!\n”;

}
Output-16
ENTER INFOrmation for the new node… 9

Creating New Node!! Press Enter to continue…

New Node Created Successfully.Press Enter to


continue…

Now inserting this node in the end of list…

Press Enter to continue…

Now the list is :

9 -> !!!

Press Y to enter more nodes,N to exit… Y

ENTER INFOrmation for the new node… 3

Creating New Node!! Press Enter to continue…

New Node Created Successfully.Press Enter to


continue…

Now inserting this node in the beginning of list…

Press Enter to continue…

Now the list is :

9 -> 3 -> !!!

Press Y to enter more nodes, N to exit……

N
Documentation-17
_______________________________________
Aim –
Deletion from the beginning of the list.

Header files used –


iostream.h,stdlib.h

process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1.If START = NULL THEN

2. Print “UNDERFLOW”
3. else
4. { ptr = start
5. Start = ptr -> LINK
6. Delete ptr }
7. END
Source code-17
# include<iostream.h>

# include<stdlib.h>

# include<process.h>

Struct Node { int info;

Node * next;

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

Node*Create_New_Node(int);

void Insert (Node*);

void Display(Node*);

void DelNode();

int main()

{ start = rear = NULL;

int inf;

char ch =’y’;

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

{ system (“cls”);

cout <<”\n Enter INFORMATION for new node...”;

cin >> inf ;

newptr = Create_ New_Node(inf);

if ( newptr == NULL )
{ cout <<”\nCannot create new node !!! Aborting!!”;

Exit(1);

Insert( newptr);

cout<<”\nPress Y to enter more nodes,N to exit..\n”;

cin>>ch;

system( “cls”);

do

{ cout <<”\n The list now is : \n”;

Display (start) ; system (‘’pause’’);

cout << ‘’\n want to delete first node ?(y/n)...”;

cin >> ch;

if ( ch ==’y’||ch == ‘Y’)

Delnode( );

} while( ch == ‘y’||ch == ’Y’) ;

return 0;

Node*Create_New_Node( int n )

{ ptr = new Node ;

ptr -> info = n ;

ptr -> next = NULL;

return ptr ;
}

Void Insert ( Node*np)

{ if ( start == NULL ) start = rear = np;

else

{ rear -> next = np;

rear = np ;

Void DelNode ()

{ if ( start == NULL ) cout << “ UNDERFLOW !!!\n”;

Else

{ ptr = start ;

start = start -> next;

delete ptr ;

Void Display ( Node*np )

{ while ( np! = NULL )

{ cout << np -> info << “->”;

np = np -> next ; }

cout <<”!!!!\n”;

}
Outputs-17

Enter INFORMATION for the new node . . . . 25

Press Y to enter more nodes , N to exit . . . .

The List now is :

3 -> 5 -> 9 -> 21 -> 25 -> !!!!

Want to delete first node ? (y/n) . . . .y

The List now is :

5 -> 9 -> 21 -> 25 -> !!!!

Want to delete first node ? (y/n) . . . .y

The List now is :

9 -> 21 -> 25 -> !!!!

Want to delete first node ? (y/n) . . . .n


Documentation-18
_______________________________________
Aim –
To illustrate popping from array-stack.

Header files used –


iostream.h,process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
Firstly , check for underflow condition

1.If top == -1 then

2. Print “Underflow !!”

3.Exit from program

4.Else

5.print stack(top)
6. top = top-1 /*top moved , so that the
topmost

element

becomes*/
/*inaccessible which is a
sort of a deletion*/

7.END
Source code-18
# include<iostream.h>

# include<process.h>

int Pup(int[ ],int&);

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

void Display(int[ ],int) ;

const int size = 50

int main ( )

{ int Stack[size],Item,top = -1 , res ;

char ch = ’y’ ;

while ( ch == ‘y’|| ch == ‘ Y’ )

{ cout << “\n Enter ITEM for insertion :” ;

cin >>Item ;

res = Push( Stack , top , Item) ;

if ( res == -1 )

cout << “OVERFLOW ! ! ! ! Aborting ! ! \n”;

exit (1) ;

cout << “\n The stack now is : \n”;

Display(Stack,top);

cout <<“\n Want to insert more elements ?(y/n)...”;

cin >> ch ;
}

cout << “ Now deletion of elements begin . . . .\n”;

ch = ‘y’ ;

while ( ch == ‘y’|| ch == ‘Y’ )

{ res = Pop ( Stack,top);

If ( res == -1 )

{ cout<< “UNDERFLOW ! ! ! Aborting ! ! \n” ;

exit (1) ;

else
{ cout<< “\n Elements deleted is
:”<<res<<endl;

cout<< “\n The Stack now is : \n”;

Display(Stack , top);

cout<<“Want to delete more elements? (y/n)...”;

cin >> ch;

return 0 ;

int Push ( int Stack[ ], int & top , int ele )

{ if ( top == size-1 ) return -1 ;

else

{ top++ ;
Stack[top] = ele ;

return 0 ;

int Pop( int Stack[ ], int & top )

int ret ;

if ( top == -1) return -1 ;

else

{ ret = Stack [top] ;

top -- ;

return ret ;

void Display (int Stack[ ], int top)

{ if ( top == -1 ) return ;

cout << Stack[top] << “< --“ << endl ;

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

cout << Stark[i] << endl ;

}
Outputs-18
Enter ITEM for insertion : 9

The Stack now is :

9 <--

Want to insert more elements ? (y\n) . . . .y

Enter ITEM for insertion : 6

The Stack now is :

6 <--

Want to insert more elements? (y\n) . . . .y

Enter ITEM for insertion : 3

The Stack now is :

3 <--

Want to insert more elements? (y\n) . . . .n

Now deletion of elements begin . . . .

Element deleted is : 3

The Stack now is :

6 <--

Want to delete more elements ? (y\n) . . . .y


Element deleted is : 6

The Stack now is :

9 <--

Want to delete more elements ? (y\n) . . . .n


Documentation-19
_______________________________________
Aim –
To illustrate popping from a linked - stack.

Header files used –


iostream.h,process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
Get new node for ITEM to be popped

1. newptr = new mode


2. newptr ->info = ITEM ;
newptr ->link = NULL /*Add new node
at the top*/

3.if top = NULL then /*stack


is empty*/

4.top = newptr

5. else

6.newptr -> link = top

7.top = newptr

8.end
Source code-19
# include<iostream.h>

# include<stdlib.h>

# include<process.h>

Struct Node { inf info ;

Node * next ;

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

Node * Create_New_Node( int ) ;

void Push( Node*) ;

void Display( Node*) ;

void Pop( ) ;

int main ( )

{ top = NULL ;

int inf ; char ch == ‘y’ ;

while (ch == ‘y’|| ch == ‘Y’ )

{ cout << “\n Enter INFOrmation for the new node” ;

cin >> inf ;

newptr = Create_New_Node( inf ) ;

if ( newptr == NULL )

{ cout<<“\nCannot create new node !!! Aborting!! \n”;

Exit (1) ;
}

Push ( newptr ) ;

cout << “\n Press Y to enter more nodes,N to exit...” ;

cin >> ch ;

system ( “cls” ) ;

do

{ cout << “\n The Stack now is : \n ;

Display ( top 0 ; system ( “pause” ) ;

cout << “ Want to pop an element ? (y/n)...” ;

cin >> ch ;

if ( ch == ‘y’|| ch == ‘Y’ ) Pop( ) ;

} while ( ch == ‘y’||ch == ‘Y’ ) ;

return 0 ;

Node * Create_New__Node( int n )

{ ptr = new Node ;

ptr -> info = n ; ptr -> next = NULL ;

return ptr ;

Void Push ( Node * np )

{ if ( top == NULL ) top ==np ;

else
{ save = top ; top =np ;

np -> next = save ;

void Pop ( )

{ if ( top == NULL ) cout << “ UNDERFLOW ! ! !”


;

else

{ ptr = top ; top = top - > next ;

delete ptr ;

void display (Node * np)

{ while ( np ! = NULL )

{ cout << np -> info << “ ->” ;

np = np -> next ;

cout << “ ! ! ! ! \n” ;

}
Output -19
Enter INFOrmation for the new node ....6

Press Y to enter more nodes , N to exit....y

Enter INFOrmation for the new node ....8

Press Y to enter more nodes , N to exit....y

Enter INFOrmation for the new node ....9

Press Y to enter more nodes , N to exit....n

The Stack now is :

9 -> 8 -> 6 -> ! ! !

Want to pop an element ? (y/n). . . . .y

The Stack now is :

8 -> 6 -> ! ! !

Want to pop an element ? (y/n). . . . .y

The Stack now is :

6 -> ! ! !

Want to pop an element ? (y/n). . . . .n


Documentation-20
_______________________________________
Aim –
To illustrate insertion in array-Queue.

Header files used –


iostream.h,process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. If rear = NULL Then
{
2. Rear = front = 0
3. QUEUE[0] = ITEM

4. Else If rear = N-1 Then


5. Print “QUEUE FULL, OVERFLOW!!”
6. Else

{
7. QUEUE [rear +1] = ITEM
8. rear = rear+1
}
9. END.
Source code-20
# include<iostream.h>

# include<stdlib.h>

# include<process.h>

int Insert_in_Q( int[ ],int ) ;

void Display( int[ ], int, int ) ;

const int size = 50 ;

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

int main( )

{ int Item, res ; char ch = ‘y’ ;

system ( “cls” ) ;

while ( ch == ‘y’|| ch == ‘Y’ )

{ cout << “\n Enter ITEM for insertion :” ;

cin >> Item ;

res = Insert_in_Q( Queue, Item ) ;

if ( res == -1 )

{ cout << “OVERFLOW ! ! ! Aborting ! !”; exit(1) ; }

cout <<“Now the Queue ( Front...to...Rear ) is : \n” ;

Display( Queue, front,rear ) ;

cout << “Want to insert more elements ? (y/n)...” ;

cin >> ch ;

}
return 0 ;

Int Insert_in_Q(int Queue[ ],int ele )

{ if ( rear == size -1 ) return -1 ;

else if (rear == -1)

{ front = rear = 0 ;

Queue[rear] = ele ;

else

{ rear++ ;

Queue[rear] = ele ;

return 0 ;

void Display( int Queue[ ], int front, int rear )

{ if ( front == -1 ) return ;

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

cout << Queue[i] << “<- \t” ;

cout << Queue[rear] << endl ;

}
Output -20
Enter ITEM for insertion : 3

Now the Queue ( Front...to...Rear ) is :

Want to insert more elements ? (y\n) . . . .y

Enter ITEM for insertion : 6

Now the Queue ( Front...to...Rear ) is :

3<-6

Want to insert more elements ? (y\n) . . . .n


Documentation-21
_______________________________________
Aim –
Insertion in linked Queue

Header files used –


iostream.h,process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. NEWPTR= new Node

2. NEWPTR->INFO= ITEM; NEWPTR->LINK= NULL


/*Insert in the Queue*/

3. If rear= NULL Then


{
4. Front= NEWPTR

5. Rear= NEWPTR
}
6. Else
{
7. Rear->LINK= NEWPTR

8. Rear= NEWPTR
}

9. END
Source code- 21
#include<iostream.h>

#include<process.h>

struct Node { int info ;

Node*next;

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

Node*create_New_Node( int) ;

void Insert_End( Node*);

void Display( Node*);

int main()

{ front=rear=NULL;

int inf ; char ch='y';

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

{ cout<<"\nEnter Information for the new node....";

cin>>inf;

newptr=create_New_Node(inf);

if(newptr==NULL)

{cout<<"\nCannot create new node !!!Aborting!!\n";


exit(1);}

Insert_End ( newptr);

cout<<"\nNow the Queue(Front..to..Rear)is:\n";

Display ( front ) ;
cout<<"\n Press Y to enter more nodes.N to
exit...";

cin>>ch;

return 0;

Node*Create_New_Node( int n)

{ ptr=new Node;

ptr->info=n; ptr->next=NULL;

return ptr; }

void Insert_End( Node*np)

{ if(front==NULL)front=rear=np;

else

{ rear->next=np;

rear=np; }

void Display(Node*np)

{ while(np!=NULL)

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

np=np->next;

cout<<"!!!\n";

}
Output- 21

Enter INFOrmation for the new node…..5

Now the Queue(Front……to….Rear) is:

5->!!!!

Press Y to enter more nodes, N to exit…..Y

Enter INFOrmation for the new node…..12

Now the Queue(Front…..to….rear) is:

5->12->!!!!

Press Y to enter more nodes, N to exit


Documentation-22
_______________________________________
Aim –
DELETION FROM A LINKED-QUEUE

Header files used –


iostream.h,process.h

Version of C++ -
Turbo C++ 3.o

Computer system –
Windows 7
Algorithm
1. If front= NULL Then

2. Print “Queue Empty”

3. Else
{
4. ITEM= front->INFO

5. If front= rear Then


{

6. Front=rear=NULL
}
7. Else

8. Front=front->LINK

}
9. END
Source code- 22
#include<iostream.h>

#include<process.h>

struct Node { int info ;

Node*next;

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

Node*Create_New_Node(int) ;

void Insert(Node*);

void Display(node*);

void DelNode_Q();

int main()

{ front=rear=NULL;

int inf; char ch='y')

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

{ cout<<"\nEnter INFOrmation for the new node...";

cin>>inf;

newptr=Create_New_Node(inf);

if(newptr==NULL)

{ cout<<"\nCannot create new node!!!Aborting!!\n";


exit(1);

Insert(newptr);

cccout<<"\nPress Y to enter more nodes,N to


exit...\n";
cin>>ch;

system("cls");

do
{ cout<<"\n The Linked-Queue now is
(Front...to...Rear):L\n";

Display(front) ;

cout<<"want to delete first node/ (y/n)....";

cin>>ch;

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

DelNode_Q();

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

Return 0;

Node*Create_New_Node( int n)

{ ptr = new Node;

ptr-> info=n; ptr->next=NULL;

return ptr; }

void Insert_End( Node*np)

{ if(front==NULL){front=rear=np;}

else

{ rear->next=np;

rear=np; }
}

Void DelNode_q()

{ if( front == NULL)cout<<”UNDERFLOW !!!\n”;

Else{ ptr = front;

Front = front -> next ;

delete ptr ;}

void Display(Node*np)

{ while (np!=NULL)

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

np=np->next;

cout<<"!!!\n";

}
Output- 22
Enter INFOrmation for the new node… 15

Press Y to enter more nodes,N to exit…

The Linked=Queue now is(Front…to…rear);

3-<4->8->9->15!!!!

Want to delete first node ?(y/n)…y

The Linked-Queue now is (Front……to…Rear);

4->8->9->15!!!!!

Want to delete first node ?(y/n)…y

The Linked-Queue now is (Front…to…Rear);

8->9->15!!!!

Want to delete first node?(y/n)… n


S
QL
PROGRAMS
Que.1. Given the following student relation :

Relation Student
No. Name Age Department Date of Fee Sex
Adm.
1. Apnkaj 24 Computer 10/01/97 120 M
2. Shalini 21 History 24/03/98 200 F
3. Sanjay 22 Hindi 12/12/96 300 M
4. Sudha 25 History 01/07/99 400 F
5. Rakesh 22 Hindi 05/09/97 250 M
6. Shakeel 30 History 27/06/98 300 M
7. Surya 34 Computer 25/02/97 210 M
8. shikha 23 hindi 31/07/97 200 F

Write SQL command for(a)to(f) and write output for


(g)..

(a) To show all information about the students of


History Department.
(b) To list names of female student who are in hindi
department.
(c) To list names of all student with their date of
admission in ascending order.
(d) To display Student’s name,fee,age<23.
(e) To count the number of student with age<23.
(f) To insert a new row in the STUDENT table with the
following data:
9,”Zaheer”,36,”computer”,{12/03/97},230,”M”
(g) Give the Ouput of the following SQL statements.

(i)Select COUNT (distinct department)from STUDENT ;

(ii) Select MAX (age)from STUDENT where sex =”F”;

(iii) select AVG(fee)from STUDENT where


Dateofadm<{01/1/98}
(iv)select SUM (Fee) From STUDENT where
Dateofadm<{01/01/98}

SOLUTION :

(a) SELECT*FROM Student

WHERE Department =” History”;

(b) SELECT Name FROM Student


WHERE sex=”F” and
Department=”Hindi”;
(c) SELECT Name FROM Student
ORDER BY Dateofadm ;
(d) SELECT Name,fee,age
FROM Student
WHERE sex=”M”;
(e) SELECT COUNT(*)FROM Student
WHERE Age<23;
(f) INSERT INTO Student

VALUES(,”Zaheer”,36,”computer”,{12/03/97},230,”M);

(g) (i)3 (ii)25 (iii)236 (iv)1080


Que.2. Write SQL command for(a)to(f) and write output
for (g)..

HOSPITAL
No. Name Age Department Date of Fee Sex
Adm.
1. Sandeep 65 Surgery 23/02/98300 M
2. Ravina 24 Orthopedia 20/01/98200 F
3. Karan 45 Orthopedia 19/02/98200 M
4. Tarun 12 Surgery 01/01/98300 M
5. Zubin 36 ENT 12/01/98250 M
6. Kratika 16 ENT 24/02/98300 F
7. Ankita 29 Cardiology 20/02/98800 F
8. Zareen 45 Gynecology 22/02/98300 F
9. Kush 19 Cardiology 13/01/98800 M
10. shadilya 31 Nuclear 19/02/98400 F
medicine
(a) To show all information about the patients of
Cardiology Department.
(b) To list names of female patients who are in
Orthopedia department.
(c) To list names of all patients with their date of
admission in ascending order.
(d) To display patients name,charge,age for only male
patients.
(e) To count the number of patients with Age > 30 .
(f) To insert a new row in the HOSPITAL table with
the following data:

11,”Mustafa”,37,”ENT”<{25/2/98}’250,”M”;
SOLUTION:

(a) SELECT*FROM HOSPITAl

WHERE Department=” Cardiology”;

(b) SELECT Name FROM HOSPITAl

WHERE Department=” Orthopedia” AND Sex=”F”;


(c) SELECT Name HOSPITAl

ORDER BY DateofAdm;

(d) SELECT Name,charge,Age HOSPITAl


WHERE sex=”M”;
(e) SELECT COUNT(*)FROM HOSPITAl
WHERE Age<30;
(f) INSERT INTO Hospital
VALUES(11,”Mustafa”,37,”ENT”<{25/2/98}’250,”M”);
Que.3.Write SQL command for(a)to(f) and write output
for (g)..

BOOKS
Book Book_name Author_name Publishers Price Type Qt
_Id y
C0001 Fast cook Lata kapoor EPB 355 cookery 5
F0001 The tears William First 650 Fiction 20
publisher
T0001 My first c++ Brain EPB 350 Text 10
brooke
T0002 C++ brain A.W.Rossaie TDH 350 Text 15
works
F0002 Thunderboltt Anna First 750 fiction 50
roberts publisher

ISSUED
Book_Id Quantity_Issued
T0001 4
C0001 5
F0001 2

Write SQL Queries for(a) to (g)


(a)To show Book name,author name price of books
of first publisher
(b) To list the names from books of text type.
(c)To display the names and price from books in
ascending order of their price.
(d)to increase the price of all books of EPB
publisher by 50.
(e)to display the books _Id,book_name and
Quantity_issued for all books which have been
issued.
(f)to insert a new row in the table issued
having the following data:”f0003”,1
SOLUTION:
(a) SELECT Book_name,Author_name, price
FROM Books
WHERE publisher=” First publisher”;
(b) SELECT Book_name
FROM Books
WHERE Type=” Text”;
(c) SELECT Book_name,price
FROM Books
ORDER BY price;
(d) UPDATES Books
SET price=price +50
WHERE publishers=”EPB”;
(e) SELECT Books.Book_Id,Book_name,
Quantity_Issued
FROM Books, ISSUED
WHERE Books.Book_Id= Issued.Book_Id;
(f) INSERT INTO Issued
VALUES(“F0003”,”1);
Bibliography
(i). C++ computer science book by Sumita Arora.

You might also like