You are on page 1of 6

ASSIGNMENT 3

Submitted by :-YOGESH YADAV ROLL NO.


R610215057
SAP ID 500048466
Q1. File handling questions (assume the language to be
C++_
(a) Why do we use file handling ?
(b) Name three classes for file stream operations.
(c) Illustrate opening, closing, reading and writing a file
with the help of a C++ program.
Ans.1
(a.) Need of File Handling: File handling in C++ is used to
store the data permanently to our computers Hard Disk. It
is used to permanently store the data to our computer and
also to transfer the input output data to other computers.
File handling can also be used to prepare some kind of list
using some calculations like preparing award list using c+
+ program which include student name, internal, external,
total marks and percentage. The other use of file
handling is when we design a calculator in c++ File
Handling can be used to show the history of the
calculations performed by him by taking record of the
input and data of calculator in file named history.

-----------------------------------------------------------------------------------------(b.) Classes for file stream operations:


1.)
Ifstream

2.)
3.)
4.)

ofstream
fstream
filebuf
5.)
fstreambase
(c.) #include<conio.h>
#include<fstream.h>
int main()
{
ofstream fout(info) ;
stream fout
char name[30], ch ;
int marks ;

//connect info file for output

cout<<Enter Your Name<<endl ;


cin.get(name,30) ;
cout<<Enter Your Marks<<endl ;
cin>>marks ;
cin.get(ch) ;
//to empty input buffer
fout<<name<< marks ;
fout.close() ;
ifstream fin(info) ;
stream fin
fin.seekg(0) ;
beginning of file

//writing data to file


//closing the file
//connect info file for input

//to bring file pointer at

fin.get(name,30) ;
//read name from info file
fin.get(ch) ;
//empty buffer
fin >>marks ;
//read marks from info file
fin.get(ch) ;
cout<<Name: <<name ;
//display contents of file
info
cout<<Marks: <<marks ;
fin.close() ;
//close the file
return 0;
}

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

Q2. Write down the algorithm for BFS and DFS and dry
run it for the below mentioned graph.

Ans.2 ALGORITHM FOR BFS TRAVERSAL IN A GRAPH:


1.)
Choose a starting vertex
2.)
Create an empty Queue
3.)
Put the starting vertex in Q
4.)
Dequeue a vertex from Q and enqueue the unvisited
vertices joining
dequeued vertex in queue Q.
5.)
Mark the dequeued vertex as visited
6.)
If queue is empty
Exit
Else
Go to step 4.
Dry Run for given graph
Let starting vertex is A in given graph.

Empty Queue is Q.
Now status of Queue Q is Q ={A}
// {=front of
queue and }=back
of queue
In given graph vertex dequeued is A and vertices
joining A are B,D,G
Now Q={B,D,G} Visited vertices :- A
NOW vertex dequeued =B Q={D,G,E,F} Visited
vertices :- A,B
THEN vertex dequeued =D Q={G,E,F} Visited
vertices :- A,B,D
THEN vertex dequeued =G Q={E,F} Visited vertices
:- A,B,D,G
THEN vertex dequeued =E Q={F} Visited vertices :A,B,D,G,E
THEN vertex dequeued =F Q={C} Visited vertices :A,B,D,G,E,F
THEN vertex dequeued =C Q={H} Visited vertices :A,B,D,G,E,F,C
THEN vertex dequeued =H Q={} Visited vertices :A,B,D,G,E,F,C,H
Queue is empty exiting.

ALGORITHM FOR DFS IN GRAPH:


1.)
Mark all vertices as Ready state and choose a starting
vertex.
2.)
Create a stack and push the starting vertex in stack and
change its state as waiting.
3.)
Repeat steps 4 until stack is empty.
4.)
Pop the top vertex of stack and push all neighbours of
poped vertex in stack and change their status to waiting
change status of poped vertex to visited

5.)

Exit

Dry Run for given graph


{=top of stack.
Firstly, Ready vertices ={A,B,C,D,E,F,G,H} Waiting vertices in
stack ={} Visited vertices={}
Let A is starting vertex so,
Now, Ready vertices ={B,C,D,E,F,G,H} Waiting vertices in
stack ={A} Visited vertices={}
Then Ready vertices ={C,E,F,H} Waiting vertices in stack
={B,G,D} Visited vertices={A}
Then Ready vertices ={C,H} Waiting vertices in stack
={E,F,G,D,} Visited vertices={A,B}
Then Ready vertices ={C,H} Waiting vertices in stack ={F,G,D,}
Visited vertices={A,B,E}
Then Ready vertices ={H} Waiting vertices in stack ={C,G,D,}
Visited vertices={A,B,E,F}
Then Ready vertices ={} Waiting vertices in stack ={H,G,D,}
Visited vertices={A,B,E,F,C}
Then Ready vertices ={} Waiting vertices in stack ={G,D,}
Visited vertices={A,B,E,F,C,H}
Then Ready vertices ={} Waiting vertices in stack ={D,} Visited
vertices={A,B,E,F,C,H,G}
Then Ready vertices ={} Waiting vertices in stack ={} Visited
vertices={A,B,E,F,C,H,G,D}
Stack is empty exiting.

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

You might also like