microsoft word - c++ assignment

Upload: sutirtha-bhaumik

Post on 04-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Microsoft Word - c++ Assignment

    1/77

    1

    OVERVIEW OF OOP

    INTRODUCTIONEversincetheinventionofcomputer,manyprogrammingapproacheshavebeentried.

    Twomostwidelyusedtechniqueistheproceduralprogrammingproceduralprogrammingproceduralprogrammingproceduralprogramming&modularprogramming.modularprogramming.modularprogramming.modularprogramming. Proceduralprogrammingsplitstheentireproblemintoacollectionoftaskstobedone&representseachofthemintheformoffunctionsorprocedures.

    Objectorientedprogrammingontheotherhandconsiderthedistinctentitiesintheworld&representsthemintheformofobjects.

    Procedureorientedprogrammingbasicallyconsistsofwritingalistofinstructionforthe

    computertofollow,&organizingthemintogroupscalledthefunctions.Howeverthisapproachdoesnotconcentrateonthedatawhichactuallyreflectthechangemadebythefunctions.Generallythefunctionsshareglobaldatathatmovearoundfreelyinamulti-functionprogrammingenvironment.Themainfeaturesofprocedureorienteddesignare:

    Emphasisonfunctionsratherthandata. Largeproblemisdividedintoasetofsmalltasks&eachofthemisimplementedthrough

    appropriatefunction. Mostofthefunctionsshareglobaldata. Datamovesfreelyaboutthesystem&ishencepronetoaccidentalorun-authorized

    access&changes. Thisisatopdownprogrammingapproach.

    Objectorientedprogrammingtreatsdataascriticalelementoftheprograms.Thedataare

    tiedtogetherwithfunctionsthataffectthem.Theentireprogramisdividedintoasetofelementscalledobjectsthatrepresentstherealworldentities.

    Thebasicfeaturesofobjectorientedprogrammingare:

    Emphasisondataratherthanfunctions. Programsaredividedintowhatareknownasobjects. Thedifferentobjectsarerepresentedbydata&functionstiedtogetherasunit. Objectscommunicatethroughmessagepassingtechniques. Programmingapproachisbottom-upprogramming.

  • 7/30/2019 Microsoft Word - c++ Assignment

    2/77

    2

    BASIC CONCEPTS IN OOP

    TheconceptsextensivelyinOOPare;

    Objects

    Objectsarerepresentstherealworldentities.Theymaybedefiningaperson,anaccountetc.

    Classes

    Theobjectsdata&codethatmanipulatethedata.Theentiresetofdata&codeofanobjectcanbemadeauserdefineddatatypewiththehelpofaclass.

    Dataabstraction&encapsulation

    Bindingthedataalongwiththecodesinasingleunitisknownasdataencapsulation.Thedataisnotaccessiblebyfunctionsthatarenotapartoftheobject.Abstractionistheact ofrepresentingessentialfeatureswithout

    definingindetailstheimplementationdetails.Classesusetheconceptofabstraction&arealsoreferredtoasAbstractDataType.

    Inheritance

    Inheritanceistheconceptwherethepropertiesofoneobjectareinheritedbyotherones.Therebydevelopingafamilyofclasses.Thisprovidesscopeforcodereusability.

    Polymorphism

    Polymorphismistheconceptwhereasinglenamecanpossessmultipleforms.Inotherwordspolymorphismisthetechniquewheretheobjectsbehavedifferentlytothesamemessage.

    Itisoftwotypes:1. Compiletimepolymorphism.2. Runtimepolymorphism.

    Dynamicbinding

    Bindingreferstolinkingthecodeswithaprocedurecall.Dynamicbindingmeansthatthecodetobeexecutedcorrespondingtofunctioncallisnotknownuntilruntime.

  • 7/30/2019 Microsoft Word - c++ Assignment

    3/77

  • 7/30/2019 Microsoft Word - c++ Assignment

    4/77

    4

    ASSIGNMENT NO:1 DATE:01/09/07

    PROBLEM STATEMENT:

    Write a program to print the PASCALs Triangle.

    THEORY:

    The following formula can be used to implement PASCALs Triangle:-

    (x+y)n =nc0xny0 + nc1x

    n-1y1 + nc2xn-2y2 + + ncnx

    0ynThe coefficient of every term (i.e.

    nc0,

    nc1,

    ncn) is obtained from the different rows. Value of n changes with

    rows. For the 1st

    row n=1 and for last row n = no. of rows to be print. But here we use a simple rule usingarray to generate the Pascal triangle, such that any element

    a[i , j] = a[i-1,j]+a[i-1,j-1],except the first & second row.

    ALGORITHM:

    a is the array and n is the number of rows, such that n number of layers have to generate the elements ofPascal triangle.

    Pascal(a,n){for(i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    5/77

    5

    PROGRAM CODE:

    /* This program displays a Pascals triangle by reading no. of rows. This program uses basic rules of C++without intervention of class*/

    //------------------------------------------------------------#include

    #include//------------------------------------------------------------

    main(){ //program starts

    int row,i,j,a[10][10];clrscr();coutrow;

    a[0][0]=1;a[1][0]=1;a[1][1]=1;

    for(i=2;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    6/77

    6

    OUTPUT

    Enter the no.of rows:5

    The pascal's triangle looks like:

    1

    1 1

    1 2 1

    1 3 3 1

    press ANY KEY to skip

    DISCUSSON:

    Complexity of this program is O(n2).

    Instead of using the above simple method using array, we can use the formula of (x + y)n

    togenerate the Pascal triangle, by using this formula some memory space will be saved.

  • 7/30/2019 Microsoft Word - c++ Assignment

    7/77

    7

    ASSIGNMENT NO:2 DATE:13/09/07

    PROBLEM STATEMENT:

    This program finds the greatest common divisor (GCD) of two numbers given as input.

    THEORY:

    The GCD of two numbers is the greatest number by which both the dividend & the divisor can bedivided.

    ALGORITHM:

    main ( ){

    Read x, y;if( x > y ){temp = x ;x = y ;y = temp ;}

    } //end main

    GCD (int m , int n){

    If ( n % m = = 0 )return ( m ) ;

    elseGCD ( n % m , m ) ;

    } //end GCD

  • 7/30/2019 Microsoft Word - c++ Assignment

    8/77

    8

    PROGRAM CODE:

    /* this program calculates the GCD of two numbers using rules of C++. The GCD is calculatedusing recursive function. Here we use a class that has two integer data members & memberfunctions to manipulate them.*///-------------------------------------------------------------------#include#include//-------------------------------------------------------------------

    /* class definition */class GCD{

    int x,y;public:void setval();int gcd();

    };

    //-------------------------------------------------------------------

    /* memeber function definition */void GCD::setval()

    {int temp;cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    9/77

    9

    OUTPUT:

    Enter the numbers

    number 1:39

    number 2:6

    The GCD is:3

    DISSCUSSION:

    This program is capable of finding the GCD of any two numbers taken as input. The algorithm is small & easy enough to be implemented as a program.

  • 7/30/2019 Microsoft Word - c++ Assignment

    10/77

    10

    ASSIGNMENT NO:3 DATE:24/09/07

    PROBLEM STATEMENT:

    This program implements linear searching using FRIEND functions & INLINE functions.

    THEORY:

    The inline directive can be included before a function declaration to specify that the function mustbe compiled as code in the same point where it is called. This is equivalent to declare a macro, and itsadvantage is only appreciated in very short functions, in which the resulting code from compiling the programmay be faster if the overhead of calling a function (stacking of arguments) is avoided.The format for its declaration is

    inline type name ( arguments ... ) { instructions ...}and the call is just like the call to any other function. It is not necessary to include the inline keyword beforeeach call, only in the declaration.

    In the previous section we have seen that there were three levels of internal protection for the differentmembers of a class: public, protected and private. In the case of membersprotectedandprivate, thesecould not be accessed from outside the same class at which they are declared. Nevertheless, this rule canbe transgressed with the use offriend keyword in a class, by means of which we can allow an external

    function to gain access to the protected and private members of a class.In order to allow an external function to have access to the private and protected members of a class wehave to declare the prototype of the external function that will gain access preceded by the keyword friendwithin the class declaration that shares its members

    ALGORITHM:

    The algorithm for searching an element in the array is given below.

    /* a[] is the array which contains a list of numbers. The numbers are essentially integers.*/

    for(int i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    11/77

    11

    inline void array::read(){coutsize;p = new int[size]; //allocate memory dynamically

    }

    void array::create()

    {cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    12/77

    12

    OUTPUT:

    Enter the size of the array: 5Enter the array elements

    element1:12element2:13element3:24

    element4:35element5:46

    The array is: 12 13 24 35 46Enter the item to be searched: 24Item found at location: 3

    DISCUSSION:

    The complexity of BINARY SEARCH is measured by the number f(n) of comparison where there ndata elements & is given by O(n)

    A solution to this complexity problem is to use Binary search technique having a complexity

    O(log2(n))

  • 7/30/2019 Microsoft Word - c++ Assignment

    13/77

    13

    ASSIGNMENT NO:4 DATE:01/11/07

    PROBLEM STATEMENT:

    Write a program to find sum & difference of two complex numbers using OPERATOROVERLOADING implemented by both MEMBER FUNCTION & FRIEND FUNCTION.

    THEORY:

    C++ incorporates the option to use language standard operators between classes in addition tobetween fundamental types. For example:

    int a, b, c;a = b + c;

    is perfectly valid, since the different variables of the addition are all fundamental types.Nevertheless, is not so obvious that we can perform the following operation (in fact it is incorrect):

    struct { char product [50]; float price; } a, b, c;a = b + c;

    The assignation of a class (or struct) to other one of the same type is allowed (default copy constructor).What would produce an error would be the addition operation that in principle is not valid between non-fundamental types. But thanks to the C++ ability to overload operators, we can get to do that objects derived

    from composed types as the previous one can accept operators which would not be accepted otherwise oreven modify the effect of operators which they already admit.

    To overload an operator we only need to write a class member function whose name is operatorfollowed by the operator sign that we want to overload, following this prototype:

    type operatorsign (parameters);

    ALGORITHM:

    The algorithm for addition & subtraction of two complex numbers are given below;

    complex complex::operator+(complex &obj){complex temp;temp.real= this->real+obj.real;temp.imag= this->imag+obj.imag;return(temp);}

    complex operator-(complex &ob1, complex &ob2){complex temp;temp.real= ob1.real - ob2.real;temp.imag= ob1.imag - ob2.imag;return(temp);}

  • 7/30/2019 Microsoft Word - c++ Assignment

    14/77

    14

    PROGRAM CODE:

    /* The program shows operation on complex numbers using operator overloading through friend functions &member functions *///---------------------------------------------------------------------------------------------------------------------#include#include//---------------------------------------------------------------------------------------------------------------------

    /* class declaration */class complex{

    int real,imag;public:complex(){}complex(int a, int b){real=a;imag=b;

    }complex operator+(complex &);friend complex operator-(complex &, complex &);void display(){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    15/77

    15

    coutreal;coutimag;complex ob2(real, imag);cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    16/77

    16

    ASSIGNMENT NO:5 DATE:10/11/07

    PROBLEM STATEMENT:

    To develop a program where derived class constructor passes arguments to the base classconstructor & initializes the data members.

    THEORY:

    An important feature of classes is the inheritance. This allows us to create an object derived fromanother one, so that it may include some of the other's members plus its own ones. For example, we aregoing to suppose that we want to declare a series of classes that describe polygons like ourCRectangle, orlike CTriangle. Both have certain common features, like for example,both can be described by means of only two sides: height and base.

    Classes derived from others inherit all the visible members of the base class. That means thatif a base class includes a memberA and we derive it to another class with another member called B, thederived class will contain both A and B.In order to derive a class from another, we must use the operator: (colon) in the declaration of the derivedclass in the following way:

    class derived_class_name: public base_class_name;

    where derived_class_nameis the name of the derivedclass and base_class_nameis the name of the classon which is based. public may be replaced by any of the other access specifiers protected orprivate, anddescribes the access for the inherited members.

    ALGORITHM:

    Consider the classes ROLL, CURRICULAR, MARKS, RESULT. Here the class ROLL is inherited by bothCURRICULAR & MARKS. The last class RESULT inherits both the classes CURRICULAR & MARKS. Anobject of the class RESULT is crated & its constructor passes parameters to the constructor of the classesCURRICULAR & MARKS. Finally the constructor of the class MARKS passes parameters to the base class

    ROLL. As shown below,

    class ROLL{

    ROLL(int r){

    --------

    }}

    class MARKS:public ROLL{

    MARKS(int n,int m,int r):ROLL(r){----------------

    }}

  • 7/30/2019 Microsoft Word - c++ Assignment

    17/77

    17

    class CURRICULAR:public ROLL{

    CURRICULAR(int n){--------------------

    }

    }class RESULT:public CURRICULAR,public MARKS{RESULT(int roll,int marks1,int marks2,intsport_mark): MARKS(marks1,marks2,roll),CURRICULAR(sport_mark)

    {--------------------

    }

    }

    PROGRAM CODE:

    /*this program illustrates the use of inheritance to calculate result*///-----------------------------------------------------------------------------------------------------------------------#include#include#include#include//-----------------------------------------------------------------------------------------------------------------------

    /* class declaration */class ROLL{public:int roll;ROLL(){}ROLL(int n){

    roll=n;}void display(){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    18/77

    18

    class CURRICULAR:public ROLL{public:int sport_mark;CURRICULAR(){}void display(){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    19/77

    19

    OUTPUT:

    enter roll:15enter marks1:45enter marks2:35enter sport marks:43

    CURRICULAR constructorMARKS constructorRESULT constructorROLL constructor

    roll no.15marks1:45marks2:35sport marks:43total is:123

    continue(y/n): n

    DISCUSSION:

    As long as the base class does not include any parameterized constructor, the derived classconstructor does not require any constructor description. But if the base class constructor isparameterized then we do need a derived class constructor that would pass the arguments to thebase class.

    In case of multiple inheritance, the constructors are invoked in order in which they appear in thederived class declaration.

    Incase there is virtual base class, then the corresponding constructor is invoked before any non-virtual base class constructor.

  • 7/30/2019 Microsoft Word - c++ Assignment

    20/77

    20

    ASSIGNMENT NO:6 DATE:16/11/07

    PROBLEM STATEMENT:

    To write a program that implements student database using structure & class collectively.

    THEORY:

    Structure is a user defined data type. It is used to hold the fields roll, name, marks1, marks2& marks3 forming a student record. An array of the structure type is created & is allocated memorydynamically. The searching technique is the linear search one & the search key is the roll number.

    A class is a logical method to organize data and functions in a same structure. They are declared usingkeyword class, whose functionality is similar to the one of the C keyword struct, but with the possibility ofincluding functions as members, moreover than only data.Its form is:

    class class_name{permission_label_1: member1;permission_label_2: member2;...

    }object_name;

    where class_nameis a name for the class (user defined type) and the optional field object_nameis one, or

    several, valid object identifiers. The body of the declaration can contain members, that can be either data orfunction declarations, and optionallypermission labels, that can be any of these three keywords: private:,public: orprotected:. They make reference to the permission which the following members acquire:

    private members of a class are accessible only from other members of its same class or from its"friend" classes.

    protected members are accessible, in addition to from members of the same class and friendclasses, also from members of its derivedclasses.

    Finally, public members are accessible from anywhere where the class is visible.

    ALGORITHM:

    The algorithm for searching an entry in the file is

    void student::search()

    {int roll;coutroll;for(int i=0;iroll==roll ){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    21/77

    21

    PROGRAM CODE:

    /* The program shows operation on student database *///-------------------------------------------------------------------------#include#include//-------------------------------------------------------------------------class student

    {struct record{int roll;char *name;int m1,m2,m3; //marks in 3 subjects

    } *ptr;int size;public:student(){coutsize;ptr = new record[size];

    }

    void create();void search();void display();

    };//------------------------------------------------------------------

    /* Member function declaration */

    void student::create(){for(int i=0;iroll;cout(ptr+i)->name;cout(ptr+i)->m1;cout(ptr+i)->m2;cout(ptr+i)->m3;

    }}

    void student::search(){int roll;coutroll;

    for(int i=0;iroll==roll ){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    22/77

    22

    void student::display(){for(int i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    23/77

    23

    OUTPUT:

    Enter the no.of students:3

    Enter roll:100Enter name:rupEnter marks1:45Enter marks2:35

    Enter marks3:25

    Enter roll:101Enter name:parEnter marks1:25Enter marks2:35Enter marks3:45

    Enter roll:102Enter name:shiEnter marks1:23Enter marks2:24Enter marks3:34

    What do you wish?

    1.search2.exit?1

    Enter the roll of the student:100

    Name:rupRoll:100Marks1:45Marks2:35Marks3:25

    What do you wish?

    1.search2.exit?1

    Enter the roll of the student:103

    Roll does not exist

    DISCUSSION:

    Complexity of linear search technique is O(n);

    Could have also been implemented using 2-D character array. Here all the entries namely roll,name, marks etc would have been treated as characters.

    Memory has been allocated dynamically using new operator preventing memory wastage.

  • 7/30/2019 Microsoft Word - c++ Assignment

    24/77

    24

    Data structureprograms

  • 7/30/2019 Microsoft Word - c++ Assignment

    25/77

    25

    ASSIGNMENT NO:7 DATE:09/01/08

    PROBLEM STATEMENT:

    Suppose DATA is an array of sorted elements .Then there will be an efficient searching algorithmcalled BINARY SEARCH which finds the location of element an in the list. We have to implement BINARYSEARCH ALGORITHM to find the location of an elements in a sorted list.

    THEORY:

    The most efficient method of searching a sequential table without the use of indices or tables is thebinary search. This searching technique is a divide-and-conquer method applicable to sort data items.

    ALGORITHM:

    Binary search{

    scanf("%d",&n); //Read the total number of elements in databeg=0;end=n-1; //initialize beg & endmid=(beg+end)/2;printf("\n\nenter the elements\n");

    for(i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    26/77

    26

    PROGRAM CODE:

    /* This program illustrates the use of binary search *///---------------------------------------------------------------------#include#include//---------------------------------------------------------------------

    /* class definition */class bin_search{int beg,end,mid,n,item,data[15];public:void create();void search();void display();};

    //---------------------------------------------------------------------

    /* function definition */

    void bin_search::create(){

    coutn; //Read the total number of elements in data

    beg=0;end=n-1;mid=(beg+end)/2;

    cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    27/77

    27

    void bin_search::search(){coutitem;

    while((beg

  • 7/30/2019 Microsoft Word - c++ Assignment

    28/77

    28

    OUTPUT:

    enter the size of the array:

    enter the elements

    element 1:11

    element 2:33element 3:55element 4:77element 5:99

    enter the element you want to search....

    successful search at position=3

    enter the size of the array:

    enter the elements

    element 1:22element 2:44element 3:66

    element 4:88element 5:100

    enter the element you want to search....

    unsuccessful search....

    DISCUSSION:

    Binary search algorithm requires two conditions:

    1) The list must be sorted.2) One must have direct access to the middle element in the list.

    The complexity of BINARY SEARCH is measured by the number f(n) of comparison wherethere n data elements.

    Average case complexity = log2(n+1) = o(log2n)

    Worst case complexity = log2(n+1) = o(log2n)

    This method demands that the list be SORTED which is normally expensive when there are many insertion& deletions. In such cases linked list can be used.

  • 7/30/2019 Microsoft Word - c++ Assignment

    29/77

    29

    ASSIGNMENT NO:8 DATE:14/01/08

    PROGRAM STATEMENT:

    The program below sorts a list of elements provided by user using INSERTION sorting technique.

    THEORY:

    Sorting is a technique to arrange a set of records in a particular order. An insertion sort is a techniquethat sorts a set of records by inserting records into an existing sorted file.

    ALGORITHM:

    void insertsort(int b[],int n){ //start algorithm/* b is list of records which are n in number */int i,j,y;for(i=1;i=0&&y

  • 7/30/2019 Microsoft Word - c++ Assignment

    30/77

    30

    PROGRAM CODE:

    /* This program sorts a list of elements using insertion sort technique. A constructor function isused to read the array size*/

    //----------------------------------------------------------#include#include

    //----------------------------------------------------------

    /*class definition*/class in_sort{

    int array[15],size;public:in_sort();void create();void sort();void display();

    };//----------------------------------------------------------

    /*function definitions*/

    in_sort::in_sort() //constructor

    {coutsize;create();

    }

    void in_sort::create(){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    31/77

    31

    void in_sort::sort(){int i,j,y;

    for(i=1;i=0 && y

  • 7/30/2019 Microsoft Word - c++ Assignment

    32/77

    32

    OUTPUT:

    enter the number of elements:6

    enter the elements

    element 1:11element 2:56

    element 3:34element 4:22element 5:78element 6:21

    the sorted list of elements is: 11 21 22 34 56 78

    DISCUSSION:

    > If we use linked list instead of array then the program can store any number of elements. But aswe have used array of size 30, the program can not store more than 30 elements.

    > .Complexity analysis:

    For worst case: [when the array is reversibly sorted]

    No. of comparisons: f(n) = 1+2++(n-1)=O(n2)

    For average case:

    No. of comparisons: f(n) = + 2/2 + ..+(n-1)/2=O(n2)

  • 7/30/2019 Microsoft Word - c++ Assignment

    33/77

    33

    ASSIGNMENT NO:9 DATE:19/01/08

    PROGRAM CODE:

    This program implements bubble sort technique using TEMPLATE class.

    THEORY:

    We also have the possibility to write class templates, so that a class can have members based ongeneric types that do not need to be defined at the moment of creating the class or whose members usethese generic types. For example:

    template class pair{

    T values [2];public:pair (T first, T second)

    {values[0]=first;values[1]=second;

    }

    };The class that we have just defined serves to store two elements of any valid type. For example, if wewanted to declare an object of this class to store two integer values of type int with the values 115 and 36we would write:pair myobject (115, 36);

    This same class would serve also to create an object to store any other type:pair myfloats (3.0, 2.18);

    The only member function has been defined inline within the class declaration

    ALGORITHM:

    The algorithm for sorting the array using BUBBLE sorting technique is given below.

    templatevoid b_sort::sort(){GP temp;for(int i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    34/77

    34

    PROGRAM CODE:

    /* This program sorts a list of elements using bubble sort. A genericclass is developed by using template */

    //----------------------------------------------------------------#include#include

    //----------------------------------------------------------------template class b_sort{

    int size;GP *array;public:b_sort(){coutsize;array = new GP[size];

    }void create_array();void display();void sort();

    };//----------------------------------------------------------------

    templatevoid b_sort::create_array(){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    35/77

    35

    templatevoid b_sort::sort(){GP temp;for(int i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    36/77

    36

    OUTPUT:

    Integer object createdEnter the array size: 4Enter the elements:

    element1:1element2:3

    element3:2element4:4

    The array is: 1 3 2 4The sorted array is: 1 2 3 4

    Float object createdEnter the array size:4Enter the elements:

    element1:11.1element2:22.1element3:33.4element4:1.7

    The array is: 11.1 22.1 33.400002 1.7The sorted array is: 1.7 11.1 22.1 33.400002

    Character object createdEnter the array size:4Enter the elements:

    element1:delement2:aelement3:celement4:b

    The array is: d a c bThe sorted array is: a b c d

    DISCUSSION:

    Templates allow to create generic functions that admit any data type as parameters and return valuewithout having to overload the function with all the possible data types

    Here we sort three types of arrays, one for integer type, one for float type,& one for character type.

    The complexity of this sorting technique is O(n2)

  • 7/30/2019 Microsoft Word - c++ Assignment

    37/77

    37

    ASSIGNMENT NO:10 DATE:22/01/08

    PROGRAM STATEMENT:

    This program shows the various operations that can be done on a linear linked list. The output ofthe various operations is stored in a file.

    THEORY:

    To prevent wastage of memory linked list are preferred over contiguous list. The structure of thenode contains a data field of integer type and a next field of the structure type. Dynamic memory allocation isused to create a node. The various operations done are:

    1. Creation of list.2. Insertion of a node.3. Deletions of nodes.4. Searching of a node.5. Counting the number of nodes.

    ALGORITHM:

    /* append */

    void link_list::append(){struct link *p,*q;int item;

    coutitem;

    p = new link;p->data=item;p->next=0;

    if(header->next==0){header->next=p;return;

    }else{q=header->next;while(q->next!=0)q=q->next;q->next=p;

    }} /* end append */

  • 7/30/2019 Microsoft Word - c++ Assignment

    38/77

    38

    /* display */void link_list::display(){struct link *p;p=header->next;coutnext=r;} /* end reverse */

    /* Insert at any position */

    void link_list::InsertAtAnyPosition(){struct link *p,*temp;int item,previousitem;

    coutitem;coutpreviousitem;p=header->next;

    while(p->data!=previousitem){if(p->next==0){coutdata=item;

    temp->next=p->next;p->next=temp;}

    /* end insert at any position */

  • 7/30/2019 Microsoft Word - c++ Assignment

    39/77

    39

    /*Delete from any position */

    void link_list::DeleteAtAnyPosition(){int item,previousitem;struct link *p, *q;coutitem;

    p=header->next;q=header->next;if(item==p->data){header->next=p->next;

    }else{while(p->data!=item){q=p;p=p->next;

    }q->next=p->next;

    }}

    /* end delete at any position */

  • 7/30/2019 Microsoft Word - c++ Assignment

    40/77

    40

    PROGRAM CODE:

    /* This program shows few operation on linked list */]

    //--------------------------------------------------------------------------------------------------------------------------------

    #include#include

    //--------------------------------------------------------------------------------------------------------------------------------

    /* class definition */

    class link_list{

    struct link{int data;struct link *next;

    };link * header;

    public:link_list(){header->data=0;header->next=0;cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    41/77

    41

    /* append */

    void link_list::append(){struct link *p,*q;int item;

    coutitem;

    p = new link;p->data=item;p->next=0;

    if(header->next==0){header->next=p;return;

    }else{q=header->next;while(q->next!=0)q=q->next;

    q->next=p;}

    } /* end append */

    /* display */void link_list::display(){struct link *p;p=header->next;coutnext=r;} /* end reverse */

  • 7/30/2019 Microsoft Word - c++ Assignment

    42/77

    42

    /* Insert at any position */

    void link_list::InsertAtAnyPosition(){struct link *p,*temp;int item,previousitem;

    coutitem;coutpreviousitem;p=header->next;

    while(p->data!=previousitem){if(p->next==0){coutdata=item;

    temp->next=p->next;p->next=temp;}

    /* end insert at any position */

    /*Delete from any position */

    void link_list::DeleteAtAnyPosition(){int item,previousitem;struct link *p, *q;coutitem;p=header->next;q=header->next;if(item==p->data){header->next=p->next;

    }else{while(p->data!=item){q=p;p=p->next;

    }q->next=p->next;

    }}

    /* end delete at any position */

  • 7/30/2019 Microsoft Word - c++ Assignment

    43/77

    43

    //----------------------------------------------------------------------------------------------------------------------------------------

    /* main funtion */

    main(){clrscr();link_list ob;

    int choice;while(1){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    44/77

    44

    OUTPUT:

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen

    7>Exit

    Enter your choice-1enter the node value:1

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-1enter the node value:3

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-2Header-->>(1)-->>(3)

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-3Enter the element:2

    Enter the preceding element:1

  • 7/30/2019 Microsoft Word - c++ Assignment

    45/77

    45

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen

    7>Exit

    Enter your choice-2Header-->>(1)-->>(2)-->>(3)What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-5

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-2Header-->>(3)-->>(2)-->>(1)

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-5

    What do you wish

    1>Append2>Display

    3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

  • 7/30/2019 Microsoft Word - c++ Assignment

    46/77

    46

    Enter your choice-2Header-->>(1)-->>(2)-->>(3)

    What do you wish

    1>Append2>Display

    3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-3Enter the element:4Enter the preceding element:3

    What do you wish

    1>Append2>Display

    3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-3Enter the element:5Enter the preceding element:4

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-2Header-->>(1)-->>(2)-->>(3)-->>(4)-->>(5)

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse

    6>Clear screen7>Exit

    Enter your choice-4Enter the element:2

  • 7/30/2019 Microsoft Word - c++ Assignment

    47/77

    47

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen

    7>Exit

    Enter your choice-2Header-->>(1)-->>(3)-->>(4)-->>(5)

    What do you wish

    1>Append2>Display3>Insert at any position4>Delete from any position5>Reverse6>Clear screen7>Exit

    Enter your choice-7

    DISCUSSION:

    Memory wastage is prevented using linked list rather the contiguous list. Dynamic memory allocation is used. The program is divided into various modules. The linked list is pointed by the pointer header.

  • 7/30/2019 Microsoft Word - c++ Assignment

    48/77

    48

    ASSIGNMENT NO.11 DATE:25/01/08

    PROBLEM STATEMENT:

    Write a program to perform operation on a STACK using TEMPLATES.

    THEORY:

    We also have the possibility to write class templates, so that a class can have members based ongeneric types that do not need to be defined at the moment of creating the class or whose members usethese generic types. For example:

    template class pair{

    T values [2];public:pair (T first, T second)

    {values[0]=first;values[1]=second;

    }

    };The class that we have just defined serves to store two elements of any valid type. For example, if wewanted to declare an object of this class to store two integer values of type int with the values 115 and 36we would write:pair myobject (115, 36);

    This same class would serve also to create an object to store any other type:pair myfloats (3.0, 2.18);

    The only member function has been defined inline within the class declaration

    ALGORITHM:

    Algorithm for push & pop are given below

    template void stack::push(){if(top==size){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    49/77

    49

    template GP stack::pop(){if(top==0){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    50/77

  • 7/30/2019 Microsoft Word - c++ Assignment

    51/77

    51

    if(choice==4){item_i=obi.pop();

    if(item_i != 0)cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    52/77

    52

    OUTPUT

    Initialize integer objectEnter size:3Top initialized

    Initialize float object

    Enter size:3Top initialized

    Initialize character objectEnter size:3Top initialized

    Object of integer, float, and character type created

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 1Enter item to be pushed (except 0): 1Item pushed

    The current stack is1

    Press ENTER to re-enter a choice

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 1Enter item to be pushed (except 0):2

    Item pushed

  • 7/30/2019 Microsoft Word - c++ Assignment

    53/77

    53

    The current stack is

    21

    Press ENTER to re-enter a choice

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice:1Enter item to be pushed (except 0):3

    Item pushedThe current stack is

    321

    Press ENTER to re-enter a choice

    What do you wish ?

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 2Enter item to be pushed (except 0):1.5

    Item pushedThe current stack is

    1.5

    Press ENTER to re-enter a choice

  • 7/30/2019 Microsoft Word - c++ Assignment

    54/77

    54

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char

    7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 2Enter item to be pushed (except 0):2.5

    Item pushed

    The current stack is

    2.51.5

    Press ENTER to re-enter a choice

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 2Enter item to be pushed (except 0): 3.5

    Item pushedThe current stack is

    3.52.51.5

    Press ENTER to re-enter a choice

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack

  • 7/30/2019 Microsoft Word - c++ Assignment

    55/77

    55

    9.display char stack10.Exit

    Enter your choice: 3Enter item to be pushed (except 0):a

    Item pushedThe current stack is

    a

    Press ENTER to re-enter a choice

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack

    9.display char stack10.ExitEnter your choice: 3Enter item to be pushed (except 0): b

    Item pushedThe current stack is

    ba

    Press ENTER to re-enter a choice

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 1Enter item to be pushed (except 0): 3

    Item pushed

    The current stack is

    cba

    Press ENTER to re-enter a choice

  • 7/30/2019 Microsoft Word - c++ Assignment

    56/77

    56

    What do you wish ?.

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char

    7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 4Item popped: 3

    What do you wish ?

    1.Push int2.Push float3.Push char4.Pop int5.Pop float

    6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 5Item popped: 3.5

    What do you wish ?

    1.Push int2.Push float3.Push char4.Pop int5.Pop float6.Pop char7.dispaly int stack8.display float stack9.display char stack10.Exit

    Enter your choice: 6Item popped: c

    DISCUSSION:

    Templates allow to create generic functions that admit any data type as parameters and return valuewithout having to overload the function with all the possible data types

    Here we have three types of STACK- one of integer type, one of float type,& one of character type.

  • 7/30/2019 Microsoft Word - c++ Assignment

    57/77

    57

    ASSIGNMENT NO:12 DATE:30/01/08

    PROGRAM STATEMENT:

    This program implements a binary search tree(BST) and performs inorder traversal andsearching operations on the tree.

    THEORY

    Suppose T is a binary tree .Then T is called a binary search tree if each node N has thefollowing properties.

    1) Every value in the left sub-tree of N has a value less than N .2) Every value in the right sub tree of N has a value greater than or equal to n.3) These properties are true for every node in tree.4) These properties guarantees that the inorder traversal of the tree gives a sorted list.

    ALGORITHM:

    The algorithm for creating, inoerder traversing & searching in a BST is given below.

    //create BSTvoid createBST(){struct tree *p,*q,*r;int i,num;

    root=NULL;printf("\n\nenter the no.of nodes:");scanf("%d",&num);for(i=0;idata);

    p->lchild=p->rchild=NULL;if(root==NULL){root=p;

    }else{q=root;r=NULL;while(q!=NULL){

    if(p->datadata){

    r=q;q=q->lchild;

    }else{r=q;q=q->rchild;

    }}

  • 7/30/2019 Microsoft Word - c++ Assignment

    58/77

    58

    if(p->datadata)r->lchild=p;

    elser->rchild=p;

    }} //end for

    } //end createBST

    //inorder

    void inorder(struct tree *root){if(root!=NULL){inorder(root->lchild);printf(" %d",root->data);inorder(root->rchild);

    }}

    //end inorder

    //search

    void search(struct tree *root){int item;

    printf("\n\nenter the value to be searched:");scanf("%d",&item);

    while(root!=NULL){if(item==root->data){

    printf("\n\nitem found at %u",root);return;

    }

    if(root->datarchild;elseroot=root->lchild;

    }printf("\n\nitem reffered not found");}

    //end search

  • 7/30/2019 Microsoft Word - c++ Assignment

    59/77

    59

    PROGRAM CODE:

    /* This program implements a binary search tree(BST) and performs inorder traversal and searchingoperations on the tree */

    //-------------------------------------------------------------------------#include#include

    //-------------------------------------------------------------------------/* class declaration */

    class BST{struct tree{int data;struct tree *rchild,*lchild; /*define node structure*/

    }*root;public:void inorder(struct tree *root);void createBST();tree* Return(); /*member function declarations /void search(struct tree *root);

    };//------------------------------------------------------------------------

    /* member function definition */

    tree* BST::Return(){return(root);

    }

    //create BSTvoid BST::createBST(){struct tree *p,*q,*r;int i,num;

    root=NULL;

    coutnum;for(i=0;i

  • 7/30/2019 Microsoft Word - c++ Assignment

    60/77

    60

    while(q!=NULL){

    if(p->datadata){

    r=q;q=q->lchild;

    }

    else{r=q;q=q->rchild;

    }}

    if(p->datadata)r->lchild=p;

    elser->rchild=p;

    }} //end for

    } //end createBST

    //inorder

    void BST::inorder(struct tree *root){if(root!=NULL){inorder(root->lchild);cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    61/77

    61

    //-----------------------------------------------------------------------

    int main(){int ch;BST ob;clrscr();

    while(1){coutch;

    if(ch==0)return(0);

    if(ch==1)ob.createBST();

    if(ch==2){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    62/77

    62

    what do you wish?

    0.exit1.createBST2.inorder3.seacrh?3

    enter the value to be searched:9

    item found at location:

    what do you wish

    0.exit1.createBST2.inorder3.seacrh?0

    DISCUSSION:

    Suppose we are searching for an item in a BST. The number of comparison is bounded by thedepth of the tree. This comes from the fact that we proceed down single path of the tree as wesearch the item. Accordingly the running time of the search will be proportional to the depth of thetree . thus the time complexity of BST searching algorithm is o(log2n).

    The inorder traversal gives a sorted list of elements.

  • 7/30/2019 Microsoft Word - c++ Assignment

    63/77

    63

    ASSIGNMENT NO:13 DATE:06/02/08

    PROGRAM STATEMENT:

    This program creates a QUEUE and performs the operations such as insertion & deletion on thequeue.

    THEORY:

    A queue is a linear list of elements in which all deletion operation can take place only at one endcalled the front & all insertion operation can take place through the other end called the rear.The information in a queue data structure or queue is processed in the same order as it was inserted i.e.,FIFO list.

    ALGORITHM:

    The algorithm for insertion & deletion in a queue are;

    void insert(){

    if(rear==size-1){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    64/77

    64

    PROGRAM CODE:

    /* This program creates a QUEUE and performs the operations such as insertion & deletion on the queue */

    //-------------------------------------------------------------------------#include#include

    //-------------------------------------------------------------------------

    /* class declaration */

    template class QUEUE{

    int size,front,rear;T *q;public:QUEUE(){coutsize;q = new T[size];front=-1;rear=-1;

    }void insert();void Delete();void display();};

    //-------------------------------------------------------------------------

    /* member function definition */

    template void QUEUE::insert(){T item;

    if(rear==size-1){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    65/77

    65

    template void QUEUE::Delete(){T item;

    if(front==-1){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    66/77

    66

    int main(){clrscr();

    cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    67/77

    67

    OUTPUT:

    Integer object createdEnter the size of the array:3Float object createdEnter the size of the array:3Character object createdEnter the size of the array:3

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?1

    Enter the element(except 0):1

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?1

    Enter the element(except 0):2

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)

    8.display(float)9.display(char)10.exit?1

    Enter the element(except 0):3

  • 7/30/2019 Microsoft Word - c++ Assignment

    68/77

    68

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)

    7.display(int)8.display(float)9.display(char)10.exit?3

    Enter the element(except 0):a

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)

    7.display(int)8.display(float)9.display(char)10.exit?3

    Enter the element(except 0):b

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?3

    Enter the element(except 0):c

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)

    5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?2

    Enter the element(except 0):1.5

  • 7/30/2019 Microsoft Word - c++ Assignment

    69/77

    69

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)

    7.display(int)8.display(float)9.display(char)10.exit?2

    Enter the element(except 0):2.5

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)

    5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?2

    Enter the element(except 0):3.5

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?1

    Overflow

    What do you wish?

    1.insert(int)

    2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?2

    Overflow

  • 7/30/2019 Microsoft Word - c++ Assignment

    70/77

    70

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)

    7.display(int)8.display(float)9.display(char)10.exit?3

    Overflow

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)

    6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?7

    The queue is: 1 2 3

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?8

    The queue is: 1.5 2.5 3.5What do you wish?

    1.insert(int)2.insert(float)3.insert(char)

    4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?9

    The queue is: a b c

  • 7/30/2019 Microsoft Word - c++ Assignment

    71/77

    71

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)

    7.display(int)8.display(float)9.display(char)10.exit?4

    Item deleted is:1

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)

    5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?5

    Item deleted is:1.5

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)7.display(int)8.display(float)9.display(char)10.exit?6

    Item deleted is: a

  • 7/30/2019 Microsoft Word - c++ Assignment

    72/77

    72

    What do you wish?

    1.insert(int)2.insert(float)3.insert(char)4.delete(int)5.delete(float)6.delete(char)

    7.display(int)8.display(float)9.display(char)10.exit?10

    DISCUSION:

    Memory allocation is done dynamically using new operator. A class template is created so as to support generic programming. The criteria for queue overflow is equaling the value of rear to the size of the queue. But

    actually there will be space in the queue after one or more deletions. But as the value ofrear is equal to the size of the queue, the algorithm will provide a overflow message.

    This problem can be recovered using a circular queue.

  • 7/30/2019 Microsoft Word - c++ Assignment

    73/77

    73

    ASSIGNMENT NO:14 DATE:12/02/08

    PROGRAM STATEMENT:

    To write a program that creates a binary tree, traverses it using inorder traversal & counts the no. ofleafs of the tree formed.

    THEORY:

    A binary tree is a finite set of elements which is either empty or divided into three disjoint subsets:one containing only one node called the root & the other two being the left & the right subtree which againbinary themselves.

    In-order traversal of a binary tree follows the below given pattern-

    1. left child2. root3. right child.

    ALGORITHM:

    The algorithms for creating the tree, traversing & node counting are given below;

    void tree::maketree(struct treenode *root){char c;cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    74/77

    74

    PROGRAM CODE:

    /* this program shows few operations on a binary tree *///-------------------------------------------------------------------#include#include//---------------------------------------------------------------------

    class tree{int count1;struct treenode{struct treenode *lchild;int data; //define node structurestruct treenode *rchild;

    } *root;public:void makeroot();void maketree(struct treenode *);treenode * makenode();void inorder(struct treenode *);int count(struct treenode *); /*member function declaration */treenode * Return();

    void clear(){count1=0;

    }

    };

    //---------------------------------------------------------------------

    /*member function definition *///makeroot

    void tree::makeroot(){coutlchild=p->rchild=0;count1=0;return(p);

    }

    //end makenode

  • 7/30/2019 Microsoft Word - c++ Assignment

    75/77

    75

    //make tree

    void tree::maketree(struct treenode *root){char c;cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    76/77

    76

    //----------------------------------------------------------------------

    main(){ //start main( )int ch;tree ob;clrscr();

    ob.makeroot();ob.maketree(ob.Return());

    while(1){coutch;

    if(ch==0)return(0);

    if(ch==1){cout

  • 7/30/2019 Microsoft Word - c++ Assignment

    77/77

    enter node key:5want to create left child of 5(y/n)?nwant to create right child of 5(y/n)?nwant to create right child of 6(y/n)?y

    enter node key:7want to create left child of 7(y/n)?n

    want to create right child of 7(y/n)?n

    what do you wish?

    0.exit1.count node2.traverse...2

    The inorder traversal is: 1 2 3 4 5 6 7

    what do you wish?

    0.exit1.count node

    2.traverse...1

    no.of nodes=7

    what do you wish?

    0.exit1.count node2.traverse...0

    DISCUSSION:

    The in-order traversal of the tree is done using recursive function. Counting the leaf is also done using recursive function. Functionalities can be extended as per wish.