Set Classes

This is a discussion on Set Classes within the C++ Programming forums, part of the General Programming Boards category; Blue ink means it is on the worksheet Red ink means the teacher told us that it is right or ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Exclamation Set Classes

    Blue ink means it is on the worksheet
    Red ink means the teacher told us that it is right or it is on the worksheet as an output.
    Black ink means it is me!

    I need help with writing functions in C++. The C++ class I am in is going like way too fast and it's like the basic (not BASIC) class. First of all I would like to know what I am doing is correct. Like in noted in the rules I am not looking for you to write the whole program, well unless you want to.


    Problem One.
    Write the set.h file. There are guidelines for this file. For the sake of commonality, I am giving you the member functions and the private data. Make sure you stick to my names so that I can swam files in and out to test them.
    • Your private data should include an array of doubles (size = 100) called setArray. Itshould also include one integer called size . These structures will be used to store the set data (the elements) and to keep track of the set size (respectively).
    • Your public functions should include all of and only the following:
    • set() //a constructor
    • printSet //prints the object to the console
    • setUnion //unions the object with another set
    • setInteresection //intersects object with another set
    • isEmpty //determines if the object is empty
    • isElementOf //determines if an element is in a object
    • addElement //adds element to the object
    • deleteElement //removes an element from the object
    • getSize //returns the current size of the object
    • isEqual //determines if the object is equal to a set



    This what I have for the header file. I don't know if it is all correct. I think it is.

    #indef SET_H
    #define SET_H

    class set
    {
    public:
    set(); //a constructor
    void printSet(); //prints the object to the console
    void setUnion(set); //unions the object with another set
    void setIntersection(set); //interesects object with another set
    bool isEmpty(); //determines if the object is empty
    bool isElementOf(set); //determines if an element is in an object
    void addElement(set); //adds element to the object
    double deleteElement(set); //removes an element from the object
    int getSize(); //returns the current size of the object
    bool isEqual(set); //determines if the object is equal to a set

    private:

    double setArray[100];
    int size;
    };

    #endif



    Problem Number Two
    Your task is to write the set.cpp file. The constructor should set the size to 0.


    This is what I have for the CPP File. I am so confused on some of the functions. I really need help with the functions.


    #include <iostream.h>
    #include "set.h"

    //constructor
    set::set()
    {
    size=0;


    void set::printSet()
    {
    for(int i=0; i<size; i++)
    cout<<setArray[i]<<" ";
    }

    void set::addElement(double element) //Teacher gave-is it right?
    {
    element=setArray[element];
    size=size+1;
    }

    void set::deleteElement(double element)
    {

    }

    bool set::isEmpty() //teacher gave it to us. It is right.
    {
    if(size==0)
    {
    return true;
    }
    else
    {
    return false;
    }
    }


    bool set::isElementOf(set)
    {

    }

    bool set::isEqual(set)
    {

    }

    int set::getSize()
    {

    }

    int main()
    {
    set mySet;
    mySet.printSet(); //print an empty set to the screen
    mySet.addElement(1); //put the element 1 in the set
    mySet.addElement(2.1); //put the element 2.1 in the set
    mySet.printSet(); //print the set {1, 2.1} to the screen
    }

    }


    Be sure to check for boundary cases, i.e. cases that may cause some problems. For instance, what happens when you try to add an element to a set that already has 100 elements in it? The cumputer will crash on you more than likely. To prevent this your addElement function should check to see if the set is full before adding it. If it is, it should probably print out an error message such as "Error: set is full. Element not added." The same sort of thing should happen is you try to remove an element that is not in a set. Be careful with the union function as well; it would be quite easy to exceed the maximum length.

    Problem Number Three
    You are to write a main program that acts as an interface for your set class. Note that this is not the same as the main programs you should have been writing for your test class. This program should allow the user to create up to 3 sets and manipulate them with the member function. This needs some explaining.

    First, you should print out an options screen. You may want to make this into a function (in your main program, not in the set class).

    **********************
    MAIN MENU

    Select a number.

    1. Create a new set
    2. Delete a set
    3. Add an element to a set
    4. Delete an element from a set
    5. Print a set to the console
    6. Display the union of two sets
    7. Display the intersection of two sets
    8. Decide if a set is empty
    9. Decide whether or not an element is in a set
    10. Decide if two sets are equal
    11. Get the size of a set
    12. Quit

    Please enter a number:


    Now once the user makes a choice, another menu should come up. (Let's say that two sets have already been declared and the user has put some things in them.) For instance, suppose that the user selects option 7. The following screen should come up.

    ***********************
    Select one of the following for
    Your intersection:

    1. Set 1
    2. Set 2

    Please enter a number: 1
    Please enter another number: 2
    The intersection of Set 1 and Set 2 is


    After this, the main menu screen should come back up.

    If the user asks to, for example, add an element to the set, and there are no sets, a screen should display this information.

    ************************
    There are no sets to work with.
    Enter 0 to return to the main menu.


    Let the user put in 0, and the print the main menu screen.

    Now we have not talked about while loops, but you will need one in order to run this screen, you will need one. When we get this far, I will provide you with some more information.


    I am confused on how I should do this. Should I just have like a lot of IF statements?


    Please respond ASAP!

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    Problem 3:

    I didn't read your whole thing...but a switch is a safe resort to alot of if statements...ex:

    Code:
    #include <iostream.h>
    main()
    {
    cout<<"Enter a number between 1-3";
    unsigned int number;
    cin>>number;
    switch(number)
    {
    case 1:
    //function call or whatever
    break; //don't forget the break...it skips the rest of the cases and jumps to the end of the {}
    case 2:
    //same as one only if number==2
    break;
    case 3:
    //more stuff
    break;
    }
    }
    Last edited by Extol; 10-21-2002 at 02:32 PM.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Thanks

    Hey! Thanks but do you like no how to do what you said? I am soo confused. Like what should I do for the delete element and functions like that?

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    23

    Problem II

    I am mostly interested in the programming of the functions for Problem II. I don't know how to start any of the functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. optimising program
    By SONU in forum C Programming
    Replies: 1
    Last Post: 05-18-2008, 11:28 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 02:08 AM
  4. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21