lab manual

71
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

Upload: beulshere

Post on 22-Nov-2014

114 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Lab Manual

LAB MANUAL

Subject Code : CA331

Subject Name : Programming in C++

List of Experiments:

1. Personal Details using Classes & Objects2. Academic Details using Single Inheritance3. Complex Number Manipulation using Operator Overloading4. Mark Sheet Generation using Multiple Inheritance and Array of Objects5. Sorting and Searching of Student Details using Array of Objects6. Rank List with Exception Handling7. File Operations8. Checking a Palindrome using a Stack9. Matrix Multiplication using Pointers10. Queue Implementation using Templates

Page 2: Lab Manual

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];

Page 3: Lab Manual

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;

Page 4: Lab Manual

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);

}

Page 5: Lab Manual

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

Page 6: Lab Manual

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];

Page 7: Lab Manual

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:

Page 8: Lab Manual

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

Page 9: Lab Manual

Enter the student address:NeyveliEnter the age of the student:21Enter the date of birth of the student:12.10.1988Enter the gender of the student:FemaleEnter the annual income of the parent:15000

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

******************Personal Details******************Student name:VimalaStudent address:NeyveliStudent age:21Student date of birth:12.10.1988Gender:FemaleAnnual Income:15000

****************Academic Details*********************Register Number:09CA002UG Percentage:72Pg Percentage:83UG grade:APG grade:S

Page 10: Lab Manual

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=";

Page 11: Lab Manual

cin>>real;cout<<"Enter imaginary part=";cin>>image; }void comp :: show() {cout.precision(2); if(image<0)cout<<real<<image<<"i"; elsecout<<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;

Page 12: Lab Manual

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;

Page 13: Lab Manual

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.Addition2.Subtraction3.Multiplication4.Division5.ExitEnter real part=2Enter imaginary part=2Enter real part=4Enter imaginary part=4first no:2+2isecond no:4+4ienter the choice:1

ADDITION OF COMPLEX NUMBERS-------------------------------------------------------Addition of two no:6+6i

Page 14: Lab Manual

-------------------------------------------------------do you want to continue(y/n)?=y********** Menu **********1.Addition2.Subtraction3.Multiplication4.Division5.ExitEnter real part=2Enter imaginary part=4Enter real part=1Enter imaginary part=1first no:2+4isecond no:1+1ienter the choice:2

SUBTRACTION OF COMPLEX NUMBERS-------------------------------------------------------Subtraction of two no:1-3i

-------------------------------------------------------do you want to continue(y/n)?=y********** Menu **********1.Addition2.Subtraction3.Multiplication4.Division5.ExitEnter real part=1Enter imaginary part=1Enter real part=2Enter imaginary part=2first no:1+1isecond no:2+2ienter the choice:3

MULTIPLICATION OF COMPLEX NUMBERS--------------------------------------------------------Multiplication of two no:0+4i

Page 15: Lab Manual

-------------------------------------------------------do you want to continue(y/n)?=y********** Menu **********1.Addition2.Subtraction3.Multiplication4.Division5.ExitEnter real part=4Enter imaginary part=2Enter real part=2Enter imaginary part=1first no:4+2isecond no:2+1ienter the choice:4

DIVISION OF COMPLEX NUMBERS-------------------------------------------------------Division of two no:1+0.2i

-------------------------------------------------------do you want to continue(y/n)?=n

Page 16: Lab Manual

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:

Page 17: Lab Manual

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;}

Page 18: Lab Manual

};

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;

Page 19: Lab Manual

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 CreditInternal External”;

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

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

res[i].displayData ();res[i].internalMark::displayData();res[i].externalMark::displayData();

}

Page 20: Lab Manual

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

Page 21: Lab Manual

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

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

Page 22: Lab Manual

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;

Page 23: Lab Manual

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++){

Page 24: Lab Manual

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){

Page 25: Lab Manual

flag=1;break;}else{flag=0;}}if(flag==1)cout<<"Student:"<<fname<<"Exists in the records .\n";elsecout<<"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";elsecout<<"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******************";

Page 26: Lab Manual

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";

Page 27: Lab Manual

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 data2.Put data3.Sort by first name4.Sort by Student id5.Sort by department6.Search by first name7.Search by Student id8.Exit Enter your choice :1

GET DATAEnter details of the students one by one:

Enter the register number :002Enter the first name :VimalaEnter the last name :BenjaminEnter the department name :MCAEnter the grade :S

Enter the register number :001Enter the first name :LincyEnter the last name :AbrahamEnter the department name :MBAEnter the grade :A

Enter the register number :003Enter the first name :HedyEnter the last name :BenjaminEnter the department name :MBA

Page 28: Lab Manual

Enter the grade :S

Enter the register number :004Enter the first name :JenyEnter the last name :ThomasEnter the department name :EEEEnter the grade :O

Enter your choice :2

DISPLAY DATA:

Register number :002First name :VimalaLast name :BenjaminDepartment name :MCAGrade : S

Register number :001First name :LincyLast name :AbrahamDepartment name :MBAGrade : A

Register number :003First name :HedyLast name :BenjaminDepartment name :MBAGrade : S

Register number :004First name :JenyLast name :ThomasDepartment name :EEEGrade : O

Enter your choice :3Sort by First name :

ID FIRST NAMELAST NAME DEPARTMENT GRADE:003 Hedy Benjamin MBA S

Page 29: Lab Manual

004 Jeny Thomas EEE O001 Lincy Abraham MBA A002 Vimala Benjamin MCA S

Enter your choice :4Sort by student id :

ID FIRST NAME LAST NAME DEPARTMENT GRADE:001 Lincy Abraham MBA A002 Vimala Benjamin MCA S003 Hedy Benjamin MBA S004 Jeny Thomas EEE O

Enter your choice :5Sort by department :

ID FIRST NAME LAST NAME DEPARTMENT GRADE:001 Lincy Abraham MBA A003 Hedy Benjamin MBA S002 Vimala Benjamin MCA S004 Jeny Thomas EEE O

Enter your choice :6Search by First name :Enter name to be searched :VimalaStudent:Vimala

Exists in the records .

Enter your choice :6Search by First name :Enter name to be searched :Divya

Record not found .

Enter your choice :7Search by Student id :Enter id to be searched:009

Enter your choice :7Search by Student id :Enter id to be searched:

Page 30: Lab Manual

001

Student:001

Exists in the records.

Record not found.

Enter your choice :8

Page 31: Lab Manual

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 30using namespace std;class Student{

private:char sub[MAXLIMIT][LEN],name[LEN];int internal[MAXLIMIT],external[MAXLIMIT];int total,cgpa,rank,regno;

public:

Page 32: Lab Manual

//Exception Handling Classesclass MINIMUM{};class MAXIMUM{};

//ConstructorStudent(){

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:";

Page 33: Lab Manual

cin>>external[i];if(external[i]<0)

throw MINIMUM();if(external[i]>60)

throw MAXIMUM();}//end forcout<<"External Marks"<<endl;cout<<"----------------------------------"<<endl;

}//end readExternal()

void calculateCGPA(){

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

total=total+internal[i]+external[i];}//end forcgpa=(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){

Page 34: Lab Manual

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:VimalaEnter the Reg.No:2Internal Marks

Page 35: Lab Manual

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

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

Enter the Student Name:LincyEnter the Reg.No:003Internal Marks--------------------------------------------------------------------------Enter CPP Marks:40Enter Operating Systems Marks:39Enter SSC Marks:38Enter OOAD Marks:40Enter DS Marks:40--------------------------------------------------------------------------External Marks--------------------------------------------------------------------------Enter CPP Marks:40

Page 36: Lab Manual

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

--------------------------------------------------------------------------Rank list:

--------------------------------------------------------------------------Reg.No Name CGPA Rank--------------------------------------------------------------------------002 Vimala 7 1003 Lincy 7 1004 Irene 6.8 2

Enter the number of students:1

Enter the Student Name:HedyEnter the Reg.No:004Internal Marks-------------------------------------------------------------------------Enter CPP Marks:40Enter Operating Systems Marks:39Enter SSC Marks:50Marks Should Not Exceed 40 for Internal and 60 for External

Page 37: Lab Manual

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 variablesofstreamfout;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;

Page 38: Lab Manual

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 markscout<<"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 totaltotal=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 variablesifstream fin;ofstreamfout;char name[20],sname[20];inti,n,scode,credit,external,internal,total;fin.open("student.out");if(!fin){

cout<<"Error";

Page 39: Lab Manual

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 marksfout<<"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();

}

Page 40: Lab Manual

Output:

Number of students:2Enter the Student details to be storedEnter the name:VimalaEnter number of subjects :2

Enter The Subject Name :CEnter The Subject Code :344

Enter The Credit :4Enter The Internal :49

Enter The External :50

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

Enter the name:LincyEnter number of subjects :3

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

Enter The Subject Name :CEnter The Subject Code :344Enter The Credit :4Enter The Internal :50Enter The External :50

Page 41: Lab Manual

Enter The Subject Name :SSCEnter The Subject Code :324Enter TheCredit :6Enter The Internal :45Enter The External :50

Student details processed from the filestudentmarklistName=Vimala

“student.out”

Vimala

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

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

Lincy

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

Subject Name :CSubject Code :344Credit :4internal :50external :50total :100

Page 42: Lab Manual

Subject Name :SSCSubject Code :324Credit :6internal :45external :50total :95

Page 43: Lab Manual

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;elsereturn 0;}

int isfull(){if (pos==MAX)

Page 44: Lab Manual

return 1;elsereturn 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

Page 45: Lab Manual

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){

Page 46: Lab Manual

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;

Page 47: Lab Manual

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:3How many columns are there:3Matrx[0,0]=?11Matrx[0,1]=?22Matrx[0,2]=?33Matrx[1,0]=?22Matrx[1,1]=?33Matrx[1,2]=?11Matrx[2,0]=?33Matrx[2,1]=?22Matrx[2,2]=?11Enter the matrix B details:How many rows are there:3How many columns are there:3Matrx[0,0]=?11Matrx[0,1]=?22Matrx[0,2]=?33Matrx[1,0]=?22Matrx[1,1]=?33Matrx[1,2]=?11Matrx[2,0]=?33Matrx[2,1]=?11Matrx[2,2]=?22Matrix c=A*B is1694 1331 13311331 1694 13311210 1573 1573

Page 48: Lab Manual

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 50using namespace std;template<class Type>class Queue{

Type que[SIZE]; //defines array of generic typeintfront,rear;public:

Queue(){

front=rear=0;}

Page 49: Lab Manual

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;

Page 50: Lab Manual

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:

Page 51: Lab Manual

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){

Page 52: Lab Manual

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;}

Page 53: Lab Manual

OUTPUT

---Menu---1. Integer Queue2. Character Queue3. Double Queue4. Exit----------Enter your choice:1---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:1Enter the element to be inserted:4---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:34---Menu---1. Integer Queue2. Character Queue3. Double Queue4. Exit----------Enter your choice:1---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:2

Page 54: Lab Manual

Element 4 is deleted

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:3Queue is empty.

---Menu---1. Integer Queue2. Character Queue3. Double Queue4. Exit----------Enter your choice:2

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:1

Enter the element to be inserted:V

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:3V

---Menu---1. Insert2. Delete

Page 55: Lab Manual

3. Display4. Exit----------Enter the choice:2

Element V is deleted

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:3Queue is empty.

---Menu---1. Integer Queue2. Character Queue3. Double Queue4. Exit----------Enter your choice:3

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:1

Enter the element to be inserted:45

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:3

Page 56: Lab Manual

45

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:2

Element 45 is deleted

---Menu---1. Integer Queue2. Character Queue3. Double Queue4. Exit----------Enter your choice:3Queue is empty.

---Menu---1. Insert2. Delete3. Display4. Exit----------Enter the choice:4