Subject Code: Subject Name:: Lab Manual
Subject Code: Subject Name:: Lab Manual
Subject Code: Subject Name:: Lab Manual
List of Experiments:
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 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 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.
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()
cin>>n;
cin>>fn;
cout<<"Enter sex:";
cin>>sex;
cin>>eq;
cin.ignore();
cin.getline(ad,50);
cin.ignore();
cin.getline(ph,12);
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 sex:Female
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:
Source Code:
#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<<"\n";
cout<<"-------------------------------------------------------\n";
cout<<"do you want to continue(y/n)?=";
cin>>ans;
}
while(ans=='y'||ans=='Y');
return 0;
//getch();
}
OUTPUT:
-------------------------------------------------------
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
-------------------------------------------------------
do you want to continue(y/n)?=n
Ex. No. 4 MARKSHEET GENERATION USING MULTIPLE INHERITANCE
Aim:
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>
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";
}
};
cin>>subMark;
if (subMark<0 || subMark>50)
{
cout<<"Invalid marks\n";
readData();
}
}
void displayData()
{
cout<<internalMark()<<"\t\t";
}
int internalMark()
{
return subMark;
}
};
cin>>subMark;
if (subMark<0 || subMark>50)
{
cout<<"Invalid marks\n";
readData();
}
}
void displayData()
{
cout<<externalMark()<<"\t\t";
}
int externalMark()
{
return subMark;
}
};
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 no of subjects : 3
*****************************************************************************
KARUNYA UNIVERSITY
MARK SHEET
****************************************************************************
****************************************************************************
****************************************************************************
****************************************************************************
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";
}
}
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:
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:
DISPLAY DATA:
Student:001
AIM:
ALGORITHM:
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()
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:
--------------------------------------------------------------------------
Rank list:
--------------------------------------------------------------------------
Reg.No Name CGPA Rank
--------------------------------------------------------------------------
002 Vimala 7 1
003 Lincy 7 1
004 Irene 6.8 2
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
“student.out”
Vimala
Subject Name :C
Subject Code :344
Credit :4
internal :49
external :50
total :99
Lincy
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 :
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>
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:
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:
#include<iostream>
#include<stdlib.h>
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:
Algorithm:
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.
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
---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
---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