Thursday, December 27, 2018

CBCS C++ ASSIGNMENT

                                                                                             edited by TANIMA LODH & BKPAUL
ASSIGNMENT 1:

#include<iostream>
using namespace std;
class Area
{
private:
double l,b;
public:
Area(double x,double y)
{
l=x;
b=y;
}
void output()
{
cout<<"THE AREA OF THE RECTANGLE IS:"<<l*b;
}
};
int main()
{
double x,y;
cout<<"ENTER THE LENGTH AND THE BREADTH OF THE RECTANGLE: \n";
cin>>x>>y;
Area a(x,y);
a.output();
return 0;
}


OUTPUT:

ENTER THE LENGTH AND THE BREADTH OF THE RECTANGLE:
2.1
4.5
THE AREA OF THE RECTANGLE IS:9.45
--------------------------------
Process exited after 4.91 seconds with return value 0
Press any key to continue . . .

ASSIGNMENT 2:

#include<iostream>
#include<math.h>
using namespace std;
class Tri_Area
{
private:
double a,b,c;
public:
Tri_Area(double x, double y, double z)
{
a=x;
b=y;
c=z;
}
void output()
{
double s;
s=(a+b+c)/2;
cout<<"THE AREA OF THE TRIANGLE IS:"<<sqrt(s*(s-a)*(s-b)*(s-c));
}
};
int main()
{
cout<<"ENTER LENGTH OF THREE SIDES OF A TRIANGLE: \n";
double x,y,z;
cin>>x>>y>>z;
Tri_Area t(x,y,z);
t.output();
}

OUTPUT:

ENTER LENGTH OF THREE SIDES OF A TRIANGLE:
1.2
3.2
3.4
THE AREA OF THE TRIANGLE IS:1.91977
--------------------------------
Process exited after 13.29 seconds with return value 0
Press any key to continue . . .

ASSIGNMENT 3:

#include<iostream>
using namespace std;
class Distance
{
private:
int ft,in;

public:
Distance()
{
}
Distance(int f, int i)
{
ft=f;
in=i;
int r;
if(in>=12)
{
r=in/12;
in=in%12;
}
ft=ft+r;
}
void show_distance(Distance ob1,Distance ob2)
{
cout<<"THE 1st DISTANCE IS:"<<ob1.ft<<" feet "<<ob1.in<<" inches \n";
cout<<"THE 2nd DISTANCE IS:"<<ob2.ft<<" feet "<<ob2.in<<" inches \n";
}
void add(Distance ob1,Distance ob2)
{
Distance ob;
ob.ft=ob1.ft+ob2.ft;
ob.in=ob1.in+ob2.in;
if(ob.in>=12)
{
ob.in=ob.in-12;
ob.ft=ob.ft+1;
}
cout<<"THE RESULTANT DISTANCE IS:"<<ob.ft<<" feet "<<ob.in<<" inches";
}
};

int main()
{
int w,x,y,z;
cout<<"ENTER THE FEET AND INCHES OF 1ST DISTANCE: \n";
cin>>w>>x;
cout<<"ENTER THE FEET AND INCHES OF 2ND DISTANCE: \n";
cin>>y>>z;
Distance ob1(w,x), ob2(y,z),ob;
ob.show_distance(ob1,ob2);
ob.add(ob1,ob2);
}

OUTPUT:

ENTER THE FEET AND INCHES OF 1ST DISTANCE:
5
10
ENTER THE FEET AND INCHES OF 2ND DISTANCE:
9
17
THE 1st DISTANCE IS:5 feet 10 inches
THE 2nd DISTANCE IS:10 feet 5 inches
THE RESULTANT DISTANCE IS:16 feet 3 inches
--------------------------------
Process exited after 7 seconds with return value 0
Press any key to continue . . .

Assignment 4:

#include<iostream>
#include<string.h>
using namespace std;
class Student
{
private:
char name[20];
int marks1,marks2,marks3;
public:
Student()
{
cout<<"ENTER THE NAME: \n";
gets(name);
cout<<"ENTER THE MARKS: \n";
cin>>marks1>>marks2>>marks3;
}
void show_student()
{
cout<<"NAME:";
puts(name);
cout<<"MARKS OBTAINED BY "<<name<<" are: \n 1)"<<marks1<<"\n 2)"<<marks2<<"\n 3)"<<marks3<<endl;
}
void grade()
{
int avg=(marks1+marks2+marks3)/3;
if(avg>=80)
cout<<"GRADE A";
else
{
if(avg>=60)

cout<<"GRADE B";
else
{
if(avg>=40)
cout<<"GRADE C";
else
cout<<"FAIL";
}
}

}
};
int main()
{
Student s;
s.show_student();
s.grade();
}

Output :

ENTER THE NAME:
Tanima Lodh
ENTER THE MARKS:
98
95
93
NAME:Tanima Lodh
MARKS OBTAINED BY Tanima Lodh are:
 1)98
 2)95
 3)93
GRADE A
--------------------------------
Process exited after 15.88 seconds with return value 0
Press any key to continue . . .

Assignment 5:
#include<iostream.h>
class matrix
{
int mat[5][5],row,column;
public:
matrix(int,int);
matrix(matrix&);
void getdata();
matrix operator-(matrix);
matrix operator+(matrix);
matrix operator*(matrix);
void display();
};
matrix::matrix(int r,int c)
{
cout<<"constructor to initialize no. of rows and coloumns"<<endl;
row=r;
column=c;
}
matrix::matrix(matrix& m)
{
row=m.row;
column=m.column;
cout<<"copy constructor \n";
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
mat[i][j]=m.mat[i][j];
}
}
}
void matrix::getdata()
{
cout<<"value input: \n";
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cout<<"value("<<i+1<<")("<<j+1<<"):";
cin>>mat[i][j];
}
}
}
matrix matrix::operator+(matrix a)
{
cout<<"addition operator \n";
matrix temp(row,column);
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
temp.mat[i][j]=mat[i][j]+a.mat[i][j];
}
}
return temp;
}
matrix matrix::operator-(matrix a)
{
cout<<"subtraction operator \n";
matrix temp(row,column);
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
temp.mat[i][j]=mat[i][j]-a.mat[i][j];
}
}
return temp;
}
matrix matrix::operator*(matrix a)
{
cout<<"multiplication operator \n";
matrix temp(row,column);
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
temp.mat[i][j]=0;
for(int k=0;k<column;k++)
{
temp.mat[i][j]=temp.mat[i][j]+(mat[i][k]*a.mat[k][j]);
}
}
}
return temp;
}
void matrix::display()
{
cout<<"the matrix is: \n";
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<endl;
}
}
void  main()
{
matrix m1(2,2),m2(2,2),m3(2,2);
m1.getdata();
m2.getdata();
m3=m1+m2;
m3.display();
m3=m1-m2;
m3.display();
m3=m1*m2;
m3.display();

}
Output:





Assignment 6:
#include<iostream.h>
#include<math.h>
class triangle
{
private:
int x1,y1,x2,y2,x3,y3;
public:
triangle(int a,int b, int c,int d,int e,int f)
{
x1=a; y1=b;  x2=c; y2=d; x3=e; y3=f;

}
void output()
{
double l1= sqrt((y2-y1)*(y2-y1)-(x2-x1)*(x2-x1));
double l2=sqrt((y3-y2)*(y3-y2)-(x3-x2)*(x3-x2));
double l3=sqrt((y1-y3)*(y1-y3)-(x1-x3)*(x1-x3));
if(l1==l2 && l2==l3)
{
cout<<"equilateral";
}
else
{
if(l1==l2||l1==l3)
{
cout<<"isosceles";
}
else
{
cout<<"scalene";
}
}
}
~triangle(){}
};
void main()
{
int a,b,c,d,e,f;
cout<<"enter the coordinates of the 1st triangle:";
cin>>a>>b;
cout<<"enter the coordinates of the 2nd triangle:";
cin>>c>>d;
cout<<"enter the coordinates of the 3rd triangle:";
cin>>e>>f;
triangle t(a,b,c,d,e,f);
t.output();
}
Output:




Assignment 7:
#include<iostream.h>
#include<math.h>
class polar
{
private:
int r, theta;
public:
void input()
{
cout<<"enter the polar coordinates of the point: \n";
cin>>r>>theta;
}
double getx()
{
return r*cos(theta);
}
  double gety()
{
return r*sin(theta);
}
void display()
{
cout<<"radius="<<r<<endl<<"angle="<<theta;
}
polar operator+(polar p)
{
double x=getx()+p.getx();
double y=gety()+p.gety();
 polar temp;
double z=sqrt(x*x+y*y);
double a=atan(y/x);
temp.r=z;
temp.theta=a;
return temp;
}
};
void main()
{
polar p1,p2,p3;
p1.input();
p2.input();
p3=p1+p2;
p3.display();
}
Output:




Assignment 8:
#include<iostream.h>
class fraction
{
private:
int n,d;
public:
void input()
{
cout<<"enter the numerator of the fraction: \n";
cin>>n;
cout<<"enter the denominator of the fraction: \n";
cin>>d;
}
fraction operator+(fraction f)
{
fraction temp1;
temp1.d=d*f.d;
temp1.n=n*f.d+f.n*d;
return temp1;
}
fraction operator-(fraction f)
{
fraction temp2;
temp2.d=d*f.d;
temp2.n=n*f.d-f.n*d;
return temp2;
}
void display();

};
void fraction::display()
{    int cf;
if(n>0 && d>0)
{
for(int i=1;i<=n && i<=d;i++)
{
  if(n%i==0&&d%i==0)
  {
  cf=i;
  }
}
cout<<"the fraction is:"<<endl<<n/cf<<"/"<<d/cf<<endl;
}
if(n<0)
{
n=n*(-1);
 for(int i=1;i<=n && i<=d;i++)
{
  if(n%i==0&&d%i==0)
  {
  cf=i;
  }
}
cout<<"the fraction is:"<<endl<<"-"<<n/cf<<"/"<<d/cf<<endl;
}
}
void main()
{
fraction f1,f2,f3,f4;
f1.input();
f2.input();
f1.display();
f2.display();
f3= f1+f2;
cout<<"after addition \n";
f3.display();
 f4= f1-f2;
cout<<"after subtraction \n";
f4.display();
}
Output:





Assignment 11:
#include<iostream.h>
class bit_op
{
private:
int a,b;
public:
bit_op(int x,int y)
{
  a=x;
  b=y;
}
void bit_and()
{
int m=a&b;
cout<<a<<" AND "<<b<<"="<<m<<endl;
}
void bit_or()
{
int n=a|b;
cout<<a<<" OR "<<b<<"="<<n<<endl;
}
void bit_xor()
{
int o=a^b;
cout<<a<<" XOR "<<b<<"="<<o<<endl;
}
};
void main()
{
cout<<"enter two integers"<<endl;
int x,y;
cin>>x>>y;
bit_op b(x,y);
b.bit_and();
b.bit_or();
b.bit_xor();
}
Output: