Archive
C++ Program to implement File Handling by using ifstream & ofstream (Prg-2) – Q30
Q30. Program to implement Function Overloading:
Write a paragraph (Atleast 2 lines of sentences) in a file. Read the same file in three different ways –
i) Reverse the whole paragraph.
ii) Line by Line Reverse the whole para.
iii) Break the pragraph int two equal halves. Reverse the second half of the pargaraph.
… from College notes (BCA/MCA assignments):
#include <string.h>
#include <fstream.h>
#include <conio.h>
void main(){
char para[500], ln[80];
int len, i, half;
clrscr();
cout<<"\n Enter a Paragraph (Quit by ^Z):-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cin.getline(para, 500, '^Z');
len = strlen(para);
ofstream fout;
fout.open("para.txt", ios::out);
fout.write((char *) ¶, len);
fout.close();
ifstream fin;
cout<<"\n Reverse the Whole Paragraph:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~";
fin.open("para.txt", ios::in);
fin.read((char *) ¶, sizeof(para));
len = strlen(para);
for(i=len; i>=0; i--)
cout<<para[i];
fin.close();
getch();
cout<<"\n Line by line Reverse the Whole Paragraph:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
fin.open("para.txt", ios::in);
while(fin){
fin.getline(ln, 80);
len = strlen(ln);
for(i=len; i>=0; i--)
cout<<ln[i];
cout<<endl;
}
fin.close();
getch();
cout<<"\n Breaking The Para Into 2 halves and Reversing the 2nd Para:-";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
fin.open("para.txt", ios::in);
fin.read((char *) ¶, sizeof(para));
len = strlen(para);
half = len / 2;
for(i=0; i<=half; i++)
cout<<para[i];
for(i=len; i>half; i--)
cout<<para[i];
fin.close();
getch();
}
Output:
Enter a Paragraph (Quit by ^Z):-
My name is Manoj Pandey.
I study in APEEJAY Institute.
I’m Doing MCA from there.
I live in Dwarka Sec. 6.
^Z
Reverse the Whole Paragraph:-
.6 .ceS akrawD ni evil I
.ereht morf ACM gnioD m’I
.etutitsnI YAJEEPA ni yduts I
.yednaP jonaM si eman yM
Line by line Reverse the Whole Paragraph:-
.yednaP jonaM si eman yM
.etutitsnI YAJEEPA ni yduts I
.ereht morf ACM gnioD m’I
.6 .ceS akrawD ni evil I
Breaking The Para Into 2 halves and Reversing the 2nd Para:-
My name is Manoj Pandey.
I study in APEEJAY Institute.
.6 .ceS akrawD ni evil I
.ereht morf ACM gnioD m’I
C++ Program to implement File Handling by using ifstream & ofstream (Prg-1) – Q29
Q29. Program to implement File Handling by using ifstream & ofstream:
Get the Rollno, Names and Marks Obtained of N students. The data should be stored in “RESULT” data file.
Use the same file to print the result along with aggregate percentage for all the students.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
class Cstud{
private:
char name[10];
int roll, marks;
public:
void getdata();
void putdata();
int calc() const;
};
void Cstud :: getdata(){
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Roll No: ";
cin>>roll;
cout<<"\n Enter Marks: ";
cin>>marks;
}
int Cstud :: calc() const{
return marks;
}
void Cstud :: putdata(){
cout<<"\n Name: "<<name;
cout<<"\n Roll No: "<<roll;
cout<<"\n Marks: "<<marks;
}
void main(){
int n, tot, avg;
Cstud Ostu1, Ostu2;
clrscr();
cout<<"\n How many Students: ";
cin>>n;
ofstream fout;
fout.open("result.txt", ios::out);
for(int i=0; i<n; i++){
cout<<"\n Student No: "<<i+1;
Ostu1.getdata();
fout.write((char *) &Ostu1, sizeof(Ostu1));
}
fout.close();
ifstream fin;
fin.open("result.txt", ios::in);
tot = 0;
clrscr();
cout<<"\n Data read from File:- \n";
for(i=0; i<n; i++){
fin.read((char *) &Ostu2, sizeof(Ostu2));
cout<<"\n\n Student No: "<<i+1;
Ostu2.putdata();
tot += Ostu2.calc();
}
avg = tot/n;
cout<<"\n\n Total marks: "<<tot;
cout<<"\n\n Average Marks: "<<avg;
fin.close();
getch();
}
Output:
How many Students: 5
Student No: 1
Enter Name: manoj
Enter Roll No: 204
Enter Marks: 200
Student No: 2
Enter Name: Bhanu
Enter Roll No: 104
Enter Marks: 300
Student No: 3
Enter Name: Nitin
Enter Roll No: 214
Enter Marks: 350
Student No: 4
Enter Name: Prakash
Enter Roll No: 304
Enter Marks: 250
Student No: 5
Enter Name: Shilpi
Enter Roll No: 123
Enter Marks: 400
Student No: 1
Name: Manoj Roll No: 204 Marks: 200
Student No: 2
Name: Bhanu Roll No: 104 Marks: 300
Student No: 3
Name: Nitin Roll No: 214 Marks: 350
Student No: 4
Name: Prakash Roll No: 304 Marks: 250
Student No: 5
Name: Shilpi Roll No: 123 Marks: 400
Total marks: 1500
Average Marks: 300
C++ Program to implement Exception Handling by using TRY CATCH – Q28
Q28. Program to implement Exception Handling by using TRY CATCH:
Write a Boolean function that returns TRUE/FALSE if the unsigned int argument passed to it is a Leap Year or Non Leap Year. The function must throw an “Out Of Range” exceptio if its argument does not lie between 0 and 2100.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
enum boolean{false, true} bool;
class Cyear{
private:
int dd, mm, yy;
public:
void getDate();
int isLeap();
void putDate();
};
void Cyear :: getDate(){
cout<<"\n Enter Date (dd mm yyyy): ";
cin>>dd>>mm>>yy;
try{
if( (yy < 0) || (yy > 2100) )
throw "Out of Range";
}
catch(char *errmsg){
cout<<"\n ERROR: "<<errmsg;
}
}
int Cyear :: isLeap (){
return ( (yy % 400 == 0) || ((yy % 4 == 0) && (yy % 100 !=
0)) );
}
void Cyear :: putDate(){
if(isLeap())
cout<<"\n Is a leap year.";
else
cout<<"\n Is not leap year.";
}
void main(){
clrscr();
Cyear Odt;
Odt.getDate();
Odt.putDate();
getch();
}
Output:
Enter a Date (dd mm yyyy): 18 11 2004
Is a Leap Year.
Enter a Date (dd mm yyyy): 18 11 2290
ERROR: Out of Range.
Is not a Leap Year.
C++ Program to implement Generic Classes for maintaining Linked List – Q27
Q27. Program to implement Generic Classes for maintaining Linked List:
Create a Template class that implements a singly-linked list. The linked list class should include following member functions-
push_front( ), push_back( ), push_at_any( ), remove_first( ), remove_last( ) & remove_at _any( ).
There may be a private data member size to keep track of no. of items remaining at any given point of time.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
template <class T>
class NODE{
private:
T elm, key;
NODE *next;
public:
void addNodeBeg(NODE **);
void addNodeEnd(NODE **);
void addNodePos(NODE **);
void deleteNode(NODE **);
void displayList(NODE *);
};
template <class T>
void NODE<T> :: addNodeBeg(NODE<T> **node){
NODE *temp;
cout<<"\n Enter a Value: ";
cin>>key;
if(*node == NULL){ // list is Empty.
*node = new(NODE);
(*node)->elm = key;
(*node)->next = NULL;
}
else{ // list is not empty.
temp = new(NODE);
temp->elm = key;
temp->next = *node;
*node = temp;
}
}
template <class T>
void NODE<T> :: addNodeEnd(NODE<T> **node){
NODE *temp;
cout<<"\n Enter a Value: ";
cin>>key;
if(*node == NULL){ // list is Empty.
*node = new(NODE);
(*node)->elm = key;
(*node)->next = NULL;
}
else{ // list is not empty.
temp = *node;
while(temp->next != NULL)
temp = temp->next;
temp->next = new(NODE);
temp = temp->next;
temp->elm = key;
temp->next = NULL;
}
}
template <class T>
void NODE<T> :: addNodePos(NODE<T> **node){
NODE *temp, *ntemp;
int pos, i=1;
cout<<"\n Enter a Position No.: ";
cin>>pos;
cout<<"\n Enter a Key: ";
cin>>key;
if(*node == NULL){ // list is Empty.
cout<<"\n List is Empty.";
return ;
}
temp = *node;
while(1){
if( (temp->next == NULL) || (i == pos) )
break;
temp = temp->next;
i++;
}
ntemp = new(NODE);
ntemp->elm = key;
ntemp->next = temp->next;
temp->next = ntemp;
}
template <class T>
void NODE<T> :: deleteNode(NODE<T> **node){
NODE *temp, *prev;
int i=0;
cout<<"\n Enter an Element of List to Delete: ";
cin>>key;
temp = *node;
prev = temp;
while(1){
if(*node == NULL){ // list is Empty.
cout<<"\n List is Empty.";
return;
}
else if(temp->elm == key){ // Element Found.
if(i == 0){ // if Element is First Node.
(*node) = (*node)->next;
cout<<"\n First Node Deleted.";
return;
}
else
break;
}
else if(temp->next == NULL){
cout<<"\n Element not Found.";
return;
}
i++;
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
cout<<"\n Element Deleted.";
}
template <class T>
void NODE<T> :: displayList(NODE<T> *node){
int i=1;
cout<<"\n\t List: ";
if(node == NULL)
cout<<" List is Empty.";
while(node != NULL){
cout<<"\t"<<node->elm;
node = node->next;
i++;
}
}
int _menu(){
int _ch;
cout<<"\n List Type:- \n";
cout<<"\n 1 -> Integer.";
cout<<"\n 2 -> Float.";
cout<<"\n 3 -> Character.";
cout<<"\n Enter your choice: ";
cin>>_ch;
return (_ch);
}
void main(){
int ch1, ch2;
NODE<int> int_list;
NODE<float> float_list;
NODE<char> char_list;
NODE<int> *int_start = NULL;
NODE<float> *float_start = NULL;
NODE<char> *char_start = NULL;
while(1){
clrscr();
cout<<"\n LINK LIST PROGRAM";
cout<<"\n ~~~~~~~~~~~~~~~~~ \n";
cout<<"\n 1 -> Add Node at Begining.";
cout<<"\n 2 -> Add Node at End.";
cout<<"\n 3 -> Add Node at Position.";
cout<<"\n 4 -> Delete Node.";
cout<<"\n 5 -> Display List.";
cout<<"\n 6 -> Exit.";
cout<<"\n Enter your choice: ";
cin>>ch1;
switch(ch1){
case 1: // Add node at Begining.
ch2 = _menu();
switch(ch2){
case 1:
cout<<"\n Enter an Integer
Value: ";
int_list.addNodeBeg(&int_start);
break;
case 2:
cout<<"\n Enter a Float Value:”;
float_list.addNodeBeg(&float_start);
break;
case 3:
cout<<"\n Enter a Character: ";
char_list.addNodeBeg(&char_start);
break;
default:
cout<<"\n Wrong Entry. No
operation Done.";
}
break;
case 2: // Add node at End.
ch2 = _menu();
switch(ch2){
case 1:
cout<<"\n Enter an Integer
Value: ";
int_list.addNodeEnd(&int_start);
break;
case 2:
cout<<"\n Enter a Float Value:";
float_list.addNodeEnd(&float_start);
break;
case 3:
cout<<"\n Enter a Character: ";
char_list.addNodeEnd(&char_start);
break;
default:
cout<<"\n Wrong Entry. No
operation Done.";
}
break;
case 3: // Add node at Position.
ch2 = _menu();
switch(ch2){
case 1:
cout<<"\n Enter an Integer
Value: ";
int_list.addNodePos(&int_start);
break;
case 2:
cout<<"\n Enter a Float Value:";
float_list.addNodePos(&float_start);
break;
case 3:
cout<<"\n Enter a Character: ";
char_list.addNodePos(&char_start);
break;
default:
cout<<"\n Wrong Entry. No
operation Done.";
}
break;
case 4: // Delete Node.
ch2 = _menu();
switch(ch2){
case 1:
cout<<"\n Enter an Integer
Value: ";
int_list.deleteNode(&int_start);
break;
case 2:
cout<<"\n Enter a Float Value:";
float_list.deleteNode(&float_start);
break;
case 3:
cout<<"\n Enter a Character: ";
char_list.deleteNode(&char_start);
break;
default:
cout<<"\n Wrong Entry. No
operation Done.";
}
break;
case 5: // Display List.
cout<<"\n\n Integer List: - ";
int_list.displayList(int_start);
cout<<"\n\n Float List: - ";
float_list.displayList(float_start);
cout<<"\n\n Character List: - ";
char_list.displayList(char_start);
break;
default:
exit(1);
}
getch();
}
}
Output:
LINK LIST PROGRAM
1 -> Add Node at Begining.
2 -> Add Node at End.
3 -> Add Node at Position.
4 -> Delete Node.
5 -> Display List.
6 -> Exit.
Enter your choice: 5
Integer List: –
List: 3 90 44 45 23 45
Float List: –
List: 90.449997 23.440001 12.34
Character List: –
List: e r
C++ Program to implement Generic classes for maintaining Stack – Q26
Q26. Program to implement Generic classes for maintaining Stack:
Implement Stacks using generic classes with member functions PUSH, POP and DISPLAY. The Program should show implementation of Stack class with at least three different objects.
… from College notes (BCA/MCA assignments):
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
template <class T>
class Cstack{
private:
T arr[10];
int ptr;
public:
void init(){
ptr = -1;
}
void push(T, int);
T pop(int &);
void clear();
void display(int);
};
template <class T>
void Cstack<T> :: push(T elm_push, int t_ptr){
if(t_ptr >= 9)
cout<<"\n STACK OVERFLOW.";
else
arr[++t_ptr] = elm_push;
}
template <class T>
T Cstack<T> :: pop(int &t_ptr){
T elm_pop;
if(t_ptr <= -1)
cout<<"\n STACK UNDERFLOW.";
else{
elm_pop = arr[t_ptr--];
cout<<"\n POPPED value is : "<<elm_pop;
return elm_pop;
}
return 0;
}
template <class T>
void Cstack<T> :: clear(){
ptr = -1;
cout<<"\n STACK Cleared.";
}
template <class T>
void Cstack<T> :: display(int t_ptr){
cout<<"\n STACK: ";
if(t_ptr == -1)
cout<<" Empty";
else
for(int i=0; i<=t_ptr; i++)
cout<<arr[i]<<" ";
}
int _menu(){
int _ch;
cout<<"\n STACK Type:- \n";
cout<<"\n 1 -> Integer.";
cout<<"\n 2 -> Float.";
cout<<"\n 3 -> Character.";
cout<<"\n Enter your choice: ";
cin>>_ch;
return (_ch);
}
void main(){
int ch, i_ptr = -1, f_ptr = -1, c_ptr = -1;
int i_num;
float f_num;
char c_num;
Cstack<int> Oint;
Cstack<float> Ofloat;
Cstack<char> Ochar;
Oint.init();
while(1){
clrscr();
cout<<"\n STACK - OPERATION";
cout<<"\n ~~~~~~~~~~~~~~~~~ \n";
cout<<"\n 1 -> PUSH";
cout<<"\n 2 -> POP";
cout<<"\n 3 -> CLEAR";
cout<<"\n 4 -> DISPLAY";
cout<<"\n 5 -> EXIT";
cout<<"\n Enter your choice: ";
cin>>ch;
switch(ch){
case 1: // PUSH
ch = _menu();
switch(ch){
case 1:
cout<<"\n Enter an Integer Value
to push: ";
cin>>i_num;
Oint.push(i_num, i_ptr);
i_ptr++;
break;
case 2:
cout<<"\n Enter a Float Value to
push: ";
cin>>f_num;
Ofloat.push(f_num, f_ptr);
f_ptr++;
break;
case 3:
cout<<"\n Enter a Character to
push: ";
cin>>c_num;
Oint.push(c_num, c_ptr);
c_ptr++;
break;
default:
cout<<"\n Wrong Choice. No
Operation Done.";
}
break;
case 2: // POP
ch = _menu();
switch(ch){
case 1:
Oint.pop(i_ptr);
i_ptr--;
break;
case 2:
Ofloat.pop(f_ptr);
f_ptr--;
break;
case 3:
Ochar.pop(c_ptr);
c_ptr--;
break;
default:
cout<<"\n Wrong Choice. No
Operation Done.";
}
break;
case 3: // CLEAR
ch = _menu();
switch(ch){
case 1:
Oint.clear();
i_ptr = -1;
break;
case 2:
Ofloat.clear();
c_ptr = -1;
break;
case 3:
Ochar.clear();
c_ptr = -1;
break;
default:
cout<<"\n Wrong Choice. No
Operation Done.";
}
break;
case 4: // DISPLAY
cout<<"\n\n Integer Stack:-";
Oint.display(i_ptr);
cout<<"\n\n Float Stack:-";
Ofloat.display(f_ptr);
cout<<"\n\n Character Stack:-";
Ochar.display(c_ptr);
break;
case 5: // EXIT
cout<<"\n\n You chose to EXIT";
getch();
exit(1);
default:
cout<<"\n\n Enter an appropriate choice.";
} // end of switch.
getch();
} // end of while.
}// end of main.
Output:
STACK – OPERATION
1 -> PUSH
2 -> POP
3 -> CLEAR
4 -> DISPLAY
5 -> EXIT
Enter your choice: 1
STACK Type:-
1 -> Integer.
2 -> Float.
3 -> Character.
Enter your choice: 1
Enter an Integer Value to push: 43
STACK – OPERATION
1 -> PUSH
2 -> POP
3 -> CLEAR
4 -> DISPLAY
5 -> EXIT
Enter your choice: 4
Integer Stack:-
STACK: 43 119 23
Float Stack:-
STACK: 34.549999 78.900002
Character Stack:-
STACK: a n r




