C++ Programming (Problem One)

This is a discussion on C++ Programming (Problem One) within the C++ Programming forums, part of the General Programming Boards category; I need help with writing functions in C++. The C++ class I am in is going like way too fast ...

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

    Exclamation C++ Programming Set Class (Problem One)

    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


    now I'm thinking that it is...
    Wait I don't have the answer with me but I will like in about half an hour. But if you want to help out please do.
    Last edited by Nicknameguy; 10-16-2002 at 01:51 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The frist step is to write out what to do in your native language. Then try to translate that into code.

    Here's some thoughts:
    a set of data may or may include duplicates of any given value, depending on your definition of what a set is, although typically duplicate values are not allowed.

    the union set of a two generative sets are all values of both generative sets without any duplicates. That is if the sets were {2, 3} and {2, 4} then the union is {2, 3, 4}.

    the intersection of two sets are only those values that are present in both generative sets. The intersection of the above two sets is just {2}.

    A set is empty if the size is zero.

    Most of the functions will be using loops (single or nested) looking for equality or inequality of a given element in one set (or a given value to add or delete) with all the elements of the other and then doing something with the element as determined by what the function is supposed to do.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    23
    Hey! Thanks. I will have the source code for the set.h file and I really need to know how to finish the set.cpp file. I will have the answers tomorrow

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    //set.h
    class set
    {
      public:
         set(); //a constructor
    
         void printSet(); //prints the object to the console
    
         set  setUnion(const set &); //unions the object with another set
    
         set  setInteresection(const set &); //intersects object with another set
    
         bool isEmpty(); //determines if the object is empty
    
         bool isElementOf(double); //determines if an element is in a object
    
        void addElement(double); //adds element to the object
    
        void deleteElement(double); //removes an element from the object
    
        int getSize() const; //returns the current size of the object
    
        bool isEqual(const set &); //determines if the object is equal to a set
       
      private:
        int size;
        double setarray[100];
    };
    The header file is relatively simple. The source code in the cpp file is where the thinking goes. Good luck.

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

    Problem One (header file)

    Hey this is what I have for the header file. I don't know if it is all correct. I can't use const because I don't know what it is (We haven't gone over it)

    #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

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

    Problem Two (CPP File)

    This is what I have for the CPP File. I am so confused on some of 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)
    {
    element=setArray[element];
    size=size-1;
    }

    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
    }

    }
    Last edited by Nicknameguy; 10-17-2002 at 01:36 PM.

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

    More information on Problem Two

    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 <COLOR=red>addElement</COLOR> 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 <COLOR=red>"Error: set is full. Element not added."</COLOR> 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.

    The above paragraph is taken right from the worksheet.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    11
    you can put most of that stuff in your header file.

    also, you should use } to close your constructor.
    --Cid666

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

    Problem

    Okay but how do I finish the functions? That is what I am confused about.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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