Lab #1

Question #1

Write a C++ program that reads three coefficients a, b and c for quadratic equation and finds whether the solutions are in real or imaginary. (ax2 + bx + c = 0 if b2-4ac >=0 then the solutions are real.)

#include <iostream>

#include <math.h>

using namespace std;

int main()

{

        float a,b,c;

    cout<<"Enter coefficient a of x^2\t";

    cin>>a;

    cout<<"Enter coefficient b of x^1\t";

    cin>>b;

    cout<<"Enter coefficient c of x^0\t";

    cin>>c;

   

    float det=(b*b-4*a*c);

   

    if(det>0)

    {

    cout<<"root is real"<<endl;

    cout<<(-b+sqrt(det))/(2*a)<<"is the 1st root"<<endl;

    cout<<(-b-sqrt(det))/(2*a)<<"is the 2nd root"<<endl;

    }

    else if(det==0)

    {

    cout<<"root is equal"<<endl;

    cout<<-b/(2*a)<<"is the root"<<endl;

    }

    else

    {

    cout<<"root is imaginary"<<endl;

    cout<<(-b/(2*a))<<"+"<<"i"<<(sqrt(-det))/(2*a)<<" is the 1st root"<<endl;

     cout<<(-b/(2*a))<<"-"<<"i"<<(sqrt(-det))/(2*a)<<" is the 2nd root"<<endl;

    }

   

    return 0;

}

Question #2

Write a C++ program that reads ten positive numbers from the user and finally prints the largest of all. (use for loop, if condition and function.)

#include <iostream>

using namespace std;

int greatest(int a[],int n)

{

        int b,i;

        b=a[0];

        for(i=1;i<n;i++)

        {

                if(a[i]>b)

                        b=a[i];

        }

        return(b);

}

int main()

{

 int a[10],i,b;

 

 

//input section

 for(i=0;i<10;i++)

 {

 cout<<"enter number"<<i+1<<"\t";

 cin>>a[i];

 }

 

 

//passing an array to the function. Here, a is an array.

cout<<"The greatest number is "<<greatest(a,10);

 

 return 0;

}

Lab #2

Question #1

Write a C++ program using function (pass by reference) that calculates the values of x and y from the two linear equations.

ax + by = m

cx + dy = n

The solutions are given as

x = (md - bn)/(ad - cb)

y = (na - mc)/(ad - cb)

The function should take eight arguments and return nothing.

#include <iostream>

using namespace std;

void solveeqn(int &a,int &b,int &m,int &c,int &d,int n)

{

        int x,y;

        x=(m*d-b*n)/(a*d-c*b);

        y=(n*a-m*c)/(a*d-c*b);

       

        cout<<"The value of x is "<<x<<" and "<<endl;

        cout<<"The value of y is "<<y<<endl;

       

}

int main()

{

    int a,b,c,d,m,n;

   

    //inputing equation 3

    cout<<"enter the coefficient of x,y and constant term of eqn 1\n";

    cin>>a>>b>>m;

    cout<<"equation 1st : "<<a<<"x+"<<b<<"y="<<m<<endl<<endl;

   

    //inputing equation 2

    cout<<"enter the coefficient of x,y and constant term of eqn 2\n";

    cin>>c>>d>>n;

    cout<<"equation 1st : "<<c<<"x+"<<d<<"y="<<n<<endl<<endl;

   

    //calling a function

    solveeqn(a,b,m,c,d,n);

    return 0;

}

Classwork:7/29/2020

Class to find the area of a triangle

#include <iostream>

using namespace std;

class area

{

        public:

        int a,b;

       

        void input(int x,int y)

        {

                a=x;

                b=y;

        }

       

        int trianglearea()

        {

                return ((a*b)/2);

        }

       

        int rectanglearea()

        {

                return (a*b);

        }

};

int main()

{

    area o1;

   

    int x,y;

    //taking input

    cout<<"enter x and y";

    cin>>x>>y;

   

    o1.input(x,y);

    //output

    cout<<"Area of triangle is "<<o1.trianglearea()<<endl;

    cout<<"Area of rectangle is "<<o1.rectanglearea();

}

Lab #3

Do you remember a graph paper; plotting x-axis, y-axis and origin (0,0). A Point consists of two values; one is x-axis value and other one is y-axis value. Considering only a first quadrant and two such points, write a program that finds the distance between each other. Use class and objects

#include <iostream>

#include <math.h>

using namespace std;

class point

{

        public:

        float x,y;

       

        point(int a=0,int b=0)

        {

                x=a;

                y=b;

        }

       

        float distance(point c)

        {

                return (sqrt(pow((x-c.x),2)+pow((y-c.y),2)));

        }

};

int main()

{

   point p1(1,2),p2(3,4);

   float d;

   

   d=p1.distance(p2);

   

   cout<<"distance is "<<d;

}

Lab #4

Define a class called "Rectangle" with following attributes: length and breadth of data type Integer. Also include the following member functions:

void setSize(int length, int breadth); // this function should set the value of length and breadth of the Rectangle.

int getArea( ); // this function should return the area of the rectangle.

int getPerimeter( ); // this function should return the perimeter of the rectangle.

// formula to calculate: area = length * breadth.

// formula to calculate: perimeter = 2 * (length + breadth)

Write a driven program as well.

1st method

#include <iostream>

#include <math.h>

using namespace std;

class rectangle

{

  public:

  int length,breadth;

 

  void setSize(int l,int b)

  {

          length=l;

          breadth=b;

  }

 

 int getarea()

 {

         return(length*breadth);

 }

 

 int getperimeter()

 {

         return(2*(length+breadth));

 }

 

};

int main()

{

    rectangle r;

    int l,b ;

   

    cout<<"Enter length and breadth of rectangle\t";

    cin>>l>>b;

   

   

    r.setSize(l,b);

   

    cout<<"The area of rectangle is "<<r.getarea()<<"\nand the perimeter of rectangle is "<<r.getperimeter();

   

}

2nd method

#include <iostream>

#include <iostream>

using namespace std;

class rectangle

{

  public:

  int length,breadth;

 

 

  void setSize(int length,int breadth)

  {

         this->length=length;

          this->breadth=breadth;

  }

 

 int getarea()

 {

         return((length*breadth));

 }

 

 int getperimeter()

 {

         return(2*(length+breadth));

 }

 

 void display()

 {

         cout<<"The area of rectangle is "<<getarea()<<"\nand the perimeter of rectangle is "<<getperimeter();

 }

 

};

int main()

{

    rectangle r;

    int l,b ;

   

    cout<<"Enter length and breadth of rectangle\t";

    cin>>l>>b;

     r.setSize(l,b);

    r.display();

   

    }

Lab#5

Question #1

class Complex

Private:

int x;

int y;

public:

Complex( );

Complex( int x, int

y);

1. Define a class called Complex.

2. Define member functions that overload the following operators:

Minus unary operator. � Returns void

Scalar multiplication. (you may use friend function) and returns Complex

Plus binary operator (+). => Returns Complex

Minus binary operator. => Returns Complex

+= Shorthand operator. => Returns void

== Equals to operator. => Returns TRUE or FALSE

Greater than operator. => Returns TRUE or FALSE

! = Not equals to operator. => Returns TRUE or FALSE

Pre Increment operator. => Returns Complex

Post Increment operator. => Returns Complex

<< Stream Insertion operator. (use friend function. Why?????) => Returns

ostream&

Write a main( ) function to implement the above-overloaded operators.

#include <iostream>

using namespace std;

class complex

{

        int x,y;

       

       

       

         public:

        complex(int a=0,int b=0)

        {

                x=a;

                y=b;

               

        }

       

        int setvalue(int a=0,int b=0)

        {

                x=a;

                y=b;

               

        }

       

          complex operator+(complex c)

          {

                  complex temp;

                  temp.x=x+c.x;

                  temp.y=y+c.y;

                  return temp;

          }

         

          complex substraction(complex c)

          {

                  complex temp;

                  temp.x=x-c.x;

                  temp.y=y-c.y;

                  return temp;

          }

         

          friend complex operator*(complex c1,complex c2);

         

         

          complex unary()

          {

               

                 x++;

                 y++;

                     

          }

         

          int getx()

          {

                  return x;

          }

         

          int gety()

          {

                  return y;

          }

       

};

complex operator*(complex c1,complex c2)

          {

                 complex temp;

                  temp.x=c1.x*c2.x+c1.y*c2.y;

                  temp.y=c1.x*c2.y+c2.x*c1.y;

                  return temp;  

          }

int main()

{

    complex c1(1,2),c2(3,4),c3,c4,c5;

    c3=c1+c2;  //c3=c1.operator+(c2);

    c4=c1*c2;

    c1.unary();

    c5=c1.substraction(c2);

   

    cout<<"Addition: "<<c3.getx()<<"+i"<<c3.gety()<<endl;

    cout<<"substraction: "<<c5.getx()<<"+i"<<c5.gety()<<endl;

    cout<<"multiplication: "<<c4.getx()<<"+i"<<c4.gety()<<endl;

    cout<<"unary: "<<c1.getx()<<"+i"<<c1.gety()<<endl;

   

}

Lab #6

Question #1

Create a base class called Shape. Use this class to store two double type values that could be

used to compute the area of figures, Derive two specific classes called Triangle and Rectanlge

from the base Shape. Add to the base class, a member function set_data() to initialize base

class data members and another member function display_data() to compute and display the

area of figures. Make display_area() as a virtual function and redefine this function in the

derived classes to suit their requirements.

Using these three classes, design a program that will accept dimensions of a triangle or a

rectangle interactively, and display the area.

#include <iostream>

using namespace std;

class shape

{

        public:

       double side_one,side_two,area;

       

       shape()

       {

          side_one=0;

          side_two=0;

       }

       

       shape(double a,double b)

       {

          side_one=a;

          side_two=b;

       }

       

       void set_data(double a,double b)

       {

               side_one=a;

               side_two=b;

       }

       

       virtual void display_area()=0;

       

        void display_data()

       {

               cout<<"the area is"<<area<<endl;

       }

};

class rectangle:public shape

{

       public:

       void display_area()

       {

               area=side_one*side_two;

       }

};

class triangle:public shape

{

       public:

       void display_area()

       {

               area=0.5*side_one*side_two;

       }

};

int main()

{

       rectangle r;

       r.set_data(1,2);

        r.display_area();

        r.display_data();

       

       triangle t;

       t.set_data(3,4);

       t.display_area();

       t.display_data();

     

       return 0;

     

 

}

Lab#7

#include <iostream>

using namespace std;

class shape

{

protected:

int x;

int y;

public:

Shape(int a,int b )

{

        x=a;

        y=b;

        }

Shape( )

{

        x=0;

        y=0;

}

virtual void draw ( ) = 0;

};

Class Circle : public shape

{

protected:

int radius;

public:

Circle( )

{

        x=0;

        y=0;

        radius=0;

}

Circle( int a, int b, int r)

{

        x=a;

        y=b;

        radius=r;

}

void draw( );

};

Class Ellipse : public shape

{

Protected:

int xradius;

int yradius;

public:

Ellispse( )

{

        x=0;

        y=0;

        xradius=0;

        yradius=0;

}

Ellipse( int a, int b, int xr, int yr)

{

        x=a;

        y=b;

        xradius=xr;

        yradius=yr;

}

void draw( );

};

class Rectangle : public shape

{

protected:

int x1;

int y1;

public:

Rectangle( )

{

       

}

Rectangle(int a,int b,int c,int d)

{

        x=a;

        y=b;

        x1=c;

        y1=d;

}

void draw( );

};

int main()

{

       rectangle r;

       r.set_data(1,2);

        r.display_area();

        r.display_data();

       

       triangle t;

       t.set_data(3,4);

       t.display_area();

       t.display_data();

     

       return 0;

     

 

}

Question 2

#include <iostream>

#include <cstring>

using namespace std;

template <class T>

class perform

{

        T *array;

        int size;

       

        public:

        perform(T *a, int size)

        {

                array= new T[size];

                memcpy(array,a,size*sizeof(T));

                this->size=size;

        }

       

        void sort_asc()

        {

               

                for(int m=0; m<size-1; m++)

{

for(int n=m+1; n<size; n++)

{

if(array[m]>array[n]) // for ascending order

{

T temp;

temp = array[m];

array[m] = array[n];

array[n] = temp;

}

}

}

}

void display()

{

        for(int i=0;i<size;i++)

        cout<<*(array+i)<<"   ";

}

};

int main()

{

        int a[]={1,5,8,19,12};

        perform<int> o(a,5);

        cout<<"number before swapping"<<endl;

        o.display();

        cout<<endl<<"number after swapping"<<endl;

        o.sort_asc();

        o.display();

        return 0;

}

                                                                                            Prepared By:Bikram Singh Bhattarai