Lab Programs 29 05 2021
Lab Programs 29 05 2021
Lab Programs 29 05 2021
C++ Program for Student Class to find the percentage and division for 5 students
with given details along with the name and branch of the student who has secured the
highest percentage.
//STUDENT CLASS
#include<iostream>
using namespace std;
#include<stdlib.h>
float max=0.0;
class Student
{
private:
int Stud_ID;
char Stud_Name[30];
char Stud_Branch[20];
int marks[5];
float per;
public:
void Get_INFO();
void Show_INFO();
void Find_INFO();
void Disp_INFO();
};
void Student::Get_INFO()
{
cout<<"Enter the Student ID: ";
cin>>Stud_ID;
fflush(stdin);
cout<<"\n Enter the Student Name: ";
gets(Stud_Name);
fflush(stdin);
cout<<"\n Enter the Student Branch: ";
gets(Stud_Branch);
fflush(stdin);
cout<<"\n Enter the Student Marks in 5 subjects: ";
for(int i=0;i<5;i++)
cin>>marks[i];
}
void Student::Show_INFO()
{
int tot=0;
cout<<"\n Student ID: "<<Stud_ID<<endl;
cout<<"Student Name: "<<Stud_Name<<endl;
cout<<"Student Branch: "<<Stud_Branch<<endl;
for(int i=0;i<5;i++)
tot=tot+marks[i];
cout<<"Student Total Marks: "<<tot<<endl;
per=(tot*100)/500;
cout<<"Student Percentage: "<<per<<endl;
if(per>=60)
cout<<"First Division"<<endl;
else if(per>=50 && per<60)
cout<<"Second Division"<<endl;
else if(per>=35 && per<50)
cout<<"Third Division"<<endl;
else
cout<<"FAIL"<<endl;
}
void Student::Find_INFO()
{
if(per > ::max)
::max=per;
}
void Student::Disp_INFO()
{
if(per== ::max)
cout<<"\n"<<Stud_Name<<" of "<<Stud_Branch<<" branch has secured the
highest percentage"<<endl;
}
int main()
{
Student Stud[5];
for(int i=0;i<5;i++)
Stud[i].Get_INFO();
for(int i=0;i<5;i++)
Stud[i].Show_INFO();
for(int i=0;i<5;i++)
Stud[i].Find_INFO();
for(int i=0;i<5;i++)
Stud[i].Disp_INFO();
}
2. C++ Program to find the real roots of a quadratic equation using class and friend
function.
//REAL ROOTS OF A QUADRATIC EQUATION USING FRIEND FUNCTION
#include<iostream>
using namespace std;
#include<math.h>
class Roots
{
private:
float a,b,c;
public:
void input();
friend void calc(Roots &);
};
void Roots::input()
{
cout<<"\n Enter the three coefficients for the quadratic equation: "<<endl;
cin>>a>>b>>c;
}
3. C++ Program to find the scalar product of two vector objects using friend function.
class Vector
{
private:
int size;
int a[10];
public:
void input();
friend int sprod(Vector &,Vector &);
};
void Vector::input()
{
cout<<"\n Enter the size of the vector: ";
cin>>size;
cout<<"\n Enter the elements of the vector: ";
for(int i=0;i<size;i++)
cin>>a[i];
}
int main()
{
Vector ob1,ob2;
cout<<"\n 1st Vector: ";
ob1.input();
cout<<"\n 2nd Vector: ";
ob2.input();
cout<<"\n The Scalar Product of two vectors is: "<<sprod(ob1,ob2);
}
4. C++ Program to write conversion routines for converting the temperature from
degree Celsius to degree Fahrenheit and vice-versa using suitable friend functions.
class TempC
{
private:
float tc;
public:
void input();
friend void Convert1(TempC &,TempF &);
friend void Convert2(TempC &,TempF &);
};
class TempF
{
private:
float tf;
public:
void input();
friend void Convert1(TempC &,TempF &);
friend void Convert2(TempC &,TempF &);
};
void TempC::input()
{
cout<<"\n Enter the temperature in degree Celsius: "<<endl;
cin>>tc;
}
void TempF::input()
{
cout<<"\n Enter the temperature in degree Fahrenheit: "<<endl;
cin>>tf;
}
int main()
{
TempC ob1;
TempF ob2;
ob1.input();
Convert1(ob1,ob2);
ob2.input();
Convert2(ob1,ob2);
return 0;
}
5. C++ Program to find the sum, difference and product of two complex number objects
using friend function.
//SUM, DIFFERENCE AND PRODUCT OF TWO COMPLEX NUMBERS USING
FRIEND FUNCTION
#include<iostream>
using namespace std;
class Complex
{
private:
float real;
float img;
public:
void input();
void display();
friend void sum(Complex &,Complex &);
friend void diff(Complex &,Complex &);
friend void prod(Complex &,Complex &);
};
void Complex::input()
{
cout<<"\n Enter the real and imaginary parts of the complex number: "<<endl;
cin>>real>>img;
}
void Complex::display()
{
cout<<"\n The complex number is: "<<real<<"+"<<img<<"i"<<endl;
}
void sum(Complex &a, Complex &b)
{
Complex temp;
temp.real=a.real+b.real;
temp.img=a.img+b.img;
cout<<"\n The Sum is: "<<temp.real<<"+"<<temp.img<<"i"<<endl;
}
void diff(Complex &a, Complex &b)
{
Complex temp;
temp.real=a.real-b.real;
temp.img=a.img-b.img;
cout<<"\n The Difference is: "<<temp.real<<"+"<<temp.img<<"i"<<endl;
}
void prod(Complex &a, Complex &b)
{
Complex temp;
temp.real=a.real*b.real-a.img*b.img;
temp.img=a.real*b.img+a.img*b.real;
cout<<"\n The Product is: "<<temp.real<<"+"<<temp.img<<"i"<<endl;
}
int main()
{
Complex ob1,ob2;
cout<<"\n 1st Complex Number: "<<endl;
ob1.input();
cout<<"\n 2nd Complex Number: "<<endl;
ob2.input();
cout<<"\n 1st Complex Number: "<<endl;
ob1.display();
cout<<"\n 2nd Complex Number: "<<endl;
ob2.display();
cout<<"\n Sum: "<<endl;
sum(ob1,ob2);
cout<<"\n Difference: "<<endl;
diff(ob1,ob2);
cout<<"\n Product: "<<endl;
prod(ob1,ob2);
return 0;
}
6. C++ Program to find the Sum, Difference and Product of two Matrix objects using
friend function.
//SUM, DIFFERENCE AND PRODUCT OF TWO MATRICES USING FRIEND
FUNCTION
#include<iostream>
using namespace std;
class Matrix
{
private:
int row,col;
int A[3][3];
public:
void input();
void display();
friend Matrix sum(Matrix &,Matrix &);
friend Matrix diff(Matrix &,Matrix &);
friend Matrix prod(Matrix &,Matrix &);
};
void Matrix::input()
{
cout<<"\n Enter the number of rows and columns for the matrix: "<<endl;
cin>>row>>col;
cout<<"\n Enter the matrix elements: "<<endl;
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
cin>>A[i][j];
}
void Matrix::display()
{
cout<<"\n The matrix is: "<<endl;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
}
Matrix sum(Matrix &a, Matrix &b)
{
Matrix temp;
if((a.row != b.row) || (a.col != b.col))
{
cout<<"\n The matrices are not of the same order";
exit(1);
}
else
{
temp.row=a.row;
temp.col=a.col;
for(int i=0; i<a.row; i++)
for(int j=0; j<a.col; j++)
temp.A[i][j]=a.A[i][j]+b.A[i][j];
}
return(temp);
}
int main()
{
Matrix ob1,ob2, res1, res2, res3;
cout<<"\n 1st Matrix: "<<endl;
ob1.input();
cout<<"\n 2nd Matrix: "<<endl;
ob2.input();
cout<<"\n 1st Matrix: "<<endl;
ob1.display();
cout<<"\n 2nd Matrix: "<<endl;
ob2.display();
res1=sum(ob1,ob2);
cout<<"\n Matrix Sum: "<<endl;
res1.display();
res2=diff(ob1,ob2);
cout<<"\n Matrix Difference: "<<endl;
res2.display();
cout<<"\n 1st Matrix: "<<endl;
ob1.input();
cout<<"\n 2nd Matrix: "<<endl;
ob2.input();
cout<<"\n 1st Matrix: "<<endl;
ob1.display();
cout<<"\n 2nd Matrix: "<<endl;
ob2.display();
res3=prod(ob1,ob2);
cout<<"\n Matrix Product: "<<endl;
res3.display();
return 0;
}
7. C++ Program to find the transpose of a matrix object using friend function.
class Matrix
{
private:
int row,col;
int A[5][5];
public:
void input();
void display();
friend Matrix Transpose(Matrix &);
};
void Matrix::input()
{
cout<<"\n Enter the number of rows and columns for the matrix: "<<endl;
cin>>row>>col;
cout<<"\n Enter the matrix elements: "<<endl;
for(int i=0; i<row; i++)
for(int j=0; j<col; j++)
cin>>A[i][j];
}
void Matrix::display()
{
cout<<"\n The matrix is: "<<endl;
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
cout<<A[i][j]<<" ";
cout<<endl;
}
}
Matrix Transpose(Matrix &obj)
{
Matrix temp;
temp.row=obj.col;
temp.col=obj.row;
for(int i=0; i<temp.row; i++)
for(int j=0; j<temp.col; j++)
temp.A[i][j]=obj.A[j][i];
return(temp);
}
int main()
{
Matrix ob,res;
cout<<"\n Input Matrix: "<<endl;
ob.input();
cout<<"\n Display Matrix: "<<endl;
ob.display();
res=Transpose(ob);
cout<<"\n Matrix Transpose: "<<endl;
res.display();
return 0;
}