CS202 Lab2submission LeTrungHieu 10ES

  • Upload
    le-hieu

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    1/27

    CS202: Programming Systems

    LAB 2: Friend Function, Function Overloading, and References

    Instructor: Hunh Cng Php

    Affiliation: IT Faculty, Danang University of Technology

    Name : L Trung HiuClass : 10ES

    1. Write and run the sample below

    /* Production of a Complex number and a Real number: m(a+jb)=ma + jmbCreate a friend function of both classes (Real and Complex) to perform this operation

    */

    #include

    #include class Complex;

    class Real{

    private:

    float value;

    public:Real(float v=0)

    {

    value=v;}

    void display(){

    cout

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    2/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    3/27

    2. Re-write the above program that the function prod is a member function of Complex

    class and a friend of Real class (Hint: prod should take only one argument)

    Code:

    /****************************Name: Le Trung Hieu

    Class: 10ESCS202 - Lab2 - Problem 2

    *****************************/

    #include

    #include class Real;

    class Complex

    {

    private:

    float real, image;public:

    Complex(float a=0, float b=0){

    real=a;

    image=b;}

    void display()

    {

    cout

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    4/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    5/27

    Functions Description

    Vector() Constructor allows user to enter values from keyboard

    Vector(float *a , int n) Construcror initializes values to data members from

    another array

    ~Vector() Destructor

    int capacity() Returns the current capacity of this vectorvoid clear() R Removes all of the elements from this Vector.

    bool contains(float elem) Tests if the specified object is a component in this vector.

    int indexOf(float elem) Searches for the first occurrence of the given argument.

    int lastIndexOf(float elem) Returns the index of the last occurrence of the specified

    object in this vector.

    float elementAt(int index) Returns the component at the specified index

    boolean isEmpty() Tests if this vector has no components.

    float[] toArray() Returns an array containing all of the elements in this

    Vector in the correct order.

    void display() Show values of vector

    Code:

    /****************************Name: Le Trung Hieu

    Class: 10ES

    CS202 - Lab2 - Problem 3*****************************/

    #include

    #include

    class Vector

    {private:

    int n; //Number of elementfloat *data; //Value of vector element

    public:

    Vector (); //Constructor allows user to enter values from keyboard

    Vector(float *a , int n); // Constructor initializes values to data members fromanother array

    ~Vector(){}; // Destructor

    int capacity(); //Returns the current capacity of this vector

    void clear(); // Removes all of the elements from this Vector.

    bool contains(float elem); //Tests if the specified object is a component in thisvector

    int indexOf(float elem); //Searches for the first occurrence of the given argument.int lastIndexOf(float elem); //Returns the index of the last occurrence of the specified

    object in this vector.

    float elementAt(int index); //Returns the component at the specified indexbool isEmpty(); // Tests if this vector has no components.

    float* toArray(); //Returns an array containing all of the elements in this

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    6/27

    Vector in the correct order

    void display(); // Show values of vector};

    Vector::Vector()

    {cout

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    7/27

    int Vector::indexOf(float elem){

    for (int j=0; j=0; j--)

    { if (data[j]==elem)

    return j;

    }return -1;

    }

    float Vector::elementAt(int index)

    {

    return data[index];

    }

    bool Vector::isEmpty(){if (n==0)

    return true;

    else

    return false;}

    float* Vector::toArray(){

    float* array= new float[n];

    for (int j=0; j

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    8/27

    void Vector::display()

    {for (int i=0; i

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    9/27

    cout

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    10/27

    void swap(int i, int j) swap rows i and j

    Matrix transpose() Create and return the transpose of the invoking matrix

    A.data[j][i] = B.data[i][j] ; i=0..n-1, j=0..m-1

    Matrix add(Matrix B) return C = A + Bits valid if (B.m == A.m && B.n == A.n)

    C.data[i][j]= A.data[i][j] + B.data[i][j]; i=0..n-1, j=0..m-1Matrix sub(Matrix B) return C = AB

    its valid if (B.m == A.m && B.n == A.n)C.data[i][j]= A.data[i][j] - B.data[i][j]; i=0..n-1, j=0..m-1

    boolean equal(Matrix B) Check if A = B exactly?

    Matrix prod(Matrix B) // return C = A * B

    its valid if (A.m == B.n)C.data[i][j] += (A.data[i][k] * B.data[k][j]);

    i=0..B.m-1, j=0..A.n-1, k=0..B.n-1

    display() Print matrix

    Code:

    /****************************

    Name: Le Trung HieuClass: 10ES

    CS202 - Lab2 - Problem 4

    *****************************/

    #include

    #include

    class Matrix

    {private:

    int n, m; //Row and Column of a matrixfloat data[100][100]; //Data of matrix

    public:

    Matrix(); //Construcror allows user to enter values for a matrix fromkeyboard

    Matrix(int M, int N); //create M-by-N matrix of 0's

    Matrix(float a[][100], int M, int N);//Create matrix based on 2d array

    void swap(int i, int j); //swap rows i and jMatrix transpose(); //Create and return the transpose of the invoking matrix

    //A.data[j][i] = B.data[i][j] ; i=0..n-1, j=0..m-1Matrix add(Matrix B); //return C = A + B

    //its valid if (B.m == A.m && B.n == A.n)//C.data[i][j]= A.data[i][j] + B.data[i][j]; i=0..n-1, j=0..m-1

    Matrix sub(Matrix B); //return C = AB//its valid if (B.m == A.m && B.n == A.n)//C.data[i][j]= A.data[i][j] - B.data[i][j]; i=0..n-1, j=0..m-1

    bool equal(Matrix B); //check if A = B exactly?

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    11/27

    Matrix prod(Matrix B); // return C = A * B//its valid if (A.m == B.n)//C.data[i][j] += (A.data[i][k] * B.data[k][j]);

    //i=0..B.m-1, j=0..A.n-1, k=0..B.n-1

    void display(); //Print matrix

    };

    Matrix::Matrix(){

    cout

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    12/27

    }

    void Matrix::swap(int i, int j)

    {

    float tmp;

    for (int k=0; k

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    13/27

    for(int j=0; j

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    14/27

    for (int i=0; i

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    15/27

    Result:

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    16/27

    5. Create functions:

    Functions Description Function type

    void swap(float &a,

    float &b)

    swap a and b non-member and

    inline function

    void swap(Vector

    &A, Vector&B)

    swap vector A and vector B

    its valid if (A.n == B.n)(using inline function above toswap elements)

    Friend function of

    class Vector

    void swap(Matrix

    &A, Matrix

    &B)

    swap matrix A and matrix B

    its valid if (B.m == A.m &&B.n == A.n)

    (using inline function above toswap elements)

    Friend function of

    class Matrix

    Vector&prod(Matrix &A,

    Vector &B

    Production of Matrix andVector

    (Its valid if (A.m == B.n))

    Friend function ofclass Vector and

    class Matrix

    Vector&prod(Matrix &A)

    P

    Production of Matrix andVector

    Member function ofclass Vector and

    friend function ofclass Matrix

    Vector&prod(Vector &A)

    Production of Matrix andVector

    Member function ofclass Matrix and

    friend function of

    class Vector

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    17/27

    Code:

    /****************************

    Name: Le Trung Hieu

    Class: 10ESCS202 - Lab2 - Problem 5

    *****************************/#include

    #include

    class Matrix;

    //class vectorclass Vector

    {

    private:

    int n; //Number of element

    float *data; //Value of vector elementpublic:

    Vector (); //Constructor allows user to enter values from keyboardVector(float *a , int n); // Constructor initializes values to data members from

    another array

    ~Vector(){}; // Destructorint capacity(); //Returns the current capacity of this vector

    void clear(); // Removes all of the elements from this Vector.

    bool contains(float elem); //Tests if the specified object is a component in this

    vectorint indexOf(float elem); //Searches for the first occurrence of the given argument.

    int lastIndexOf(float elem); //Returns the index of the last occurrence of the specifiedobject in this vector.float elementAt(int index); //Returns the component at the specified index

    bool isEmpty(); // Tests if this vector has no components.

    float* toArray(); //Returns an array containing all of the elements in this

    Vector in the correct ordervoid display(); // Show values of vector

    Vector &prod(Matrix &A);

    friend void swap(Vector &A, Vector &B);friend Vector &prod(Matrix &A, Vector &B);

    };

    Vector::Vector(){

    cout

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    18/27

    for (int i=0;i

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    19/27

    { if (data[j]==elem)

    return j;}

    return -1;

    }

    float Vector::elementAt(int index) //check element at specified index

    {return data[index];

    }

    bool Vector::isEmpty() // Tests if this vector has no components.{

    if (n==0)

    return true;

    else

    return false;}

    float* Vector::toArray() //Returns an array containing all of the elements in this

    Vector in the correct order

    {return data;

    }

    void Vector::display() // Show values of vector{

    for (int i=0; i

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    20/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    21/27

    n=M;

    m=N;for (int i=0;i

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    22/27

    return C;

    }

    Matrix Matrix::sub(Matrix B)

    {Matrix C(n,m);

    if ((B.n==n)&&(B.m==m)){

    for (int i=0;i

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    23/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    24/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    25/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    26/27

  • 8/11/2019 CS202 Lab2submission LeTrungHieu 10ES

    27/27