Sei sulla pagina 1di 31

Class, Constructor and Destructor

C1. Define a class student with the following descriptions:


Private members
Data members
Data type
Roll number
integer
Name
string
mark
float of 5
avgmark
float
grade
char
CalAvg() : a function that accepts a float of 5 elements and return their average.
CalGrade(): a function that accepts a float and returns the corresponding grade. (>=75 A, >=50&<75 B
and <50 C)
Public member functions of class student;

o
o
o
o

getData() : accepts roll number, name and marks in 5 subjects. Invokes the functions calAvg() to store
average marks in avgmark and calGrade() to store grade.
putData() : to display roll number, name, average mark and grade.

Implement the above class for 10 students.


Display the data of the highest scorer.
Display number of students in each grade.
Display the data of a given roll number.

#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollNo;
char name[20];
float mark[5];
float avgmark;
char grade;
void calAvg()
{
float tot;
for(int i = 0; i < 5; i++)
{
tot += mark[i];
}
avgmark = tot/5;
}
char calGrade()
{
if(avgmark >= 75)
grade = 'A';
else if(avgmark >= 50 && avgmark < 75)
grade = 'B';
else
grade = 'C';
return grade;
}
public:
void getData()
{
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
cout << "Enter 5 marks: ";
for(int i = 0; i < 5; i++)
{
cin >> mark[i];
}
calAvg();
calGrade();
}
void putData()
{
cout << "Roll number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Average marks: " << avgmark << endl;
cout << "Grade: " << grade << endl;
}
int getRollNo()
{return rollNo; }
int getGrade()

{return grade; }
int getAvgmark()
{return avgmark; }
};
void main()
{
clrscr();
student derps[10];
cout << "Enter details for 10 students: " << endl;
for(int i = 0; i < 10; i++)
derps[i].getData();
int
int
int
int

nofA = 0;
nofB = 0;
nofC = 0;
highInd = 0;

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


{
if(derps[j].getGrade() == 'A') // Finding no. of students by grade
nofA++;
else if(derps[j].getGrade() == 'B')
nofB++;
else
nofC++;
if(derps[j].getAvgmark() > derps[highInd].getAvgmark())
highInd = j;
}
cout
cout
cout
cout

<<
<<
<<
<<

"Number of students in each grade are: ";


"A: " << nofA << endl;
"B: " << nofB << endl;
"C: " << nofC << endl;

cout << "The records for highest-scoring student is: " << endl;
derps[highInd].putData();
int num;
cout << "Enter roll number: " << endl;
cin >> num;
int flag = 0;
for(int k = 0; k < 10; k++)
{
if(derps[k].getRollNo() == num)
{
derps[k].putData();
flag = 1;
break;
}
}
if(flag == 0)
cout << "Student with entered roll number not found. " << endl;
getch();
}

C2. Define a class Play with the following descriptions:


Private members
Data members
Data type
PlayCode
long
PlayTitle
string
Duration
integer
No_Scenes
integer
Public member functions of class Play:
A constructor to initialise Duration as 30, No_Scenes as 5.
New_Data()

to input the values for the data members PlayCode and PlayTitle.
More_Data()

to assign the values of Duration and No_Scenes with the help of corresponding values
passed as parameter to this function.
Show_Data()

to display all the data stored in the data members.


Implement the above class for 5 elements.

#include<iostream.h>
#include<conio.h>
class Play
{
long int playCode;
char playTitle[20];
int duration;
int no_scenes;
public:
Play()
{
duration = 30;
no_scenes = 5;
}
void new_data()
{
cout << "Enter playCode: " << endl;
cin >> playCode;
cout << "Enter playTitle: " << endl;
cin >> playTitle;
}
void more_data(int dur, int nos)
{
duration = dur;
no_scenes = nos;
}
void show_data()
{
cout << "playCode: " << playCode << endl;
cout << "playTitle: " << playTitle << endl;
cout << "Duration: " << duration << endl;
cout << "Number of scenes: " << no_scenes << endl;
}
};
void main()
{
clrscr();
Play playList[5];
for(int i = 0; i < 5; i++)
{
char ch;
int dur, nos;
playList[i].new_data();
cout << "Would you like to enter more data? Answer y/n" << endl;
cin >> ch;
if(ch == 'y')
{
cout << "Enter duration: "; cin >> dur;
cout << "Number of scenes: "; cin >> nos;
playList[i].more_data(dur, nos);
}
}
for(int j = 0; j < 5; j++)
playList[j].show_data();
getch();
}

C3. Define a class Tour in C++ with the following descriptions:


Private members
Data members
Data type
TCode
string
NoofAdults
integer
NoofKids
integer
Kilometer
integer
TotalFare
integer
Public members:
A constructor to assign initial values as NULL to string type data members and 0 to all integer type data members.
A
function AssignFare() which assigns the total fare to TotalFare, given fare per adult as follows:
Distance (Km)
Fare (Rs)
<500
100
>=500 & < 750
150
>=750
200
Each kid gets 50% discount on the above fare.
A function EnterTour() to input the values of data members TCode, NoofAdults, NoofKids and Kilometer.
Invoke the function AssignFare() function.
A function DisplayTour() to display all the data stored in the data members.

O
O

#include<iostream.h>
#include<conio.h>
class Tour
{
char TCode[20];
int noOfAdults;
int noOfKids;
int kilometer;
int totalFare;
public:
Tour()
{
for(int i = 0; i < 20; i++)
TCode[i] = '\0';
noOfAdults = 0;
noOfKids = 0;
kilometer = 0;
totalFare = 0;
}
void assignFare()
{
int fare;
if(kilometer < 500)
fare = 100;
else(kilometer >= 500 && kilometer < 750)
fare = 150;
else
fare = 200;
totalFare = noOfAdults*fare + noOfKids*0.5*fare;
}
void enterTour()
{
cout << "Enter TCode: ";
cin >> TCode;
cout << "Enter number of adults: ";
cin >> noOfAdults;
cout << "Enter number of kids: ";
cin >> noOfKids;
cout << "Enter distance: ";
cin >> kilometer;
assignFare();
}
void displayTour()
{
cout << "TCode: " << TCode << endl;
cout << "Number of adults: " << noOfAdults << endl;
cout << "Number of kids: " << noOfKids << endl;
cout << "Distance: " << kilometer << endl;
cout << "Total fare: " << totalFare;
}
}

Polymorphism
P1. Declare overloaded functions Large to find the largest element.
a. Large (int n) : Returns the number itself.
b. Large (int a,int b) : Returns the largest among two numbers a and b.
c. Large(int A[ ]) : Returns the largest value stored in the array A
#include<iostream.h>
int large(int n)
{
return n;
}
int large(int a, int b)
{
if(a > b)
return a;
return b;
}
int large(int A[])
{
int i = 0;
int gr = 0;
while(A[i] != '\0')
{
if(A[i] > gr)
gr = A[i];
i++
}
return gr;
}

P2. Declare a class complex, which has two data members real and imaginary. Write overloaded functions Multiply for the
following cases:
a. Multiply(complex a, complex b) to multiply two complex numbers a and b and return a complex number
b. Multiply(complex a, int k) - to multiply the scalar k to the complex number a and return a complex number
c. Multiply(int k , complex a) - to multiply the scalar k to the complex number a and return a complex number
d. Multiply (int a, int b) to multiply two integers and return an integer.
#include<iostream.h>
#include<conio.h>
class Complex
{
int real;
int imag;
public:
Complex(int a, int b)
{real = a; imag = b;}
int getReal()
{return real;}
int getImag()
{return imag;}
}
Complex multiply(Complex a, Complex b)
{
c = a.getReal(); d = a.getImag();
e = b.getReal(); f = b.getImag();
real = c*e - d*f;
imag = c*f + d*e;
res = Complex(real, imag);
return res;
}
Complex multiply(Complex a, int k)
{
real = a.getReal()*k;
imag = a.getImag()*k;
res = Complex(real, imag);
return res;
}
Complex multiply(int k, Complex a)
{
real = a.getReal()*k;
imag = a.getImag()*k;
res = Complex(real, imag);
return res;
}
multiply(int a, int b)
{
return a*b; }

Inheritance
I1. Enter data and display information about employees and manager. Employee is the base class (Data members : employee
number, Name, member functions getdata(), putdata() ), Manager is the derived class (Data members : Department and salary,
member functions indata(), outdata()). Implement the inheritance for 5 objects.

#include<iostream.h>
#include<conio.h>
class Employee
{
int empNo;
char name[20];
public:
void getData()
{
cout << "Enter employee number: "; cin >> empNo;
cout << "Enter name: "; cin >> name;
}
void putData()
{
cout << "Employee number: " << empNo << endl;
cout << "Name: " << name << endl;
}
};
class Manager : public Employee
{
char dept[20];
int salary;
public:
void inData()
{
cout << "Enter department: "; cin >> dept;
cout << "Enter salary: "; cin >> salary;
}
void outData()
{
cout << "Department: " << dept << endl;
cout << "Salary: " << salary << endl;
}
};
void main()
{
clrscr();
Manager empList[5];
for(int i = 0; i < 5; i++)
{
char ch;
cout << "Is this a manager? Answer y/n" << endl;
cin >> ch;
if(ch == 'y')
{
empList[i].getData();
empList[i].inData();
}
else
empList[i].getData();
}
for(int j = 0; j < 5; j++)
{
empList[j].putData();
empList[j].outData();
}
getch();
}

Data Structure
D1. Create a one-dimensional array; sort the array in ascending order using Insertion Sort. Display the unsorted and sorted arrays.

#include<iostream.h>
#include<limits.h>
#include<conio.h>
void inputArr(int n, int arr[])
{
for(int i = 1; i <= n; i++)
cin >> arr[i];
}
void dispArr(int n, int arr[])
{
for(int i = 1; i <= n; i++)
cout << arr[i] << ' ';
cout << endl;
}
int insSort(int n, int arr[])
{
int tmp, j;
arr[0] = INT_MIN;
for(int i = 1; i <= n; i++)
{
tmp = arr[i];
j = i-1;
while(tmp < arr[j])
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = tmp;
}
}
void main()
{
clrscr();
int arr[100];
int n;
cout << "Enter number of elements: ";
cin >> n;
inputArr(n, arr);
insSort(n, arr);
dispArr(n, arr);
getch();
}

D2. Create a one-dimensional array; sort the array in ascending order using Bubble Sort and search for an element using Binary
Search. Delete the element if found, otherwise display appropriate error message.

#include<iostream.h>
#include<limits.h>
#include<conio.h>
int N;
void inputArr(int n, int arr[])
{
for(int i = 0; i < n; i++)
cin >> arr[i];
}
void dispArr(int n, int arr[])
{
for(int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << endl;
}
void bubbleAsc(int n, int arr[])
{
int tmp;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-1; j++)
{
if(arr[j] > arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int binSearch(int n, int num, int arr[])
{
int beg, last, mid;
beg = 0; last = n-1;
while(beg <= last)
{
mid = (beg+last)/2;
if(num == arr[mid])
return mid;
else if(num > arr[mid])
beg = mid+1;
else
last = mid-1;
}
return -1;
}
void deleteElem(int ind, int n, int arr[])
{
for(int i = ind; i < n; i++)
{
arr[i] = arr[i+1];
}
N--;
}
void main()
{
clrscr();
int arr[100];
int num;
cout << "Enter number of elements: ";
cin >> N;
inputArr(N, arr);
bubbleAsc(N, arr);
cout << "Enter element to be deleted: ";
cin >> num;
int ind = binSearch(N, num, arr);
if(ind == -1)
cout << "Element not found" << endl;
else
{
deleteElem(ind, N, arr);
dispArr(N, arr);
}
getch();
}

D3. Write the code for the following functions:


a. InputArray (int A[ ], int size) to enter the data into an array A of given size
b. DisplayArray (int A[ ], int size) to display the array elements
C. SortArray (int A[ ], int size) to sort the array lements in ascending order using Selection Sort.
d. Merge (int A[ ],int size1, int B[ ], int size2,int C[ ]) to merge the contenets of array A of size1 elements and array
B of size2 elements into a third array C in ascending order
Enter 2 arrays, sort the array and merge into a third array. Display the data stored in all three arrays.

#include<iostream.h>
#include<conio.h>
void inputArr(int n, int arr[])
{
for(int i = 0; i < n; i++)
cin >> arr[i];
}
void dispArr(int n, int arr[])
{
for(int i = 0; i < n; i++)
cout << arr[i] << ' ';
cout << endl;
}
void selSort(int n, int arr[])
{
int small, pos, tmp;
for(int i = 0; i < n-1; i++)
{
small = arr[i];
pos = i;
for(int j = i+1; j < n; j++)
{
if(arr[j] < small)
{
small = arr[j]; pos = j;
}
}
tmp = arr[i];
arr[i] = arr[pos];
arr[pos] = tmp;
}
}
void merge(int m, int A[], int n, int B[], int C[])
{
int a = 0, b = 0, c = 0;
while(a < m && b < n)
{
if(A[a] < B[b])
{
C[c] = A[a];
a++;
}
else
{
C[c] = B[b];
b++;
}
c++;
}
while(a < m)
{
C[c] = A[a];
c++; a++;
}
while(b < n)
{
C[c] = B[b];
c++; b++;
}
}
void main()
{
clrscr();
int A[100], B[100], C[200];
int m, n;
cout << "Enter number of elements in array 1 and array 2: ";
cin >> m >> n;
cout << "Enter array 1: " << endl;
inputArr(m, A);
cout << "Enter array 2: " << endl;
inputArr(n, B);
selSort(m, A);
selSort(n, B);
merge(m, A, n, B, C);
cout << "Array 1: " << endl;
dispArr(m, A);
cout << "Array 2: " << endl;

dispArr(n, B);
cout << "Merged array: " << endl;
dispArr(m+n, C);
getch();
}

D4. Declare a 2-dim array and write code for the following functions: Implement using main function.
a. InputArray (int A,int row,int col) to enter the data into an array A of given size
b. DisplayArray (int A,int row,int col) to display the array elements
c. SumRow (int A,int row,int col) to display the sum of elements of each row
d. SumCol (int A,int Col,int col) to display the sum of elements of each Column
e. SumDiag (int A,int Col,int col) to display the sum of diagonal elements , if number of rows is equal to number of
rows, display appropriate message otherwise.

#include<iostream.h>
#include<conio.h>
void inputArray(int A[][3], int row, int col)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cin >> A[i][j];
}
cout << endl;
}
}
void displayArray(int A[][3], int row, int col)
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cout << A[i][j];
cout << " ";
}
cout << endl;
}
}
void sumRow(int
{
for(int i =
{
int sum
for(int
{
sum
}
cout <<
}
}
void sumCol(int
{
for(int j =
{
int sum
for(int
{
sum
}
cout <<
}
}

A[][3], int row, int col)


0; i < row; i++)
= 0;
j = 0; j < col; j++)
+= A[i][j];
"Sum of row " << i+1 << " is: " << sum << endl;

A[][3], int row, int col)


0; j < col; j++)
= 0;
i = 0; i < row; i++)
+= A[i][j];
"Sum of column " << j+1 << " is: " << sum << endl;

void sumDiag(int A[][3], int row, int col)


{
if(row == col)
{
int sum = 0;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(i == j)
sum += A[i][j];
}
}
cout << "Sum of diagonal elements is: " << sum << endl;
}
else
{
cout << "Number of rows is not equal to number of columns." << endl;
cout << "Hence, no diagonal elements possible." << endl;

}
}
void main()
{
clrscr();
int arr[3][3];
inputArray(arr, 3, 3);
displayArray(arr, 3, 3);
sumRow(arr, 3, 3);
sumCol(arr, 3, 3);
sumDiag(arr, 3, 3);
getch();
}

D5. Declare a 2-dim array and write code for the following functions: Implement using main function.
a. InputArray (int A,int row,int col) to enter the data into an array A of given size
b. DisplayArray (int A,int row,int col) to display the array elements
c. DispTopTriangle (int A,int row,int col) to display the array elements present above the major diagonal
d. DispBottomTriangle (int A,int row,int col) to display the array elements present below the major diagonal

D6. Define a class student with the following descriptions:


Private members
Data members
Data type
Roll number
integer
Name
string
Fee
float
Public member functions of class student;
getData() : accepts roll number, name and fee.
putData() : to display roll number, name and fee

Implement the objects of the above class into a dynamic Stack


Display all the data

#include<iostream.h>
#include<conio.h>
class student
{
int rollNo;
char name[20];
float fee;
public:
void getData()
{
cout << "Enter roll number: " << endl; cin >> rollNo;
cout << "Enter name: " << endl; cin >> name;
cout << "Enter fee: " << endl; cin >> fee;
}
void putData()
{
cout << "Roll number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Fee: " << fee << endl;
}
};
struct node
{
class data;
node *next;
};
node *top = NULL;
node *createNode(student wut)
{
node *ptr = new node;
ptr -> data = wut;
pt -> next = NULL;
return ptr;
}
void push(student wut)
{
node *temp;
temp = new node;
temp -> data.getData();
temp -> next = top;
top = temp;
}
void pop()
{
if(top!=NULL)
{
node *temp = top;
top = top -> next;
delete temp;
}
else
cout << "Underflow! " << endl;
}
void display()
{
if(top == NULL)
cout << "Nothing to see here, move on." << endl;
else
{
node *ptr = top;
while(ptr != NULL)
{
ptr -> data.getData();
cout << "-->" << endl;
ptr = ptr -> next;
}
cout << "Finito. " << endl;

}
}
void main()
{
clrscr();
char inpt = 'b';
while(inpt != 'x')
{
cout << "Enter: " << endl;
cout << "a to push" << endl;
cout << "b to pop" << endl;
cout << "x to exit" << endl;
cout << "Enter your option: " << endl;
cin >> inpt;
if(inpt == 'a')
{
student stud;
stud.getData();
push(stud);
}
else if(inpt = 'b')
pop();
else
cout << "kthnxbye" << endl;
cout << "Your stack is now: " << endl;
display();
}
getch();
}

D7. Define a class student with the following descriptions:


Private members
Data members
Data type
Roll number
integer
Name
string
Date of birth
A structure in the format dd,mm,yy; dd ,mm & yy are integers.
Public member functions of class student;
Getdata() : to enter all data
Putdata() : to display all data
o Write the objects of the class into a dynamic Queue
o Write a function to pass the month number and display the data of students born in that particular month

#include<iostream.h>
#include<conio.h>
struct date
{
int dd, mm, yy;
};
class student
{
private:
int rollNo;
char name[20];
date dob;
public:
void getData()
{
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
cout << "Enter date of birth in dd-mm-yy format: ";
cin >> dob.dd >> dob.mm >> dob.yy;
}
void putData()
{
cout << "Roll number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Date of birth: " << dob.dd << "-" << dob.mm << "-" << dob.yy << endl;
}
int getMonth()
{return dob.mm;}
};
struct node
{
student data;
node *next;
};
node *front = NULL;
node *rear = NULL;
node *createNode(student wat)
{
node *ptr = new node;
ptr -> data = wat;
ptr -> next = NULL;
return ptr;
}
void addNode(student wat)
{
node *freshNode = createNode(wat);
if(rear == NULL)
{
front = freshNode;
rear = freshNode;
}
else
{
rear -> next = freshNode;
freshNode -> next = NULL;
rear = freshNode;
}
}
void delNode()
{
if(front == NULL)
cout << "Underflow! " << endl;
else
{

node *temp = front;


front = temp -> next;
temp -> next = NULL;
delete temp;
}
}
void display()
{
if(front == NULL)
cout << "Nothing to see here, move on. " << endl;
else
{
cout << "Your queue is now: " << endl;
node *ptr = front;
while(ptr != NULL)
{
ptr -> data.putData();
cout << "-->" << endl;
ptr = ptr -> next;
}
cout << "Finito. " << endl;
}
}
void dispMonth(int n)
{
node *ptr = front;
int flag = 0;
cout << "The people born in month number " << n << " are: " << endl;
while(ptr != NULL)
{
if(ptr -> data.getMonth() == n)
{
ptr -> data.putData();
flag = 1;
}
ptr = ptr -> next;
}
if(flag == 0)
cout << "No person found with entered month number. " << endl;
}
void main()
{
clrscr();
char inpt = 'b';
while(inpt != 'x')
{
cout << "Enter: " << endl;
cout << "a to add" << endl;
cout << "d to delete" << endl;
cout << "c to check for month" << endl;
cout << "x to exit" << endl;
cout << "Enter your option: " << endl;
cin >> inpt;
if(inpt == 'a')
{
student stud;
stud.getData();
addNode(stud);
display();
}
else if(inpt == 'd')
{
delNode();
display();
}
else if(inpt == 'c')
{
int n;
cout << "Enter month number: " << endl;
cin >> n;
if(n > 12)
cout << "Month number out of range. " << endl;
else
dispMonth(n);
}
}
display();
cout << "kthnxbye" << endl;
getch();
}

D8. Implement a static Stack.

#include<iostream.h>
#include<conio.h>
int top = -1;
void push(int n, int arr[])
{
int num;
cout << "Enter number to be pushed: ";
cin >> num;
if(top < n)
{
arr[top+1] = num;
top++;
}
else
cout << "Overflow! " << endl;
}
void pop(int arr[])
{
if(top > -1)
{
arr[top] = '\0';
top--;
}
else
cout << "Underflow! " << endl;
}
void display(int arr[])
{
cout << "Elements in stack are: " << endl;
for(int i = 0; i <= top; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
}
void main()
{
int n;
int arr[100];
cout << "Enter number of elements in stack: ";
cin >> n;
char inpt = 'm';
while(inpt != 'x')
{
cout << "Enter: " << endl;
cout << "a to push" << endl;
cout << "b to pop" << endl;
cout << "x to exit" << endl;
cout << "Enter your option: " << endl;
cin >> inpt;
if(inpt == 'a')
{
push(n, arr);
}
else if(inpt = 'b')
pop(arr);
else
cout << "kthnxbye" << endl;
cout << "Your stack is now: " << endl;
display(arr);
}
getch();
}

D9. Implement a static Queue.

#include<iostream.h>
#include<conio.h>
int front = 0;
int back = 0;
void add(int n, int arr[])
{
int num;
cout << "Enter number to be added: ";
cin >> num;
if(back < n)
{
arr[back] = num;
back++;
}
else
cout << "Overflow! " << endl;
}
void del(int arr[])
{
if(back - front > 0)
{
arr[front] = '\0';
front++;
}
else
cout << "Underflow! " << endl;
}
void display(int arr[])
{
cout << "Elements in stack are: " << endl;
for(int i = front; i < back; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
}
void main()
{
int n;
int arr[100];
cout << "Enter number of elements in queue: ";
cin >> n;
char inpt = 'm';
while(inpt != 'x')
{
cout << "Enter: " << endl;
cout << "a to add" << endl;
cout << "d to delete" << endl;
cout << "x to exit" << endl;
cout << "Enter your option: " << endl;
cin >> inpt;
if(inpt == 'a')
{
add(n, arr);
}
else if(inpt == 'd')
del(arr);
else
cout << "kthnxbye" << endl;
cout << "Your queue is now: " << endl;
display(arr);
}
getch();
}

D10. Implement a static Circular Queue.

#include<iostream.h>
#include<conio.h>
int n;
int front = -1, rear = -1;
void add(int arr[])
{
int num;
cout << "Enter number to be added: ";
cin >> num;
if(front == (rear + 1)%n)
{
cout << "Overflow! ";
}
else
{
if(front == -1)
front = rear = 0;
else
rear = (rear + 1)%n;
arr[rear] = num;
}
}
void del(int arr[])
{
int a;
if(front == -1)
cout << "Underflow! " << endl;
else
{
a = arr[front];
if(front == rear)
front = rear = -1;
else
front = (front + 1)%n;
}
}
void display(int arr[])
{
int i;
if(front == -1)
{
cout << "Underflow! " << endl;
}
else
{
cout << endl;
cout << "Front --> " << front << endl;
cout << "Rear --> " << rear << endl;
cout << "The Circular Queue elements are: " << endl;
for(i = 0; i <= rear - front; i++)
cout << arr[front+i] << endl;
}
}
int main()
{
int arr[100];
cout << "Enter number of elements in circular queue: ";
cin >> n;
char inpt = 'm';
while(inpt != 'x')
{
cout << "Enter: " << endl;
cout << "a to add" << endl;
cout << "d to delete" << endl;
cout << "x to exit" << endl;
cout << "Enter your option: " << endl;
cin >> inpt;
if(inpt == 'a')
{
add(arr);
}
else if(inpt == 'd')
del(arr);
else
cout << "kthnxbye" << endl;
cout << "Your queue is now: " << endl;
display(arr);
}
getch();
}

Binary file handling:


B1. Define a class student with the following descriptions:
Private members
Data members
Data type
Roll number
integer
Name
string
mark
float[5]
avgmark
float
grade
char
CalAvg() : a function that accepts a float of 5 elements and return their average.
CalGrade(): a function that accepts a float and returns the corresponding grade. (>=75 A, >=50&<75 B
and <50 C)
Public member functions of class student;
getData() : accepts roll number, name and marks in 5 subjects. Invokes the functions calAvg() to store
average marks in avgmark and calGrade() to store grade.
putData() : to display roll number, name, average mark and grade.
Write the objects of the class into a binary file called student.dat
Display all the records stored in the file.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class student
{
private:
int rollNo;
char name[20];
float mark[5];
float avgmark;
char grade;
void calAvg()
{
float tot;
for(int i = 0; i < 5; i++)
{
tot += mark[i];
}
avgmark = tot/5;
}
char calGrade()
{
if(avgmark >= 75)
grade = 'A';
else if(avgmark >= 50 && avgmark < 75)
grade = 'B';
else
grade = 'C';
return grade;
}
public:
void getData()
{
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
cout << "Enter 5 marks: ";
for(int i = 0; i < 5; i++)
{
cin >> mark[i];
}
calAvg();
calGrade();
}
void putData()
{
cout << "Roll number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Average marks: " << avgmark << endl;
cout << "Grade: " << grade << endl;
}
};
void main()
{
student derps[100];
int n;
cout << "Enter number of students: ";
cin >> n;
fstream file;
file.open("student.dat", ios::in | ios::out);

cout << "Enter details for " << n << " students: " << endl;
for(int i = 0; i < n; i++)
{
derps[i].getData();
file.write((char *)&derps[i], sizeof(derps[i]));
}
file.seekg(0);
cout << "The records in student.dat are as follows: " << endl;
for(int j = 0; j < n; j++)
{
file.read((char *)&derps[j], sizeof(derps[j]));
derps[j].putData();
}
file.close();
getch();
}

B2. Define a class student with the following descriptions:


Private members
Data members
Data type
Roll number
integer
Name
string
avgmark
float
grade
char
CalGrade(): a function that accepts a float and returns the corresponding grade. (>=75 A, >=50&<75 B
and <50 C)
Public member functions of class student;
getData() : accepts roll number, name and average mark , invokes calGrade() to store grade.
putData() : to display roll number, name, average mark and grade.

Write the objects of the class into a binary file called Result.dat
Display number of students in each grade.
Display the data of the students securing highest and the lowest average marks.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class student
{
private:
int rollNo;
char name[20];
float avgmark;
char grade;
void calGrade()
{
if(avgmark >= 75)
grade = 'A';
else if(avgmark >= 50 && avgmark < 75)
grade = 'B';
else
grade = 'C';
}
public:
void getData()
{
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
cout << "Enter average marks: ";
cin >> avgmark;
calGrade();
}
void putData()
{
cout << "Roll number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Average marks: " << avgmark << endl;
cout << "Grade: " << grade << endl;
}
float getAvgmark()
{
return avgmark;
}
char getGrade()
{
return grade;

}
};
void main()
{
student derps[100];
int n;
cout << "Enter number of students: ";
cin >> n;
fstream file;
file.open("result.dat", ios::in | ios::out);
cout << "Enter details for " << n << " students: " << endl;
for(int i = 0; i < n; i++)
{
derps[i].getData();
file.write((char *)&derps[i], sizeof(derps[i]));
}
int
int
int
int
int

nofA = 0;
nofB = 0;
nofC = 0;
lowInd = 0;
highInd = 0;

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


{
if(derps[j].getGrade() == 'A') // Finding no. of students by grade
nofA++;
else if(derps[j].getGrade() == 'B')
nofB++;
else
nofC++;
if(derps[j].getAvgmark() < derps[lowInd].getAvgmark()) // Finding lowest and highest
marks
lowInd = j;
if(derps[j].getAvgmark() > derps[highInd].getAvgmark())
highInd = j;
}
cout
cout
cout
cout

<<
<<
<<
<<

"Number of students in each grade are: ";


"A: " << nofA << endl;
"B: " << nofB << endl;
"C: " << nofC << endl;

file.seekg(0);
cout << "The records for highest-scoring student is: " << endl;
file.read((char *)&derps[highInd], sizeof(derps[highInd]));
derps[highInd].putData();
file.seekg(0);
cout << "The records for lowest-scoring student is: " << endl;
file.read((char *)&derps[lowInd], sizeof(derps[lowInd]));
derps[lowInd].putData();
file.close();
getch();
}

B3. Define a class student with the following descriptions:


Private members
Data members
Data type
Roll number
integer
Name
string
Date of birth
A structure in the format dd,mm,yy
dd,mm & yy are integers.

o
o

Public member functions of class student;


Getdata() : to enter all data
Putdata() : to display all data
Write the objects of the class into a binary file called Information.dat
Display the contents of the file, date of birth in dd-character month-yy format.
Write a function to pass the month number and display the data of students born in that particular month.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
struct date
{
int dd, mm, yy;
};
class student
{
private:
int rollNo;
char name[20];
date dob;
public:
void monthName(int month)
{
switch (month)
{
case 1 : cout << "Jan";
case 2 : cout << "Feb";
case 3 : cout << "Mar";
case 4 : cout << "Apr";
case 5 : cout << "May";
case 6 : cout << "Jun";
case 7 : cout << "Jul";
case 8 : cout << "Aug";
case 9 : cout << "Sep";
case 10: cout << "Oct";
case 11: cout << "Nov";
case 12: cout << "Dec";
}
}

break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;
break;

void getData()
{
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter name: ";
cin >> name;
cout << "Enter date of birth in dd-mm-yy format: ";
cin >> dob.dd >> dob.mm >> dob.yy;
}
void putData()
{
cout << "Roll number: " << rollNo << endl;
cout << "Name: " << name << endl;
cout << "Date of birth: " << dob.dd << "-";
MonthName(dob.mm)
cout << "-" << dob.yy << endl;
}
int getMonth()
{
return dob.mm;
}
}s1;
fstream file;
ifstream fio;
void monthOut()
{
fio.open("information.dat", ios::in);
int monthNum;
cout << "Enter a month of birth: " << endl;
cin >> monthNum;

int found = 0;
while(!fio.eof())
{
fio.read((char *)&s1, sizeof(s1));
if(s1.getMonth() == monthNum)
{
s1.putData();
found = 1;
}
}
if(found == 0)
cout << "No records with entered month of birth! " << endl;
fio.close();
}
void main()
{
student derps[100];
int n;
cout << "Enter number of students: ";
cin >> n;
file.open("information.dat", ios::in | ios::out);
cout << "Enter details for " << n << " students: " << endl;
for(int i = 0; i < n; i++)
{
derps[i].getData();
file.write((char *)&derps[i], sizeof(derps[i]));
}
file.seekg(0);
cout << "The records in information.dat are as follows: " << endl;
for(int j = 0; j < n; j++)
{
file.read((char *)&derps[j], sizeof(derps[j]));
derps[j].putData();
}
file.close();
monthOut();
getch();
}

B4. Define a class emp with the following descriptions:


Private members
Data members
Data type
Emp Number
integer
Emp Name
char[20]
Department
char[10]
Salary
integer

o
o
o

Public member functions of class student;


Getdata() : to enter all data,
Putdata() : to display all data
Write the objects of the class into a binary file called employee.dat
Display the contents of the file.
Display the data of a given department. Also display the total salary and average salary paid to the employees of this
department.
Display the data of n-th record.

#include<fstream.h>
#include<string.h>
#include<conio.h>
fstream file;
ifstream fio;
char globDept[10];
class emp
{
private:
int empNo;
char empName[20];
char dept[10];
int salary;
public:
void getData()
{
cout << "Enter employee number: ";
cin >> empNo;
cout << "Enter employee name: ";
cin >> empName;
cout << "Enter department: ";
cin >> dept;
cout << "Enter salary: ";
cin >> salary;
}
void putData()
{
cout << "Employee number: " << empNo << endl;
cout << "Name: " << empName << endl;
cout << "Department: " << dept << endl;
cout << "Salary: " << salary << endl;
}
void getDept()
{
strcpy(dept, globDept);
}
}e1;
void descrOut()
{
fio.open("information.dat", ios::in);
char dept[10];
cout << "Enter a department for more info: " << endl;
cin >> dept;
int found = 0;
while(!fio.eof())
{
fio.read((char *)&e1, sizeof(e1));
e1.getDept();
if(strcmp(globDept, dept) == 0)
{
e1.putData();
found = 1;
}
}
if(found == 0)
cout << "No records with entered department! " << endl;
fio.close();
}
void main()
{

emp derps[100];
int n;
cout << "Enter number of employees: ";
cin >> n;
file.open("employee.dat", ios::in | ios::out);
cout << "Enter details for " << n << " employees: " << endl;
for(int i = 0; i < n; i++)
{
derps[i].getData();
file.write((char *)&derps[i], sizeof(derps[i]));
}
file.seekg(0);
cout << "The records in employee.dat are as follows: " << endl;
for(int j = 0; j < n; j++)
{
file.read((char *)&derps[j], sizeof(derps[j]));
derps[j].putData();
}
int num;
cout << "Enter n for n-th record: " << endl;
cin >> num;
if(num < n)
{
file.seekg(0);
for(int k = 0; k < n; k++)
{
if(k == num-1)
{
file.read((char *)&derps[k], sizeof(derps[k]));
derps[k].putData();
}
}
}
else
cout << "n out of range";
file.close();
getch();
}

B5. Define a class emp with the following descriptions:


Private members
Data members
Data type
Emp number
integer
Emp Name
char[20]
salary
integer

o
o

Public member functions of class student;


Getdata() : to enter all data
Putdata() : to display all data
Write the objects of the class into a binary file called employee.dat
Display the contents of the file.
Delete the record of the employee getting least salary.

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
class emp
{
private:
int empNo;
char empName[20];
int salary;
public:
void getData()
{
cout << "Enter employee number: ";
cin >> empNo;
cout << "Enter employee name: ";
cin >> empName;
cout << "Enter salary: ";
cin >> salary;
}
void putData()
{
cout << "Employee number: " << empNo << endl;
cout << "Name: " << empName << endl;
cout << "Salary: " << salary << endl;
}
int getSalary()
{
return salary;
}
};
void main()
{
fstream file("employee.dat", ios::in | ios::out);
ofstream fio("temp.dat", ios::out);
emp derps[100];
int n;
cout << "Enter number of employees: ";
cin >> n;
cout << "Enter details for " << n << " employees: " << endl;
for(int i = 0; i < n; i++)
{
derps[i].getData();
file.write((char *)&derps[i], sizeof(derps[i]));
}
file.seekg(0);
cout << "The records in employee.dat are as follows: " << endl;
for(int j = 0; j < n; j++)
{
file.read((char *)&derps[j], sizeof(derps[j]));
derps[j].putData();
}
int lowInd = 0;
for(int k = 0; k < n; k++)
{
if(derps[k].getSalary() < derps[lowInd].getSalary())
lowInd = k;
}
cout << "Now deleting record of employee getting least salary... ";
for(int l = 0; l < n; l++)
{
if(l != lowInd)

fio.write((char *)&derps[l], sizeof(derps[l]));


}
cout << "Done. " << endl;
fio.close();
file.close();
remove("employee.dat");
rename("temp.dat", "employee.dat");
getch();
}

B6. Sort the file employee.dat in ascending order of salary. Display the original file data and sorted file data.

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
class emp
{
private:
int empNo;
char empName[20];
char dept[10];
int salary;
public:
void getData()
{
cout << "Enter employee number: ";
cin >> empNo;
cout << "Enter employee name: ";
cin >> empName;
cout << "Enter department: ";
cin >> dept;
cout << "Enter salary: ";
cin >> salary;
}
void putData()
{
cout << "Employee number: " << empNo << endl;
cout << "Name: " << empName << endl;
cout << "Department: " << dept << endl;
cout << "Salary: " << salary << endl;
}
int getSalary()
{
return salary;
}
}e1;
void sort(int n, int list[])
{
int temp;
for(int i = 0; i < n; i++)
{
for (int j = 0; j < n-1; j++)
{
if (list[j] > list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
}
void main()
{
clrscr();
ifstream fio;
ofstream file;
emp derps[100];
fio.open("employee.dat", ios::in);
file.open("empSorted.dat", ios::out);
fio.seekg(0);
int nofRec = 0;
int ascList[100];

while(!fio.eof())
{
fio.read((char *)&e1, sizeof(e1));
derps[nofRec] = e1;
ascList[nofRec] = e1.getSalary();
nofRec++;
}
sort(nofRec, ascList);
for(int i = 0; i < nofRec; i++)
{
for(int j = 0; j < nofRec; j++)
{
if(derps[j].getSalary() == ascList[i])
{
file.write((char *)&derps[j], sizeof(derps[j]));
break;
}
}
}
for(int k = 0; k < nofRec; k++) // Displays employee.dat
{
fio.read((char *)&e1, sizeof(e1));
e1.putData();
}
for(int l = 0; l < nofRec; l++) // Display empSorted.dat
{
fio.read((char *)&e1, sizeof(e1));
e1.putData();
}
fio.close();
file.close();
getch();
}

Text file handling


T7. Create a Text File Create a Text File story.dat and write the functions for the following questions
a. Display the contents of the file
b. Display each line in reverse order
c. Display the frequency table of all the alphabets present in the text file
d. Display number of upper case alphabets and number of lowercase alphabets present in the text file
e. Display number of vowels, number of consonants, number of digits and number of words present in the text file.
f. Display all words and frequency of a particular word.

#include<fstream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
void create()
{
char line[80];
ofstream f1("story.txt");
cout << "Enter the text : " << endl << endl;
for(int i = 0; i < 3; i++)
{
gets(line);
f1 << line << endl;
}
f1.close();
}
void display()
{
char line[80];
ifstream f1("story.txt");
cout << endl << endl;
while(f1.getline(line,80))
puts(line);
f1.close();
}
void reverse()
{
char line[80];
ifstream f1("story.txt");
while(f1.getline(line, 80, '\n'))
{
int n = strlen(line);
for(int i = n; i >= 0; i--)
cout << line[i];
cout << endl;
}
f1.close();
}
void frequency()
{
ifstream f1("story.txt");
int freq[26];
for(int i = 0; i < 26; i++)
freq[i] = 0;
int countVow = 0, countCons = 0, countDig = 0;
int let_upper = 0, let_lower = 0;
while(1)
{
char letter = f1.get();
if (isdigit(letter))
countDig++;
if ( letter == 'a' ||
letter == 'e'
letter == 'i'
letter == 'o'
letter == 'u'
countVow++;
else
countCons++;

letter ==
|| letter
|| letter
|| letter
|| letter

'A' ||
== 'E'
== 'I'
== 'O'
== 'U'

||
||
||
)

if (isupper(letter))
let_upper++;
if (islower(letter))
let_lower++;
if isupper(letter)
letter = tolower(letter);
int alphabet_no = int(letter)-97;
freq[alphabet_no]++;
if (f1.eof())
break;
}
f1.close();
cout
cout
cout
cout
cout

<<
<<
<<
<<
<<

"No.
"No.
"No.
"No.
"No.

of
of
of
of
of

uppercase letters = " << let_upper << endl;


lowercase letters = " << let_lower <<endl;
consonants = " << countCons << endl;
vowels = " << countVow << endl;
digits = " << countDig << endl;

getch();
clrscr();
for(i = 0; i < 26; i++)
{
cout << "Frequency of " << char(i + 97) << " is : ";
cout << freq[i] << endl;
}
}
void CountWord(char word[])
{
int count = 0;
char c[80];
ifstream f1("story.txt");
while (f1 >> c)
{
if(strcmp(c, word) == 0)
count++;
}
cout << "Word { " << word << " } appears " << count << " times" << endl;
f1.close();
}
void main()
{
clrscr();
create();
getch();
clrscr();
display();
getch();
clrscr();
reverse();
getch();
clrscr();
frequency();
getch();
clrscr();
cout<<"Enter word : ";
char word[40];
gets(word);
CountWord(word);
getch();
}

Potrebbero piacerti anche