NEW C++ Lab Manual

  • Upload
    vplvpl

  • View
    265

  • Download
    1

Embed Size (px)

Citation preview

  • 8/12/2019 NEW C++ Lab Manual

    1/35

    Matrix-Vector Multiplication using friend function

    Aim:

    To write C++ program to define matrix and vector class, to use function with default

    argument and to do matrix-vector multiplication using friend function.

    Algorithm:1. Declare vector Class

    2. Define matrix Class

    3. Declare friend function multiply() inside the matrix class

    4. Define vector Class

    5. Declare friend function multiply() inside the vector class

    6. Define getvector() function with for loop to get the el ements for vector

    7. Define disvector() functi on with for loop to display the contents of vector

    8. Define getmatrix() function with nested for loops to get the matrix elements

    9. Define dismatrix() function with nested for loops to display the matrix

    10. Define the multiply() to multiply matrix and vector

    a. No of columns in the matrix should be equal to no. of elements in the vector

    b. Appl y the matrix-vector multiplication mechanism:

    a b x (a*x)+(b* )=

    c d (c*x)+(d * )

    c. Display the resultant matrix in the screen

    11. Define main() to create objects and to call the defined functions.

    Program:

    #include

    #include

    class vector; //class declarationneeded, because this class referred before it is defined in the

    friend function.

    class matrix

    { int m[3][3];public:

    void getmatri x(void);

    void dismatrix(void);

    friend void multi ply(matrix &, vector &);

    };

    class vector

    { int v[10];

    public:

    //default argument is 3

    void getvector(int n=3);

    void disvector(void);

    friend void multiply(matrix &, vector &);

    };

    void vector::getvector(int n)

    { int i;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    2/35

    void matrix::dismatrix()

    { int i, j;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    3/35

    Arithmetic Operations on Complex Number using Operator Overloading Ex.No.2

    Aim:

    To write C++ program to define complex number class and to do arithmetic operations

    on it using operator overloading.

    Algorithm:1. Define complex Class.

    2. Define default constructor

    3. Define conversion constructors

    4. Declare friend function to overload >> and >(i stream &, complex &);

  • 8/12/2019 NEW C++ Lab Manual

    4/35

    friend ostream& operator(istream &in, complex &c)

    {

    coutc.real;

    coutc.imag;

    return in;

    }

    ostream& operator

  • 8/12/2019 NEW C++ Lab Manual

    5/35

    qt = c.real*c.real+c.imag*c.imag;

    temp.real = (real*c.real+imag*c.imag)/qt;

    temp.imag = (i mag*c.real-real*c.i mag)/qt;

    return temp;

    }

    void main()

    {

    complex c1, c2, c3,c4(4,9),c5(3.23004,4.666304444);

    double t;

    clrscr();

    t=condou(c5);

    coutc1;

    coutc2;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    6/35

    Infant Jesus College of Engineering

    Matrix Class with Dynamic Memory AllocationEx.No.3

    Aim:

    To write C++ program to define matrix class with dynami c memory allocation and to use

    constructor, destructor, copy constructor and assignment operator overloading.

    Algorithm:1. Define matrix Class.

    2. Define default constructor

    3. Declare constructor for dynamic memory allocation

    4. Declare matrix destructor

    5. Declare the functions

    6. Declare the assignment operator overloading function within the class

    7. Define all the functions.

    8. Create objects for matrix class in main() function.

    9. The memory for the objects is dynamically allocated.

    10. Invoke copy constructor function

    11. Invoke assignment operator overloading function.

    Program:

    //Matrix Class - Dynamic Memory Allocation

    /*Program to explain Constructor, Destructor, Copy constructor and Assignment operator

    overloading*/

    #include

    #include

    class matrix

    { int **m;

    int row, col;

    public:

    matrix()

    { row=col=0;

    m=NULL;}

    matrix(int r ,int c);

    ~matrix();

    void getmatri x();

    void showmatrix();

    matrix(matrix &m2); //copy constructor

    matrix& operator=(matrix &m2);

    };

    matrix::~matrix()

    { for(int i=0;i

  • 8/12/2019 NEW C++ Lab Manual

    7/35

    Infant Jesus College of Engineering

    matrix::matrix(matrix &m2)

    { cout

  • 8/12/2019 NEW C++ Lab Manual

    8/35

    getch();

    }

  • 8/12/2019 NEW C++ Lab Manual

    9/35

    Overloading new and delete operatorEx.No.4

    Aim:

    To write C++ program to overload new and delete operators to provide custom dynamic

    memory allocation.

    Algorithm:1. Define vector Class.

    2. Define new overloaded method

    a. Accepts the size as input

    b. Memory is created using malloc()

    c. If there is no memory available, execution will be stopped.

    d. Address is returned.

    3. Define delete overloaded method

    a. Object is removed from the memory using free()

    4. Declare the functions read(), max() and sum()

    5. Define all the functions.

    6. Create objects for vector class in main() function.

    7. The memory for the objects is dynamically allocated.

    8. Invoke the functions dynamically.

    9. Display the results.

    10. Invoke the delete operator.

    Program :

    /*Overloading new and delete operator usi ng malloc and free*/

    #include

    #include

    #include

    class vector

    { private:

    int *array;

    public:void *operator new(size_t size)

    {

    void *v;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    10/35

    void vector::read(int s)

    {

    for(int i=0; i

  • 8/12/2019 NEW C++ Lab Manual

    11/35

    Template for Linked List Class and its MethodsEx.No.5

    Aim:

    To write C++ program to develop a template for linked list class and its methods.

    Algorithm:

    1. Define struct node2. Define list class with declaration of constructor and functions.

    3. Define all the constructor and functions.

    4. Length function will return the size of the list.

    5. Makeempty() function will delete all the items in the list.

    6. Insert() function will insert the item at the first position of the list.

    7. Remove() function will remove item in the list which is entered by the user .

    8. Create list object of list class in main() function.

    9. Get the choice from the user

    10. Invoke the functions appropriately.

    11. Display the results.

    Program :

    #include

    #include

    #include

    template

    struct node

    { type data;

    node* next;

    };

    template

    class list

    { public:

    list();

    int length(void) const;void makeempty(void);

    void insert(void);

    void remove(void);

    void display(void);

    private:

    node* linklist;

    int count;

    };

    template

    void list::display(void)

    {

    node* cur = linklist;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    12/35

    template

    list::list()

    {

    count=0;

    linklist=NULL;

    }

    template

    int list::length(void) const

    {

    return count;

    }

    template

    void list::makeempty(void)

    {

    node* temp;

    while(linklist !=NULL)

    {

    temp=linklist;

    linklist=linklist->next;

    delete temp;}

    count=0;

    coutnext = linkl ist;

    linklist=temp;

    count++;

    }

    template

    void list::remove(void)

    {

    node* cur = linklist;

    type item;

    coutitem;

    node* temp;if(item==linklist->data)

    {

    temp = cur;

    linklist=linklist->next;

    }

    else

    {

  • 8/12/2019 NEW C++ Lab Manual

    13/35

    while(!(item==(cur->next->data)))

    cur=cur->next;

    temp = cur->next;

    cur->next = (cur->next)->next;

    }

    delete temp;

    count--;

    }

    void main()

    { int ch;

    list list1;

    clrscr();

    while(1)

    {

    cout

  • 8/12/2019 NEW C++ Lab Manual

    14/35

    Sorting Algorithms Using TemplatesEx.No.6(6a) Merge sort using templates

    Aim:To write a program to do merge sort using templates.

    Algorithm:

    1. Get the array length.. Get the array elements.

    . If the l ist i s of length 0 or 1, then it is already sorted. Otherwise:

    . Divide the unsorted list i nto two sublists of about half the size.

    . Sort each subl ist recursively by re-applying merge sort.

    . Merge the two sublists back into one sorted list.

    . Display the sorted list.

    Program:

    #include

    #include

    #include

    template

    class sort

    {

    t a[10];

    public:

    void get(int);

    void merge(int,int);

    void mergesort(int,int,int);

    void display(int);

    };

    template

    void sort ::get(int n)

    {

    int i;cout

  • 8/12/2019 NEW C++ Lab Manual

    15/35

    merge(mid+1,high);

    mergesort(low,mid,high);

    }

    }

    template

    void sort::mergesort(i nt low,int mid,int high)

    {

    t b[10];

    int h,i,j,k;

    h=low;

    i=low;

    =mid+1;

    while((h

  • 8/12/2019 NEW C++ Lab Manual

    16/35

    sortn2;

    coutn;

    n1.get(n);

    n1.merge(1,n);

    n1.display(n);

    n2.get(n);

    n2.merge(1,n);

    n2.display(n);

    getch();

    }

    (6b) Quick sort using templates

    Aim:

    To write a C++ program to do quick sort using templages.

    Algorithm:

    1. Get the array length.

    2. Get the array elements.

    3. If the l ist i s of length 0 or 1, then it is already s orted. Otherwise:4. Choose a key element in the list which is called a pivot.

    5. Reorder the li st with the rule that all elements which are less than the pivot come before

    the pivot and so that all elements greater than the pivot come after it.

    6. After the parti tioning, the pivot is in its final position.

    7. Recur sively reorder two sub-lists: the sub-list of lesser elements and the sub-list of greater

    elements.

    8. Display the sorted list.

    Program:

    #include

    #include

    template

    class quick

    { w a[50];

    int n;

    public:

    void get();

    void sort(int,int);

    int partition(int,int);

    void put();

    };

    template

    void quick ::get()

    { int i;

    coutn;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    17/35

    void quick ::sort(int p,i nt q)

    {

    int j;

    if(p

  • 8/12/2019 NEW C++ Lab Manual

    18/35

    q2.put();

    getch();

    }

    (6c) Bubble Sort using Templates

    Aim:

    To write a C++ program to do Bubble sort using templages.

    Algorithm:

    1. Get the array length.

    2. Get the array elements.

    3. If the l ist i s of length 0 or 1, then it is already sorted. Otherwise:

    4. Compare the first element with other elements in that list.

    5. If it is bigger, swap them.

    6. Continue the step 4 and 5 until all the elements in the l ist is compared.

    7. Display the sorted list.

    Program:

    #include #include

    #include

    template

    class bubble

    { t a[25];

    public:

    void get(int);

    void sort(int);

    void display(int);

    };

    template

    void bubble ::get(int n)

    { int i;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    19/35

    {

    if(a[i]>a[j])

    {

    temp=a[i];

    a[i]=a[j];

    a[j]=temp;

    }

    }

    }

    }

    void main()

    {

    int n;

    bubble b1;

    bubble b2;

    clrscr();

    cout

  • 8/12/2019 NEW C++ Lab Manual

    20/35

    { t a[25];

    public:

    void get(int);

    void sort(int);

    void display(int);

    };

    template

    void insertion::get(int n)

    { int i;

    cout

  • 8/12/2019 NEW C++ Lab Manual

    21/35

    cin>>n;

    i2.get(n);

    i2.sort(n);

    i2.display(n);

    getch();

    }

  • 8/12/2019 NEW C++ Lab Manual

    22/35

    Stack and Queue Operations with Exception Handling Ex.No.7(7a) Stack class with Exception Handling

    Aim:

    To write a C++ program to create a stack class and to do stack operations with

    exception handling.

    Algorithm

    1. Define the stack cl ass.

    . Define dummy class for FULL and EMPTY

    . Get the capacity of stack from the user.

    . Get the choice of operation to do in stack

    . If push is selected

    1. Check for stack overflow

    2. If not push the item and increment top

    3. If overflow throw FULL object

    . If pop is selected

    1. Check for stack underflow

    2. If not pop an item from the stack

    3. If underflow throw EMPTY object

    . If stack show is selected display the stack.

    . Continue until user select Exit option

    Program

    #include

    #include

    #include

    using namespace std;

    class stack

    {

    private:

    int *s;int max;

    int top;

    public:

    class FULL{}; //for exception handling

    class EMPTY{}; //for exception handling

    stack(int);

    void push(int);

    int pop(void);

    void display(void);

    };

    stack::stack(int m)

    {

    s=new int[m];

    top=-1;

    max=m;

    }

    void stack::push(int item)

    {

    if(top

  • 8/12/2019 NEW C++ Lab Manual

    23/35

    else

    throw FULL(); //FULL object is thrown

    }

    int stack::pop(void)

    {

    if(top>=0)

    return s[top--];

    else

    throw EMPTY(); //EMPTY object is thrown

    }

    void stack::display(void)

    {

    if(top>=0)

    for(int i=top; i>=0;i --)

    cout

  • 8/12/2019 NEW C++ Lab Manual

    24/35

    {

    cout

  • 8/12/2019 NEW C++ Lab Manual

    25/35

    public:

    class FULL{}; //for exception handling

    class EMPTY{}; //for exception handli ng

    queue(int);

    void enqueue(i nt);

    int dequeue(void);

    void display(void);

    };

    queue::queue(int m)

    {

    q=new int[m];

    rear=0;

    front=0;

    cnt=0;

    max=m;

    }

    void queue::enqueue(int item)

    {

    if(cnt0)

    {

    cnt--;

    rear = rear %max;

    return q[rear++];

    }

    else

    throw EMPTY(); //EMPTY object is thrown

    }

    void queue::di splay(void)

    {

    if(cnt>0)

    for(int i=0, j=front; i

  • 8/12/2019 NEW C++ Lab Manual

    26/35

    cin>>size;

    queue q(size);

    cout

  • 8/12/2019 NEW C++ Lab Manual

    27/35

    Operations on Complex Number using FilesEx.No. 8

    Aim:

    To write a C++ program that randomly generates complex numbers and write them two

    per line in a fi le along with an operator (+, -, *, or /) in the format (a + ib). To read one line at a

    time from this file, perform the corresponding operation on the two complex numbers read, and

    write the result to another file (one per line).

    Algorithm:

    1. Define complex class

    2. Generate random numbers

    3. Store the random numbers as complex number

    4. Create an object to work with file

    5. Open a file to write.

    6. Write the complex numbers with the operator in a file.

    7. Close the file.

    8. Create an anther object to read the file.

    9. Read the content and do the operation

    10. Store the resultant matrix in another file.

    Program:

    #include

    #include

    #include

    #include

    #include

    class complex

    { public:

    int real;

    int imag;

    complex(int r,int i)

    { real = r ;imag = i;

    }

    complex()

    { real=imag=0; }

    void display(void);

    };

    void complex::display(void)

    { cout

  • 8/12/2019 NEW C++ Lab Manual

    28/35

    ocom.close();

    ifstream icom("Complex.txt");

    char no,t, ch,op;

    icom>>no;

    icom>>real;

    icom>>ch;

    icom>>no;

    icom>>imag;

    imag=(ch=='+')?imag:-imag;

    icom>>no;

    icom>>op;

    complex a(real ,imag);

    icom>>no;

    icom>>real;

    icom>>ch;

    icom>>no;

    icom>>imag;

    imag=(ch=='+')?imag:-imag;

    complex b(real ,imag); //complex b is created

    complex c;switch(op)

    { case '+':

    c.real=a.real+b.real;

    c.imag=a.imag+b.imag;

    break;

    case '-':

    c.real=a.real-b.real;

    c.imag=a.imag-b.imag;

    break;

    case '*':

    c.real = (a.real*b.real)-(a.imag*b.i mag);

    c.imag = (a.real*b.imag)+(a.imag*b.real);

    break;

    case '/':

    float qt;

    qt = b.real*b.real+b.imag*b.imag;

    c.real = (a.real*b.real+a.imag*b.imag)/qt;

    c.imag = (a.imag*b.real-a.real*b.imag)/qt;

    break;

    default:

    cout

  • 8/12/2019 NEW C++ Lab Manual

    29/35

  • 8/12/2019 NEW C++ Lab Manual

    30/35

    Demonstration of Dynamic Polymorphism and RTTI on ShapeEx.No. 9

    Hierarchy

    Aim:

    Write a program to design a simple test application to demonstrate dynamic

    polymorphism and RTTI.

    Algorithm:

    1. Create square class with display() and area() function

    2. Create rectangle class with display() and area() function

    3. Create circle class with display() and area() function

    4. Create triangle cl ass with display() and area() function

    5. Create a template for polygon cl ass with the ability to handle vari ous shape

    classes.

    6. Use typeid to identify the class name from the given object.

    7. Invoke the area and display functions from the polygon class using RTTI

    8. Define the main() function to create the shape objects and polygon object

    9. Invoke the displayarea() function of polygon class which in turn invoke the

    corresponding objects method.

    Program:

    #include

    using namespace std;

    class square

    {

    private:

    float side;

    public:

    square(float x)

    {

    side = x;

    }square () {}

    void display()

    {

    cout

  • 8/12/2019 NEW C++ Lab Manual

    31/35

    bre=y;

    }

    void display()

    {

    cout

  • 8/12/2019 NEW C++ Lab Manual

    32/35

    class polygon

    {

    private:

    T shape;

    public:

    polygon(T x)

    {

    shape = x;

    }

    void displayarea()

    {

    char type[20];

    strcpy(type, typeid(shape).name());

    cout

  • 8/12/2019 NEW C++ Lab Manual

    33/35

    Finding Minimum Cost Spanning TreeEx.No. 10

    Aim:

    To define point class (node) and an arc class. To define a Graph class which represents

    graph as a collection of Point objects and Arc objects. And to write a method to find a minimum

    cost spanning tree in a graph.

    Algorithm:

    1. Define node class2. Define arc class3. Define graph class with a collecti on of node and arc class objects.4. Get the no. of nodes5. Get the edges with weight from the user.6. Generate adjacency matrix7. Appl y prims algori thm to find minimum cost spanning tree8. Display the minimum cost spanning tree

    Program:

    #include

    using namespace std;

    #define MAX 50

    #define TRUE 1

    #define FALSE 0

    #define MAXINT 250

    class node

    {

    public:

    int no;

    node() {}

    node(int a)

    {no=a;

    }

    };

    class arc

    {

    public:

    int adj;

    int weight;

    arc(){}

    arc(int a)

    {

    adj = a;

    }

    };

    class graph

    {

    public:

    node nodes[MAX];

    arc arcs[MAX][MAX];

  • 8/12/2019 NEW C++ Lab Manual

    34/35

    graph(int n)

    {

    for(int i=1;i

  • 8/12/2019 NEW C++ Lab Manual

    35/35

    for(j=2;j