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.
    Code:
    #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.

    Code:
    #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! I really need the real answers (like in C++ language for Borland if possible).
    Last edited by Nicknameguy; 10-25-2002 at 04:24 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I got good news and bad news. The good news is that I can help you. The bad news is I won't (and I'm sure most respectable people on the boards won't either) until you produce some code. I cannot help when I haven't been asked a question.

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

    Didn't I post code?

    Didn't I post code in my letter? I wrote out the beginning of each function? I am so confused on how to write the functions, I have started to write the begining but I am so confused on the rest. PLEASE HELP. Is problem One correct? You do not have to answer problem three.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Problem One looks correct. Try putting code tags around your code, though, so it's clear to everyone where it is.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

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

    Thanks

    Thank you so much for your help. It really does help me out a lot. I know this is a lot but cand you tell me how to finish the Delete Element?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to delete a random element from an array you need to do two things:

    1) find the index of an array element that has the desired value to delete (in a true mathematical set there should be only one element with a given value);

    2) all elements with indexes higher than the index of the element to be removed need to be shifted to the left by one (the indexes end up one value lower after the deletion)

    Often a loop is used to find the element to delete and then a loop is used to shift elements to the left (down). Here's one way to accomplish a single shift to the left (down) for a single element of an array:

    array[i] = array[i + 1];

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

    Turbo C++

    Okay I am using turbo C++ and I am trying to use bool but it keeps saying like "type name expected" or "multiple declaration of bool" or "declaration missing ;"

    here is my code.

    Code:
    #ifndef SET_H
    #define SET_H
    
    class set
    {
     public:
    	set(); //a constructor
    		void printSet(); //prints the object to the console
    		set setUnion(set); //unions the object with another set
    		set setIntersection(set); //interesects object with another set
    		bool isEmpty(); //determines if the object is empty
    		bool isElementOf(double); //determines if an element is in an object
    		void addElement(double); //adds element to the object
    		void deleteElement(double); //removes an element from the object
    		int getSize(int); //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
    Last edited by Nicknameguy; 10-24-2002 at 04:53 PM.

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

    Thanks

    Thanks for the program and it turns out that Turbo C++ does not have Bool.

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

    Similar to Borland

    Are any of those similar to Borland 5.6? I think that is what version it is. It is the Borland that is not the DOS version.

    Okay, still abit confused on the deleteElement:

    Code:
     void set::deleteElement(double element)
    {
     for(int i=0;i<size; i++)
     setArray[i]=setArray[i+1];
    }
    that moves the elements one space up. Now I am confused on how to make it delete the certain element (I have 5 elements {1, 3, 5, 6, 9} and I want to get rid of the third element). I think you have to use a for loop but it still is confusing. Do you do like

    Code:
    for(int i=0; i<element; i++)
    That probably is showing how completely confused I am with C++ (if the messages before haven't). I believe it is element because aren't you looking for that specific element to delete? If you just use the first for loop, it will delete the first element and put a 0 at the end. I don't want that. I've tried to actually show that I can do this on my own (or well try) and I am hoping that you can help (considering this time I actually have put in some code). Thanks.

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

    If

    Lets say I wanted to put an error message if there is no set. Like if there isn't a set you can't delete from it. Um, in the addition one i could find where to put it. Where do i put it in the delete? OH yes, and is there anyway to not use the break? We've never learned it.
    Last edited by Nicknameguy; 10-25-2002 at 06:49 PM.

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

    Please help finish

    Code:
    #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)               //T. Gave us
     {
     		 if(size==100)
      		{
       	 cout<<"The array is too big.  Element not added."<<endl;
      		}
      		 else
      		{
           setArray[size]=element;
       	 size=size+1;
      		}
     }
    //==============================================================================
      void set::deleteElement(double element)
     {
    	int i;
    
         if(size==0)
        {
         cout<<"There is no set.  Element not deleted."<<endl;
        }
         else
        {
        	// find the element to delete
         	 for (i=0; i<size; i++)
        	{
            if (setArray[i]==element) break;  //Check to see if element was found
          }
        }
    
    
        // i points to the element to delete
        // move everyone after it down one
         for (i; i<size; i++)
        {
         setArray[i]=setArray[i+1];
        }
    
        // one less element now
        size=size-1;
    }
    //==============================================================================
      bool set::isEmpty()    									//T. Gave us
     {
     		if(size==0)
          {
          	return true;
          }
           else
          {
          	return false;
          }
     }
    //==============================================================================
      bool set::isElementOf(double element)
     {
    	bool found=false;
    
       for(int i=0; i<size && !found; i++)
       	 if(setArray[i]==element)
          {
           found=true;
          }
           else
          {
           found=false;
          }
       return found;
     }
    //==============================================================================
      bool set::isEqual(int mySet)
     {
       if(size!=mySet.getSize() )
      {
    
      }
     }
    //==============================================================================
      int set::getSize()
     {
      return size;
     }
    //==============================================================================
      void setUnion()
     {
    
     }
    //==============================================================================
      void setIntersection()
     {
    
     }
    //==============================================================================
      int main()
     {
    		set mySet;
          mySet.printSet();				//print an empty set to the screen
          mySet.addElement(1);			//put the element in the set
          mySet.addElement(2.1);		//put the element in the set
          mySet.addElement(3);
          mySet.addElement(7);
          mySet.addElement(134);
          mySet.addElement(32);
          mySet.addElement(100);
          mySet.addElement(4);
          mySet.addElement(343);
          mySet.addElement(321.8);
          mySet.printSet();				//print the set {1} to the screen
     }

  12. #12
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    No, my toaster has not recoverd from mad cow disease.

    ~Inquirer

    P.S. Wasn't that your question?
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

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

    Ha Ha Okay

    Yeah I see your point. What I would like help with is isEqual, setIntersection, and setUnion. I know in setEqual you have to like declare another set (i think) but I don't know how to do that. Then with that new set you make like an if statement saying
    Code:
     if(new set != mySet.getSize)
    {
    make the new set false.

    But I am so confused on how to do the rest. After the else statement.

    =====================================

    For the Intersection and Union, I have no idea where to start. I have to have the isEqual done don't I? I'm very confused on how to like declare a new set, and once I've done that do you like mySet.printSet for both of the sets? That can't be right. I'm so confused. Please someone help me. The union is probably easy considering all you have to do is print out all of the elements but you can't print out doubles. How do you stop that? And the intersection you have to make sure that you take everything in the set that is not in the other one. I'm so confused. And my teacher even said like intersection is five lines and union is even worse...it's four. I hope that qualifies as being a question.

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

    New

    OKay want to know if this could be right for the isEqual.

    Code:
      bool set::isEqual(set object)
    {
      bool answer = false;
         int number = 0;
         if(size == object.getSize() )
       {
                for(int i=0; i<size; i++)
              {
                   for(int e=0; e<size; i++)
                 {
                        if(setArray[i] == object.setArray[e] )
                      {
                           number = number + 1;
                       }
                   }
               }
                             if(number==size)
                           {
                             answer=true;
                            }
       }
          return answer
    }
    That seems really confusing. There has to be a way to shorten it down. how?

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