creating a Set in C++ using classes (need help on an exercise)

Discussion in 'C++' started by Richard Gromstein, Mar 10, 2007.

  1. Hello, I have an exercise that I need to finish very soon and I really
    need help understanding what to do and how exactly to do it. I am
    working on reading the chapter right now and working on it myself, but
    I have a feeling I won't get far. Please help me complete the
    exercise. I am posting what needs to be done and will be updating
    with pieces of code that I come up with. Thank you all for your
    attention.


    -----------------------------------------
    // Exercise text

    Implement a Set class, where a set is an unordered collection of zero
    or more elements with no duplicates. For this exercise, the elements
    should be ints. The public interface consists of methods to

    - Create s Set.
    - Add a new element to a Set.
    - Remove an elemnt from a Set.
    - Enumerate the elements in the Set.
    - Compute the intersection of two Sets S1 and S2, that is, the set of
    elemtns that belong to both S1 and S2.
    - Compute the union of two Sets S1 and S2, that is, the set of
    elements that belong to S1 or S2 or both.
    - Compute the difference of two Sets S1 and S2, that is, the set of
    elements that belong to S1 but not to S2.


    ------------------------------------------
    // Instructor't notes

    implement with a linked list, using classes

    intent: to write classes correctly
    Set.h has class declarations (including inlines)
    Set.cpp has method definitions for the class
    main.cpp is the "driver" (i.e. uses the Set class)


    if l is a linked list, is x already on the list?
    bool search(x,l) /* search for x on list l*/
    if l is null return false
    if l->info == x return true
    return search(x, l->next)

    assume you store the items in order
    how to do union of two sets?

    create a copy constructor which copies the set -- if you dont, any
    copies will destroy the original
    when they are destroyed.

    of course, your destructor must delete any dynamically
    allocated storage

    destructor():
    if l == null return;
    destructor(l->next);
    delete(l);

    your destructor will call delete on each dynamically
    created element. when you creat a copy, if its a
    shallow copy, the originals dynamically allocated
    parts will be destroyed. (you implicity make copies
    when you do call by value, return by value,
    initialization copies (i.e. when the copy constructor
    is called) the copies are destroyed when they go out
    of scope).

    -------------------------------------------------------------------
     
    Richard Gromstein, Mar 10, 2007
    #1
    1. Advertising

  2. Richard Gromstein

    kwikius Guest

    On 10 Mar, 05:18, "Richard Gromstein" <> wrote:

    > I have a feeling I won't get far. Please help me complete the
    > exercise. I am posting what needs to be done and will be updating
    > with pieces of code that I come up with. Thank you all for your
    > attention.


    <...>

    Break it down. Your instructor told you where to start. Ignore the set
    part and complete the first part...

    > // Instructor't notes
    >
    > implement with a linked list, using classes.


    So try to make a linked list...
    If and when you get that to work then you will have partially
    completed the task (and so may get some marks) and also may be able to
    move on.

    regards
    Andy Little
     
    kwikius, Mar 10, 2007
    #2
    1. Advertising

  3. kwikius wrote:
    > On 10 Mar, 05:18, "Richard Gromstein" <> wrote:
    >
    >
    >>I have a feeling I won't get far. Please help me complete the
    >>exercise. I am posting what needs to be done and will be updating
    >>with pieces of code that I come up with. Thank you all for your
    >>attention.

    >
    >
    > <...>
    >
    > Break it down. Your instructor told you where to start. Ignore the set
    > part and complete the first part...
    >
    >
    >>// Instructor't notes
    >>
    >>implement with a linked list, using classes.

    >
    >
    > So try to make a linked list...
    > If and when you get that to work then you will have partially
    > completed the task (and so may get some marks) and also may be able to
    > move on.
    >
    > regards
    > Andy Little
    >
    >


    Good advice, to which I would add, don't underestimate the time it will
    take you to get a linked list working, if you have never done it before
    (unless of course you are a programming genius). Sounds like you've left
    it a bit late already. Don't be afraid to ask for an extension.

    john
     
    John Harrison, Mar 10, 2007
    #3
  4. Richard Gromstein

    red floyd Guest

    Richard Gromstein wrote:
    > Hello, I have an exercise that I need to finish very soon and I really
    > need help understanding what to do and how exactly to do it. I am
    > working on reading the chapter right now and working on it myself, but
    > I have a feeling I won't get far. Please help me complete the
    > exercise. I am posting what needs to be done and will be updating
    > with pieces of code that I come up with. Thank you all for your
    > attention.
    >

    Show us what you already have. We won't do your homework for you.

    > [problem statement redacted]
     
    red floyd, Mar 10, 2007
    #4
  5. Richard Gromstein

    Guest

    Working on a linked list creation. Digging through old C notes and
    reading online tutorials... I'll post what I have when I get
    something solid.
     
    , Mar 11, 2007
    #5
  6. Working on a linked list creation. Digging through old C notes and
    reading online tutorials... I'll post what I have when I get
    something solid.
     
    Richard Gromstein, Mar 11, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aries
    Replies:
    7
    Views:
    418
    Aries
    May 3, 2006
  2. =?Utf-8?B?TWFyayBQYXJ0ZXI=?=

    Need help using Classes

    =?Utf-8?B?TWFyayBQYXJ0ZXI=?=, Jun 3, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    340
    =?Utf-8?B?TWFyayBQYXJ0ZXI=?=
    Jun 4, 2006
  3. arnuld
    Replies:
    9
    Views:
    391
  4. Mononoke
    Replies:
    0
    Views:
    348
    Mononoke
    Nov 21, 2007
  5. Sean McCourt
    Replies:
    3
    Views:
    139
Loading...

Share This Page