Sunteți pe pagina 1din 56

LAB MANUAL

Subject Code : CA331

Subject Name : Programming in C++

List of Experiments:

1. Personal Details using Classes & Objects


2. Academic Details using Single Inheritance
3. Complex Number Manipulation using Operator Overloading
4. Mark Sheet Generation using Multiple Inheritance and Array of Objects
5. Sorting and Searching of Student Details using Array of Objects
6. Rank List with Exception Handling
7. File Operations
8. Checking a Palindrome using a Stack
9. Matrix Multiplication using Pointers
10. Queue Implementation using Templates
EX.NO:1 PERSONAL DETAILS USING CLASSES & OBJECTS

Aim:

To write a C++ program using classes and objects to read the Personal Details of a student and
displaying it.

Algorithm:

Step 1: Start

Step 2: Create a class called person.

Step 3: Declare the necessary variables inside the class person.

Step 4: Define a constructor to initialize the class members.

Step 5: Define the member functions such as getData( ) and putdata( ) as public inside the class.

Step 6: getData( ) is used to get the personal details of the student from the user.

Step 7: putdata( ) is used to display the personal details of the student.

Step 8: In the main( ), object of the class person is declared.

Step 9: Access the member function getData( ) using the object of the class to get the personal
details of the student.

Step 10: Access the member function putdata( ) using the object of the class to display the
personal details of the student.

Step 11: Stop.

Source code:

#include<iostream.h>

#include<conio.h>

class person

private:

char n[25],fn[25],sex[6];

char eq[20],ad[50],dob[15];
char ph[10];

double ai;

public:

person()

ai=0;

void getData()

cout<<"Enter the name:";

cin>>n;

cout<<"enter Fathers Name:";

cin>>fn;

cout<<"Enter sex:";

cin>>sex;

cout<<"enter educational qualification:";

cin>>eq;

cout<<"Enter your address:";

cin.ignore();

cin.getline(ad,50);

cout<<"Enter phone number:";

cin.ignore();

cin.getline(ph,12);

cout<<"enter your Date of birth:";

cin>>dob;
cout<<" Enter annual Income:";

cin>>ai;

void putdata()

cout<<"Name:"<<n<<endl;

cout<<"Fathers Name:"<<fn<<endl;

cout<<"Sex:"<<sex<<endl;

cout<<"Date of Birth:"<<dob<<endl;

cout<<"Educational qualification:"<<eq<<endl;

cout<<"Annual Income:"<<ai<<endl;

cout<<"Address:"<< ad<<endl;

cout<<"Phone number:"<<ph;

};

int main()

person p;

clrscr();

p.getData();

p.putdata();

getch();

return(0);

}
Output:

Enter the name:Jaiwin

enter Fathers Name:Babu

Enter sex:Female

enter educational qualification:B.Sc

Enter your address:Tuticorin

Enter phone number:2364234

enter your Date of birth:08/02/1987

Enter annual Income:190000

Name:Jaiwin

Fathers Name:Babu

Sex:FemaleB.Sc

Date of Birth:08/02/1987

Educational qualification:B.Sc

Annual Income:190000

Address:Tuticorin

Phone number:364234
ExNo:2 ACADEMIC DETAILS USING SINGLE INHERITANCE

Aim:
To write a c++ program to get and display the academic details of the student using single
inheritance.
Algorithm:
Step 1: Start the program.
Step 2: Create a class named student and declare the required variables as private and member
methods as protected.
Step 3: Define a function called get() to get the personal details of the student as input from the
user.
Step 4: Define a function called put() to display all the details.
Step 5: Create a class named academic as a derived class from the base class student.
Step 6: Declare the required variables as private.
Step 7: Define a function called getdata() to get the academic details of the students as input
from the user. Call the function of the parent class to get the personal details.
Step 8: Define a function called putdata() to display the details. Call the function of the parent
class to display the details.
Step 9: Call the two functions getdata() and putdata() of the derived class using an object of the
derived class.
Step10: Save and execute the program.
Step11: Stop the process.

Source Code:

#include<iostream>
using namespace std;
class student
{
private:
char name[15];
char add[20];
int age;
char dob[12];
char gen[8];
char quali[15];
int ai;
protected:
student()
{
int age=0;
int ai=0;
}
void get()
{
cout<<"\n*****************Personal Details*****************\n";
cout<<"\nEnter the student name:\n";
cin>>name;
cout<<"\nEnter the student address:\n";
cin>>add;
cout<<"\nEnter the age of the student:\n";
cin>>age;
cout<<"\nEnter th date of birth of the student:\n";
cin>>dob;
cout<<"\nEnter the gender of the student:\n";
cin>>gen;
cout<<"\nEnter the annual income of the parent:\n";
cin>>ai;
cout<<"\n";
}
void put()
{
cout<<"\n******************Personal Details******************\n";
cout<<"\nStudent name:"<<name;
cout<<"\nStudent address:"<<add;
cout<<"\nStudent age:"<<age;
cout<<"\nStudent date of birth:"<<dob;
cout<<"\nGender:"<<gen;
cout<<"\nAnnual Income:"<<ai;
cout<<"\n";
}
};
class academic:public student
{
private:
char regno[8];
int ugm;
int pgm;
char ugg[5];
char pgg[5];
public:
academic()
{
int ugm=0;
int pgm=0;
}
void getdata()
{
student::get();
cout<<"\n****************Academic details******************\n";
cout<<"\nEnter the register number:\n";
cin>>regno;
cout<<"\nEnter the UG percentage:\n";
cin>>ugm;
cout<<"\nEnter the PG percentage:\n";
cin>>pgm;
cout<<"\nEnter the Ug grade:\n";
cin>>ugg;
cout<<"\nEnter the PG grade:\n";
cin>>pgg;
cout<<"\n";
}
void putdata()
{
student::put();
cout<<"\n****************Academic Details*********************\n";
cout<<"\nRegister Number:"<<regno;
cout<<"\nUG Percentage:"<<ugm;
cout<<"\nPg Percentage:"<<pgm;
cout<<"\nUG grade:"<<ugg;
cout<<"\nPG grade:"<<pgg;
cout<<"\n";
}
};
int main()
{
academic a;
a.getdata();
a.putdata();
return(0);
}

OUTPUT:

*****************Personal Details*****************
Enter the student name:
Vimala
Enter the student address:
Neyveli
Enter the age of the student:
21
Enter the date of birth of the student:
12.10.1988
Enter the gender of the student:
Female
Enter the annual income of the parent:
15000

****************Academic details******************
Enter the register number:
09CA002
Enter the UG percentage:
72
Enter the PG percentage:
83
Enter the Ug grade:
A
Enter the PG grade:
S

******************Personal Details******************
Student name:Vimala
Student address:Neyveli
Student age:21
Student date of birth:12.10.1988
Gender:Female
Annual Income:15000

****************Academic Details*********************
Register Number:09CA002
UG Percentage:72
Pg Percentage:83
UG grade:A
PG grade:S
ExNo:3 COMPLEX NUMBER MANIPILATION USING OPERATOR
OVERLOADING
Aim:

To write a program to arithmetically manipulate complex numbers using operator overloading.


Algorithm:
Step 1:Start the program.
Step 2:Define a class named complex and declare the required variables as private.
Step 3:Define the required functions as public.
Step 4:Get the real part and the imaginary part as input from the user.
Step 5:Define the functions operator +(), operator -() and operator /() to perform the necessary
operations.
Step 6:Call all the functions in the main function and display the values.
Step 7:Save and execute the program.
Step 8:Stop the program.

Source Code:

//To perform operations on complex number using operator

#include<iostream>
#include<math.h>
using namespace std;
//int exit(0);
class comp
{
private:
float real,image;
public:
comp operator +(comp a);
comp operator -(comp a);
comp operator *(comp a);
comp operator /(comp a);
void getdata();
void show();
};
void comp :: getdata()
{
cout<<"Enter real part=";
cin>>real;
cout<<"Enter imaginary part=";
cin>>image;
}
void comp :: show()
{
cout.precision(2);
if(image<0)
cout<<real<<image<<"i";
else
cout<<real<<"+"<<image<<"i";
}
comp comp :: operator +(comp a)
{
comp temp;
temp.real=real+a.real;
temp.image=image+a.image;
return temp;
}
comp comp :: operator -(comp a)
{
comp temp;
temp.real=real-a.real;
temp.image=a.image-image;
return temp;
}
comp comp :: operator *(comp a)
{
comp temp;
temp.real=(a.real*real)-(a.image*image);
temp.image=(a.real*image)+(real*a.image);
return temp;
}
comp comp :: operator /(comp a)
{
comp temp;
temp.real=((real*a.real)+(a.image*image))/((real*a.real)+(image*a.image));
temp.image=((real*a.image)-(image*a.image))/((real*a.real)+(image*a.image));
return temp;
}

int main()
{
comp d,e,f;
intch;
char ans;
do
{

cout<<"********** Menu **********\n";


cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiplication\n";
cout<<"4.Division\n";
cout<<"5.Exit\n";
d.getdata();
e.getdata();
cout<<"first no:\n";
d.show();
cout<<"\n";
cout<<"second no:\n";
e.show();
cout<<"\n";
cout<<"enter the choice:\n";
cin>>ch;
cout<<"\n";
switch(ch)
{
case 1:
f=d+e;
cout<<"ADDITION OF COMPLEX NUMBERS\n";
cout<<"-------------------------------------------------------\n";
cout<<"Addition of two no:";
f.show();
cout<<"\n";
break;
case 2:
f=d-e;
cout<<"SUBTRACTION OF COMPLEX NUMBERS\n";
cout<<"-------------------------------------------------------\n";
cout<<"Subtraction of two no:";
f.show();
cout<<"\n";
break;
case 3:
f=d*e;
cout<<"MULTIPLICATION OF COMPLEX NUMBERS\n";
cout<<"--------------------------------------------------------\n";
cout<<"Multiplication of two no:";
f.show();
cout<<"\n";
break;
case 4:
f=d/e;
cout<<"DIVISION OF COMPLEX NUMBERS\n";
cout<<"-------------------------------------------------------\n";
cout<<"Division of two no:";
f.show();
cout<<"\n";
break;
//case 5:
//exit(0);

}
cout<<"\n";
cout<<"-------------------------------------------------------\n";
cout<<"do you want to continue(y/n)?=";
cin>>ans;
}
while(ans=='y'||ans=='Y');
return 0;
//getch();
}

OUTPUT:

********** Menu **********


1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter real part=2
Enter imaginary part=2
Enter real part=4
Enter imaginary part=4
first no:
2+2i
second no:
4+4i
enter the choice:
1

ADDITION OF COMPLEX NUMBERS


-------------------------------------------------------
Addition of two no:6+6i
-------------------------------------------------------
do you want to continue(y/n)?=y
********** Menu **********
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter real part=2
Enter imaginary part=4
Enter real part=1
Enter imaginary part=1
first no:
2+4i
second no:
1+1i
enter the choice:
2

SUBTRACTION OF COMPLEX NUMBERS


-------------------------------------------------------
Subtraction of two no:1-3i

-------------------------------------------------------
do you want to continue(y/n)?=y
********** Menu **********
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter real part=1
Enter imaginary part=1
Enter real part=2
Enter imaginary part=2
first no:
1+1i
second no:
2+2i
enter the choice:
3

MULTIPLICATION OF COMPLEX NUMBERS


--------------------------------------------------------
Multiplication of two no:0+4i
-------------------------------------------------------
do you want to continue(y/n)?=y
********** Menu **********
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter real part=4
Enter imaginary part=2
Enter real part=2
Enter imaginary part=1
first no:
4+2i
second no:
2+1i
enter the choice:
4

DIVISION OF COMPLEX NUMBERS


-------------------------------------------------------
Division of two no:1+0.2i

-------------------------------------------------------
do you want to continue(y/n)?=n
Ex. No. 4 MARKSHEET GENERATION USING MULTIPLE INHERITANCE

AND ARRAY OF OBJECTS

Aim:

To print a mark sheet using multiple inheritance and array of objects.

Algorithm:

1. Start.
2. Define a class subject with the variables for student name, code and credit.
3. Define the methods readSub() to read the details of the subject, displaySub() to display
the details of the subject.
4. Derive a class internalExam from mark with data member subMark.
5. Define the methods readData() to read the internal marks for the subject and
displayData() to display the mark.
6. Define a method subMark() to return the marks for the subject.
7. Derive a class internalExam from subject with data member subMark.
8. Define the methods readData() to read the internal marks for the subject and
displayData() to display the mark.
9. Define a method subMark() to return the marks for the subject.
10. Derive a class externalExam from subject with data member subMark.
11. Define the methods readData() to read the external marks for the subject and
displayData() to display the mark.
12. Define a method subMark() to return the external marks for the subject.
13. Derive a class result from internalExam and externalExam.
14. Define a data member total and a method totalMarks() to return the sum of internal and
external marks for a subject.
15. Create an array of objects of class result.
16. Read the details of the student.
17. Process the details and print the result.

Source Code:

#include <iostream>

using namespace std;

class subject
{
protected:
char sName[30];
char sCode[4];
int credit;
public:
void readSub()
{
cout<<"\nEnter the subject code :";
cin>>sCode;
cout<<"\nEnter the subject name :";
cin>>sName;
cout<<”\nEnter the credit :”;
cin>>credit;
}

void displaySub()
{
cout<<sCode<<"\t\t";
cout<<sName<<"\t\t";
cout<<credit<<"\t\t";
}
};

class internalExam:virtual public subject


{
protected:
int subMark;
public:
void readData()
{

cin>>subMark;
if (subMark<0 || subMark>50)
{
cout<<"Invalid marks\n";
readData();
}
}

void displayData()
{
cout<<internalMark()<<"\t\t";
}

int internalMark()
{
return subMark;
}
};

class exxternalExam:virtual public subject


{
protected:
int subMark;
public:
void readData()
{

cin>>subMark;
if (subMark<0 || subMark>50)
{
cout<<"Invalid marks\n";
readData();
}
}

void displayData()
{
cout<<externalMark()<<"\t\t";
}

int externalMark()
{
return subMark;
}
};

class result:public internalExam, public externalExam


{
private:
int total;
public:
int totalMarks()
{
return internalMark()+externalMark();
}
};

int main()
{
int i,j,no,n,tot=0;
char name[30],regno[20];
result res[5];
cout<<"\nEnter the no of students :");
cin>>no;
for j=0;j<no;++j)
{
cout<<"\nEnter your name :";
cin>>name;
cout<<"\nEnter regno :";
cin>>regno;
cout<<"\nEnter no of subjects :";
cin>>n;
for (i=0;i<n;++i)
{
cout<<"\nEnter details for subject "<<i+1<<"\n";
res[i].readSub ();
cout<<"\nEnter internal marks :";
res[i].internalMark::readData();
cout<<"\nEnter external marks :";
res[i].externalMark::readData();
}

cout<<"\n*********************************************************************
*****************\n";
cout<<"
KARUNYA UNIVERSITY";
cout<<"\n MARK
SHEET";

cout<<"\n*********************************************************************
*****************\n";
cout<<"\n Name :"<<name<<"\t Regno :
"<<regno;

cout<<"\n*********************************************************************
******************";
cout<<"\n Sub Code Subject Name Credit
Internal External”;
cout<<"\n*********************************************************************
******************";

for (i=0;i<n;++i)
{
res[i].displayData ();
res[i].internalMark::displayData();
res[i].externalMark::displayData();
}
cout<<"\n*********************************************************************
******************";
for(i=0;i<n;++i)
{
tot+=res[i].totalMarks();
}
cout<<"\n";
cout<<"\n Total : "<<tot<<"\n";
cout<<"\n*********************************************************************
******************";

}
}

Output:

Enter the no of students : 1

Enter your name : Tina

Enter regno : 09CA023

Enter no of subjects : 3

Enter details for subject 1 :

Enter the subject code : CA301

Enter the subject name : Introduction to Computers

Enter the credit : 4

Enter internal marks : 45

Enter external marks : 43

Enter details for subject 2 :

Enter the subject code : CA302

Enter the subject name : Computer Organization and Architecture

Enter the credit : 4

Enter internal marks : 40

Enter external marks : 42


Enter details for subject 3 :

Enter the subject code : CA303

Enter the subject name : Programming in C++

Enter the credit : 4

Enter internal marks : 49

Enter external marks : 48

*****************************************************************************

KARUNYA UNIVERSITY

MARK SHEET

****************************************************************************

Name : Tina Regno : 09CA023

****************************************************************************

Sub Code Subject Name Credit Internal External

****************************************************************************

CA301 Introduction to Computers 4 45 43

CA302 Computer Organization and Architecture 4 40 42

CA303 Programming in C++ 4 49 48

****************************************************************************

Total : 267

****************************************************************************
EX.NO:5 SORTING AND SEARCHING OF STUDENTS DETAILS USING ARRAY OF
OBJECTS

Aim:
To write a C++ program to sortand search the student details using sorting and searching
concepts.

Algorithm:
Step1: Start the program.
Step2: Create a class called student and declare the required variables as private.
Step3: Define a default constructor.
Step4: Get the necessary data using get() and display using display().
Step5: Sort the student details using the function swap(), sortbyfname(), sortbyregno(),
sortbydept()
Step6: Search the details using the functions searchbyfname() and searchbyregno().
Step7: Call all the functions in the main functions.
Step8: Save and execute the program.
Step9: Stop the process.

Source Code:

#include<string.h>
#include<iostream>
using namespace std;
class student
{
charregno[10];
char fname[20],lname[20],dept[20],grade[3];
public:
void get()
{
cout<<"\nEnter the register number :";
cin>>regno;
cout<<"Enter the first name :";
cin>>fname;
cout<<"Enter the last name :";
cin>>lname;
cout<<"Enter the department name :";
cin>>dept;
cout<<"Enter the grade :";
cin>>grade;
}
void display()
{
cout<<"\nRegister number :"<<regno;
cout<<"\nFirst name :"<<fname;
cout<<"\nLast name :"<<lname;
cout<<"\nDepartment name :"<<dept;
cout<<"\nGrade :\n"<<grade;
}
void swap(student *a,student *b)
{
student temp;
temp=*a;
*a=*b;
*b=temp;
}
voidsortbyfname(student std[50],intno_of_students)
{
inti,j;
for(i=0;i<no_of_students;i++)
{
for(j=i+1;j<no_of_students;j++)
{
if(strcmp(std[i].fname,std[j].fname)>0)
{
swap(&std[i],&std[j]);
}
}
}
cout<<"\n ID FIRST NAME LAST NAME DEPARTMENT GRADE:\n";
for(i=0;i<no_of_students;i++)
{
cout<<"\t"<<std[i].regno<<”\t”<<std[i].fname<<"\t"<<std[i].lname<<"\t\t"<<std[i].dept<<"\t"<<
std[i].grade;
cout<<"\n";
}
}
voidsortbyregno(student std[50],intno_of_students)
{
inti,j;
for(i=0;i<no_of_students;i++)
{
for(j=i+1;j<no_of_students;j++)
{
if(strcmp(std[i].regno,std[j].regno)>0)
{
swap(&std[i],&std[j]);
}
}
}
cout<<"\nID FIRST NAME LAST NAME DEPARTMENT GRADE:\n";
for(i=0;i<no_of_students;i++)
{
cout<<"\t"<<std[i].regno<<"\t"<<std[i].fname<<"\t\t"<<std[i].lname<<"\t"<<std[i].dept<<"\t"<<
std[i].grade;
cout<<"\n";
}
}

voidsortbydept(student std[50],intno_of_students)
{
inti,j;
for(i=0;i<no_of_students;i++)
{
for(j=i+1;j<no_of_students;j++)
{
if(strcmp(std[i].dept,std[j].dept)>0)
{
swap(&std[i],&std[j]);
}
}
}
cout<<"\nID FIRST NAME LAST NAME DEPARTMENT GRADE:\n";
for(i=0;i<no_of_students;i++)
{
cout<<"\t"<<std[i].regno<<"\t"<<std[i].fname<<"\t\t"<<std[i].lname<<"\t"<<std[i].dept<<"\t"<<
std[i].grade;
cout<<"\n";
}
}

voidsearchbyfname(student std[50],intno_of_students,char *fname)


{
inti,flag;
for(i=0;i<no_of_students;++i)
{
if(strcmp(fname,std[i].fname)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==1)
cout<<"Student:"<<fname<<"Exists in the records .\n";
else
cout<<"Record not found .";
}
voidsearchbyregno(student std[50],intno_of_students,char *regno)
{
inti,flag;
for(i=0;i<no_of_students;++i)
{
if(strcmp(regno,std[i].regno)==0)
{
flag=1;
break;
}
else
{
flag=0;
}
}
if(flag==1)
cout<<"Student :"<<regno<<"Exists in the records .\n";
else
cout<<"Record not found .";
}
};

int main()
{
student stud[50];
intnos_std;
intn,count=0,k;
cout<<"Enter number of students:\n";
cin>>nos_std;
charfname[20];
charregno[10];
cout<<"\n STUDENT DETAILS";
cout<<"\n******************";
cout<<"\n 1.Get data";
cout<<"\n 2.Put data";
cout<<"\n 3.Sort by first name";
cout<<"\n 4.Sort by Student id";
cout<<"\n 5.Sort by department";
cout<<"\n 6.Search by first name";
cout<<"\n 7.Search by Student id";
cout<<"\n 8.Exit";
do
{
cout<<"\n Enter your choice :";
cin>>n;
switch(n)
{
case 1:
cout<<"\n GET DATA\n";
cout<<"Enter details of the students one by one:\n";
for(k=0;k<nos_std;++k)
stud[k].get();
break;
case 2:
cout<<"\n DISPLAY DATA:\n";
for(k=0;k<nos_std;++k)
stud[k].display();
break;
case 3:
cout<<"Sort by First name :";
stud[0].sortbyfname(stud,nos_std);
break;
case 4:
cout<<"Sort by student id :";
stud[0].sortbyregno(stud,nos_std);
break;
case 5:
cout<<"Sort by department :";
stud[0].sortbydept(stud,nos_std);
break;
case 6:
cout<<"Search by First name :\n";
cout<<"Enter name to be searched :\n ";
cin>>fname;
stud[0].searchbyfname(stud,nos_std,fname);
break;
case 7:
cout<<"Search by Student id :\n";
cout<<"Enter id to be searched:\n";
cin>>regno;
stud[0].searchbyregno(stud,nos_std,regno);
break;
default:
break;
}
}while(n<8);
return(0);
}

Output:

Enter number of students:


4

STUDENT DETAILS
******************
1.Get data
2.Put data
3.Sort by first name
4.Sort by Student id
5.Sort by department
6.Search by first name
7.Search by Student id
8.Exit
Enter your choice :1

GET DATA
Enter details of the students one by one:

Enter the register number :002


Enter the first name :Vimala
Enter the last name :Benjamin
Enter the department name :MCA
Enter the grade :S

Enter the register number :001


Enter the first name :Lincy
Enter the last name :Abraham
Enter the department name :MBA
Enter the grade :A

Enter the register number :003


Enter the first name :Hedy
Enter the last name :Benjamin
Enter the department name :MBA
Enter the grade :S

Enter the register number :004


Enter the first name :Jeny
Enter the last name :Thomas
Enter the department name :EEE
Enter the grade :O

Enter your choice :2

DISPLAY DATA:

Register number :002


First name :Vimala
Last name :Benjamin
Department name :MCA
Grade : S

Register number :001


First name :Lincy
Last name :Abraham
Department name :MBA
Grade : A

Register number :003


First name :Hedy
Last name :Benjamin
Department name :MBA
Grade : S

Register number :004


First name :Jeny
Last name :Thomas
Department name :EEE
Grade : O

Enter your choice :3


Sort by First name :

ID FIRST NAME LAST NAME DEPARTMENT GRADE:


003 Hedy Benjamin MBA S
004 Jeny Thomas EEE O
001 Lincy Abraham MBA A
002 Vimala Benjamin MCA S

Enter your choice :4


Sort by student id :

ID FIRST NAME LAST NAME DEPARTMENT GRADE:


001 Lincy Abraham MBA A
002 Vimala Benjamin MCA S
003 Hedy Benjamin MBA S
004 Jeny Thomas EEE O

Enter your choice :5


Sort by department :

ID FIRST NAME LAST NAME DEPARTMENT GRADE:


001 Lincy Abraham MBA A
003 Hedy Benjamin MBA S
002 Vimala Benjamin MCA S
004 Jeny Thomas EEE O

Enter your choice :6


Search by First name :
Enter name to be searched :
Vimala
Student:Vimala

Exists in the records .

Enter your choice :6


Search by First name :
Enter name to be searched :
Divya

Record not found .

Enter your choice :7


Search by Student id :
Enter id to be searched:
009

Enter your choice :7


Search by Student id :
Enter id to be searched:
001

Student:001

Exists in the records.

Record not found.

Enter your choice :8


ExNo:6 RANK LIST WITH EXCEPTION HANDLING

AIM:

To prepare the rank list of students with exception handling.

ALGORITHM:

Step 1: Start the program.


Step 2: Define a class called student and declare the required variables.
Step 3: Create two exception handling classes maximum and minimum.
Step 4: Define a function read_details() to get the student details as input from the user.
Step 5: Define the functions readinternal() and readexternal() to get the internal and the
external marks as input from the user.
Step 6: Throw the two exceptions in the functions minimum() and maximum() when the
user enters a mark outside the range.
Step 7: Define the functions calculatecgpa() and calculaterank() to calculate cgpa and
rank of the student.
Step 8: Call the functions in the main function.
Step 9: Get the number of students as input from the user in the main function.
Step10: Catch the two exceptions in the main function.
Step11: Display the student details.
Step12: Save and execute the program.

Source Code:

#include<iostream>
#include<string.h>
#define MAXLIMIT 10
#define LEN 30
using namespace std;
class Student
{
private:
char sub[MAXLIMIT][LEN],name[LEN];
int internal[MAXLIMIT],external[MAXLIMIT];
int total,cgpa,rank,regno;
public:
//Exception Handling Classes
class MINIMUM{};
class MAXIMUM{};

//Constructor
Student()
{
strcpy(sub[0],"CPP");
strcpy(sub[1],"Operating Systems");
strcpy(sub[2],"SSC");
strcpy(sub[3],"OOAD");
strcpy(sub[4],"DS");
}

void readDetail()
{
cout<<"\n";
cout<<"Enter the Student Name:";
cin>>name;
cout<<"Enter the Reg.No:";
cin>>regno;
}//end readDetails

void readInternal()throw(MINIMUM,MAXIMUM)
{
cout<<"Internal Marks"<<endl;
cout<<"----------------------------------"<<endl;
for(int i=0;i<5;i++)
{
cout<<"Enter "<<sub[i]<<" Marks:";
cin>>internal[i];
if(internal[i]<0)
throw MINIMUM();
if(internal[i]>40)
throw MAXIMUM();
}//end for
cout<<"----------------------------------"<<endl;
}//end readInternal()

void readExternal()throw(MINIMUM,MAXIMUM)
{
cout<<"External Marks"<<endl;
cout<<"----------------------------------"<<endl;
for(int i=0;i<5;i++)
{
cout<<"Enter "<<sub[i]<<" Marks:";
cin>>external[i];
if(external[i]<0)
throw MINIMUM();
if(external[i]>60)
throw MAXIMUM();
}//end for
cout<<"External Marks"<<endl;
cout<<"----------------------------------"<<endl;
}//end readExternal()

void calculateCGPA()
{
for(int i=0;i<5;i++)
{
total=total+internal[i]+external[i];
}//end for
cgpa=(total/5)/10;
}//end calculateTotal()

void calculateRank(Student *s,int n)


{
inti,j,k=0;
Student temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;++j)
{
if(s[i].cgpa<s[j].cgpa)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
cout<<"\t\t\tRank list: \n";
cout<<"-------------------------------------------------------------------------- \n";
cout<<"Reg.No \t Name \t CGPA \t Rank \n";
cout<<"-------------------------------------------------------------------------- \n";

for(i=0;i<n;++i)
{
s[i].rank=i+1;
if(i>0)
{
if(s[i].cgpa==s[i-1].cgpa)
s[i].rank=i;
}

cout<<s[i].regno<<"\t"<<s[i].name<<"\t"<<s[i].cgpa<<"\t"<<s[i].rank<<"\n";
}
}//end calculateRank()
};//end Student

int main()
{
Student s[10],*sptr;
inti,n;
float cgpa[10];
try
{
cout<<"Enter the number of students:";
cin>>n;
for(i=0;i<n;++i)
{
s[i].readDetail();
s[i].readInternal();
s[i].readExternal();
s[i].calculateCGPA();
}
sptr->calculateRank(s,n);
}
catch(Student::MINIMUM)
{
cout<<"Marks Should Be Positive"<<"\n";
}
catch(Student::MAXIMUM)
{
cout<<"Marks Should Not Exceed 40 for Internal and 60 for External"<<"\n";
}
return 0;
}// end main()

Output:

Enter the number of students:3

Enter the Student Name:Vimala


Enter the Reg.No:2
Internal Marks
---------------------------------------------------------------------------
Enter CPP Marks:30
Enter Operating Systems Marks:30
Enter SSC Marks:30
Enter OOAD Marks:30
Enter DS Marks:30
---------------------------------------------------------------------------
External Marks
---------------------------------------------------------------------------
Enter CPP Marks:99
Marks Should Not Exceed 40 for Internal and 60 for External
Enter the number of students:2

Enter the Student Name:Vimala


Enter the Reg.No:2
Internal Marks
------------------------------------------------------------------------- --
Enter CPP Marks:40
Enter Operating Systems Marks:40
Enter SSC Marks:40
Enter OOAD Marks:39
Enter DS Marks:40
--------------------------------------------------------------------------
External Marks
--------------------------------------------------------------------------
Enter CPP Marks:40
Enter Operating Systems Marks:40
Enter SSC Marks:39
Enter OOAD Marks:30
Enter DS Marks:40
External Marks
--------------------------------------------------------------------------

Enter the Student Name:Lincy


Enter the Reg.No:003
Internal Marks
--------------------------------------------------------------------------
Enter CPP Marks:40
Enter Operating Systems Marks:39
Enter SSC Marks:38
Enter OOAD Marks:40
Enter DS Marks:40
--------------------------------------------------------------------------
External Marks
--------------------------------------------------------------------------
Enter CPP Marks:40
Enter Operating Systems Marks:40
Enter SSC Marks:40
Enter OOAD Marks:38
Enter DS Marks:37
--------------------------------------------------------------------------
Enter the Student Name:Irene
Enter the Reg.No:004
Internal Marks
--------------------------------------------------------------------------
Enter CPP Marks:30
Enter Operating Systems Marks:30
Enter SSC Marks:30
Enter OOAD Marks:30
Enter DS Marks:20
--------------------------------------------------------------------------
External Marks
--------------------------------------------------------------------------
Enter CPP Marks:40
Enter Operating Systems Marks:40
Enter SSC Marks:40
Enter OOAD Marks:40
Enter DS Marks:40

--------------------------------------------------------------------------
Rank list:
--------------------------------------------------------------------------
Reg.No Name CGPA Rank
--------------------------------------------------------------------------
002 Vimala 7 1
003 Lincy 7 1
004 Irene 6.8 2

Enter the number of students:


1

Enter the Student Name:Hedy


Enter the Reg.No:004
Internal Marks
-------------------------------------------------------------------------
Enter CPP Marks:40
Enter Operating Systems Marks:39
Enter SSC Marks:50
Marks Should Not Exceed 40 for Internal and 60 for External
Ex. No:7 FILE OPERATIONS

Aim:
To write a c++ program to store the student details in files
Algorithm:
Step 1:Start the program.
Step 2:Create a function name called student_write() and declared all the required variables.
Step 3: Get the Markdetails from the user.
Step 4: Create a function name called student_read() and declared all the required variables.
Step 5: Using ofstream fout to store all the details in student.out file.
Step 6:In main function, call the student_write() and student_read().
Step 7:Save and execute the program.
Step 9: Stop the program.
Source Code:

#include<iostream>
#include<fstream>
using namespace std;
void student_write(int count)
{
char name[30],sname[30],scode[5];
intn,total,i,internal,external,credit; // declare necessary variables
ofstreamfout;
ifstream fin;
fout.open("student.out");
if(!fout)
{
cout<<"Error";
return;
}
for(i=0;i<count;i++)
{
cout<<"Enter the name:";
cin>>name;
fout<<name<<endl;
cout<<"\n Enter number of subjects :"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
// get subject name, subject code, credit, internal and external marks
cout<<"Enter The Subject Name :";
cin>>sname;
cout<<"Enter The Subject Code :";
cin>>scode;
cout<<"Enter The Credit :";
cin>>credit;
cout<<"Enter The Internal :";
cin>>internal;
cout<<"Enter The External :";
cin>>external;
// calculate total
total=internal+external;
fout<<"Subject Name :"<<sname<<endl;
fout<<"Subject Code :"<<scode<<endl;
fout<<"Credit :"<<credit<<endl;
fout<<"internal :"<<internal<<endl;
fout<<"external :"<<external;
fout<<"total :"<<total;
// credit, internal, external and total marks;
}
}
fout.close();
}

void student_read()
{
//declare necessary variables
ifstream fin;
ofstreamfout;
char name[20],sname[20];
inti,n,scode,credit,external,internal,total;
fin.open("student.out");
if(!fin)
{
cout<<"Error";
return;
}
while(1)
{
fin>>name;
for(i=0;i<n;i++)
{
fout<<"Subject Name :"<<sname<<endl;
fout<<"Subject Code :"<<scode<<endl;
fout<<"Credit :"<<credit<<endl;
//internal, external, total
}
if(fin.eof())
break;
cout<< "student marklist"<<endl;
cout<<"Name="<<name<<endl;
for(i=0;i<n;i++)
{
//display subject name, code, credit, internal, external and total marks
fout<<"Subject Name :"<<sname<<endl;
fout<<"Subject Code :"<<scode<<endl;
fout<<"Credit :"<<credit<<endl;
fout<<"internal :"<<internal<<endl;
fout<<"external :"<<external;
fout<<"total :"<<total;
}
fin.close();
}
}
int main()
{
int count;
cout<<"Number of students:";
cin>>count;
cout<<"Enter the Student details to be stored"<<endl;
student_write(count);
cout<<"Student details processed from the file"<<endl;
student_read();
}
Output:

Number of students:2
Enter the Student details to be stored
Enter the name:Vimala
Enter number of subjects :2

Enter The Subject Name :C


Enter The Subject Code :344

Enter The Credit :4


Enter The Internal :49

Enter The External :50

Enter The Subject Name :C++


Enter The Subject Code :322
Enter The Credit :5
Enter The Internal :50
Enter The External :49

Enter the name:Lincy


Enter number of subjects :3

Enter The Subject Name :C++


Enter The Subject Code :322
Enter The Credit :5
Enter The Internal :48
Enter The External :50

Enter The Subject Name :C


Enter The Subject Code :344
Enter The Credit :4
Enter The Internal :50
Enter The External :50
Enter The Subject Name :SSC
Enter The Subject Code :324
Enter TheCredit :6
Enter The Internal :45
Enter The External :50

Student details processed from the file


studentmarklist
Name=Vimala

“student.out”

Vimala

Subject Name :C
Subject Code :344
Credit :4
internal :49
external :50
total :99

Subject Name :C++


Subject Code :322
Credit :3
internal :50
external :49
total :99

Lincy

Subject Name :C++


Subject Code :322
Credit :5
internal :48
external :50
total :98

Subject Name :C
Subject Code :344
Credit :4
internal :50
external :50
total :100
Subject Name :SSC
Subject Code :324
Credit :6
internal :45
external :50
total :95
Ex. No. 8 CHECKING A PALINDROME USING A STACK

Aim :

To check whether the given string is a palindrome or not using a stack.

Algorithm:

1. Create a class called stack with an array to store characters and an integer pointer top.
2. Define a constructor to initialize the pointer to zero.
3. Define a method Isempty() to check whether the stack is empty or not. If the pointer is at
position 0, it returns 1; otherwise it returns 0.
4. Define a method Isfull() to check whether the stack is full or not. If the pointer reaches
the maximum size of the array, it returns 1; otherwise it returns 0.
5. Define a function Push() to push the characters in the string one by one inside the stack.
6. Define a function Pop() to pop them out in LIFO fashion.
7. Read a string as input.
8. Push the characters in the string into the stack.
9. Pop out the characters.
10. Compare the original string and popped out string. If both are equal, print it is a
palindrome. Otherwise print it is not a palindrome.
11. Stop the process

Source Code:
#include <iostream>
#include <string.h>

using namespace std;


const int MAX=50;
class stack
{
int pos;
char s[MAX];
public:
stack()
{
pos=0;
}
int isempty()
{
if (pos==0)
return 1;
else
return 0;
}

int isfull()
{
if (pos==MAX)
return 1;
else
return 0;
}

void push(char c)
{
if (!isfull())
{
s[pos]=c;
pos++;
}
}

char pop()
{
if (!isempty())
{
pos--;
return s[pos];
}
}
};

void main()
{
stack a;
char t[MAX],x[MAX];
int i;
//clrscr();
cout<<"Enter a string :";
cin>>t;
for (i=0;t[i]!='\0';i++)
{
a.push(t[i]);
}
i=0;
while (!a.isempty())
{
x[i]=a.pop();
i++;
}
x[i]='\0';
if (strcmp(t,x)==0)
cout<<t<<" is a palindrome";
else
cout<<t<<" is not a palindrome";

Output:

Enter a string malayalam

malayalam is a palindrome
ExNo:9 MATRIX MULTIPLICATION USING POINTERS

AIM: To write a C++ program to perform the matrix multiplication operation using pointers.

ALGORITHM:

Step 1: Start the program.


Step 2: Declare a function called matalloc to create a matrix of the required order.
Step 3: Declare a function called matrelease to delete the matrix.
Step 4: Declare a function called matread to get the order of the matrix.
Step 5: Declare a function called matmul to perform the matrix multiplication operation.
Step 6: Declare a function called show to display the output of the multiplication operation.
Step 7: Get the number of rows and columns for the matrices A and B as input from the user.
Step 8: Call the required functions in the main function.
Step 9: Save and execute the program.
Step10: Stop the program.
Source Code:

#include<iostream>
#include<stdlib.h>

using namespace std;


int **matalloc(introw,int col)
{
int **p;
p=new int *[row];
for(int i=0;i<row;i++)
p[i]=new int [col];
return p;
}
voidmatrelease(int **p,int row)
{
for(int i=0;i<row;i++)
delete p[i];
delete p;
}
voidmatread(int **a,introw,int col)
{
inti,j;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
cout<<"Matrx["<<i<<","<<j<<"]=?";
cin>>a[i][j];
}
}
voidmul(int **a,intm,intn,int **b,intp,intq,int **c)
{
inti,j,k;
if(n!=p)
{
cout<<"Error:invalid matrix order for multiplication";
exit(1);
}
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
void show(int **a,introw,int col)
{
inti,j;
for(i=0;i<row;i++)
{
cout<<endl;
for(j=0;j<col;j++)
cout<<a[i][j]<<" ";
}
}
int main()
{
intm,n,p,q;
int **a,**b,**c;
cout<<"Enter the matrix A details:"<<endl;
cout<<"How many rows are there:";
cin>>m;
cout<<"How many columns are there:";
cin>>n;
a=matalloc(m,n);
matread(a,m,n);
cout<<"Enter the matrix B details:"<<endl;
cout<<"How many rows are there:";
cin>>p;
cout<<"How many columns are there:";
cin>>q;
b=matalloc(p,q);
matread(b,p,q);
c=matalloc(m,q);
mul(a,m,n,b,p,q,c);
cout<<"Matrix c=A*B is";
show(c,m,q);
cout<<endl;
return 0;
}

Output:
How many rows are there:3
How many columns are there:3
Matrx[0,0]=?11
Matrx[0,1]=?22
Matrx[0,2]=?33
Matrx[1,0]=?22
Matrx[1,1]=?33
Matrx[1,2]=?11
Matrx[2,0]=?33
Matrx[2,1]=?22
Matrx[2,2]=?11
Enter the matrix B details:
How many rows are there:3
How many columns are there:3
Matrx[0,0]=?11
Matrx[0,1]=?22
Matrx[0,2]=?33
Matrx[1,0]=?22
Matrx[1,1]=?33
Matrx[1,2]=?11
Matrx[2,0]=?33
Matrx[2,1]=?11
Matrx[2,2]=?22
Matrix c=A*B is
1694 1331 1331
1331 1694 1331
1210 1573 1573
Exp.No:10 QUEUE IMPLEMENTATION USING TEMPLATES

Aim:

To write a c++ program to perform queue implementation using templates.

Algorithm:

Step 1: Start the program.

Step 2: Create a template name template Type

Step 3: Create a class named Queue.

Step 4: Using constructor initialize the variables to zero.

Step 5: Create a function name called insert() using insert the element in queue.

Step 6: Create a function name called display() using display the element

Step 7: Create a Type named del() used delete the element in queue.

Step 8: Using do-while statement display the Menu and get the input from the user.

Step 9: Using switch case statement get the option from user for Insertion, Deletion, Display and

Exit.

Step 10: Save and execute the program.

Step 10: Stop the process.

Source Code:

#include<iostream>
#include<stdlib.h>
#define SIZE 50
using namespace std;
template<class Type>
class Queue
{
Type que[SIZE]; //defines array of generic type
intfront,rear;
public:
Queue()
{
front=rear=0;
}
void insert(Type item)
{
if(rear<SIZE)
{
que[rear]=item;
rear++;
}
else
{
cout<<"Queue is full..."<<endl;
}
}
Type del()
{
if(front!=rear)
{
Type k = que[front];
front++;
return k;
}
else
{
cout<<"Queue is Empty..."<<endl;
}
}
void display()
{
for(int i=front;i<rear;i++)
{
cout<<que[i]<<endl;
}
}
};

int main()
{
Queue<int>intQ;
Queue<double>dblQ;
Queue<char>charQ;
int ch,ch1;
int ele1;
char ele2;
double ele3;
do
{
cout<<"---Menu---"<<endl;
cout<<"1. Integer Queue"<<endl;
cout<<"2. Character Queue"<<endl;
cout<<"3. Double Queue"<<endl;
cout<<"4. Exit"<<endl;
cout<<"----------"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;

switch(ch)
{
case 1:
do
{
cout<<endl<<"---Menu---"<<endl;
cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"----------"<<endl;
cout<<"Enter the choice:"<<endl;
cin>>ch1;
switch(ch1)
{
case 1:
cout<<endl<<"Enter the element to be
inserted:";
cin>>ele1;
intQ.insert(ele1);
break;
case 2:
ele1=intQ.del();
cout<<endl<<"Element "<<ele1<<" is
deleted";
break;
case 3:
intQ.display();
break;

case 4:
exit(0);
default:
cout<<endl<<"Invalid Choice";
}
}while(ch1!=3);
break;
case 2:
do
{
cout<<endl<<"---Menu---"<<endl;
cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"----------"<<endl;
cout<<"Enter the choice:"<<endl;
cin>>ch1;
switch(ch1)
{
case 1:
cout<<endl<<"Enter the element to be
inserted:";
cin>>ele2;
charQ.insert(ele2);
break;
case 2:
ele2=charQ.del();
cout<<endl<<"Element "<<ele2<<" is
deleted";
break;
case 3:
charQ.display();
break;
case 4:
exit(0);
default:
cout<<endl<<"Invalid Choice"<<endl;
}
}while(ch1!=4);
break;
case 3:
do
{
cout<<endl<<"---Menu---"<<endl;
cout<<"1. Insert"<<endl;
cout<<"2. Delete"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Exit"<<endl;
cout<<"----------"<<endl;
cout<<"Enter the choice:"<<endl;
cin>>ch1;
switch(ch1)
{
case 1:
cout<<endl<<"Enter the element to be
inserted:";
cin>>ele3;
dblQ.insert(ele3);
break;
case 2:
ele3=dblQ.del();
cout<<endl<<"Element "<<ele3<<" is
deleted";
break;
case 3:
dblQ.display();
break;
case 4:
exit(0);
default:
cout<<endl<<"Invalid Choice";
}
}while(ch1!=4);
break;
}
}while(ch!=4);

return 0;
}
OUTPUT

---Menu---
1. Integer Queue
2. Character Queue
3. Double Queue
4. Exit
----------
Enter your choice:
1
---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
1
Enter the element to be inserted:4
---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
3
4
---Menu---
1. Integer Queue
2. Character Queue
3. Double Queue
4. Exit
----------
Enter your choice:
1
---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
2
Element 4 is deleted

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
3
Queue is empty.

---Menu---
1. Integer Queue
2. Character Queue
3. Double Queue
4. Exit
----------
Enter your choice:
2

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
1

Enter the element to be inserted:V

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
3
V

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
2

Element V is deleted

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
3
Queue is empty.

---Menu---
1. Integer Queue
2. Character Queue
3. Double Queue
4. Exit
----------
Enter your choice:
3

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
1

Enter the element to be inserted:45

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
3
45

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
2

Element 45 is deleted

---Menu---
1. Integer Queue
2. Character Queue
3. Double Queue
4. Exit
----------
Enter your choice:
3
Queue is empty.

---Menu---
1. Insert
2. Delete
3. Display
4. Exit
----------
Enter the choice:
4

S-ar putea să vă placă și