Click here to Skip to main content
15,904,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to do the implementation for the Sorted Set interface. Any other suggestions for good and clean code would be great
* 1. size(): return the number, n, of elements in the set
2. add(x): add the element x to the set if not already present;
Add x to the set provided that there is no element y in the set such
that x equals y. Return true if x was added to the set and false
otherwise.
3. remove(x): remove x from the set;
Find an element y in the set such that x equals y and remove y.
Return y, or null if no such element exists.
4. find(x): locate x in the sorted set;
Find the smallest element y in the set such that y ≥ x. Return y or
null if no such element exists.*/
C++
<pre>/*
 * 1. size(): return the number, n, of elements in the set
2. add(x): add the element x to the set if not already present;
Add x to the set provided that there is no element y in the set such
that x equals y. Return true if x was added to the set and false
otherwise.
3. remove(x): remove x from the set;
Find an element y in the set such that x equals y and remove y.
Return y, or null if no such element exists.
4. find(x): locate x in the sorted set;
Find the smallest element y in the set such that y ≥ x. Return y or
null if no such element exists.*/
#include <iostream>

class SSet{
private:
    int *arr;
    int size=0;
public:
    void insert(int var);
    void remove(int var);
    int find(int *arr, int left, int right, int value);
    int getSize();
    SSet()
    {
        arr=new int;
    }
    SSet(const SSet& sset);
    ~SSet()
    {
        delete arr;
    }
};

//resource handle->default memberwise copy is not good
SSet::SSet(const SSet &sset)
{
    int local_size=sset.size;
    for(int i=0; i<local_size; i++)
    {
        arr[i]=sset.arr[i];
    }
}

void SSet::insert(int var) {

    if(size==0)
    {
        arr[0]=var;
        return;
    }

    if (find(arr, 0, size - 1, var) == 1) {
        return;
    } else {
        size++;
        int index = 0;
        while (var < arr[index] && index < size) {
            index++;
        }
        for (index; index < size; index++) {
            arr[index + 1] = arr[index];
        }
    }

}

void SSet::remove(int var)
{
    int index=0;
    if(find(arr, 0, size, var))
    {
        size--;

        for (index = 0; index < size; index++)
        {
            if (arr[index] == var)
            {
                for (index; index < size; index++)
                    arr[index] = arr[index + 1];
                return;
            }
        }
    }

    else{
        return;
    }
}

int SSet::find(int *arr, int left, int right, int value)
{
    if (right >= left)
    {
        int middle = left+(right-left)/2;
        if (arr[middle] == value)
        {
            return middle;
        }
        if (value < arr[middle])
        {
            find(arr, left, middle - 1, value);
        }
        else
        {
            find(arr, middle+1, right, value);
        }
    }
    return -1; //not found
}
int SSet::getSize()
{
    return size;
}

int main()
{
    SSet s_set;
    int *arr=new int;
    s_set.insert(5);
    s_set.insert(7);
    s_set.insert(3);

    int size=s_set.getSize();
    for(int i=0; i<3; i++)
    {
        std::cout<<arr[i]<<" ";
    }

    int searchFor;
    std::cin>>searchFor;
    std::cout<<s_set.find(arr, 0, size, searchFor);


    delete[] arr;
}


What I have tried:

I searched in Google to find the way, but I thought it would be much faster to learn the right way by asking myself
Posted
Updated 29-Dec-18 21:52pm
v3

One error I see in your code is in the array allocation. You are not giving it a size. Typically it's done like this :
C++
int *arr=new int[arraySize];
// more code here
delete[] arr;
The size of the array has to be passed to the allocator.
 
Share this answer
 
Um.
Think about what your code will do at the moment:
for (index; index < size; index++) {
    arr[index + 1] = arr[index];
}
Assume you have a five element array:
A B D E F
And you want to insert 'C'
Your while loop will locate the right place: index will be 2 when it exits.
But what happens each time round the loop:
First go:
A B D D F

Second go:
A B D D D

Third go:
A B D D D ARRGH!
As it runs off the end...
And you never insert var at all.
You can so the inserts with a simple loop, but you have to do it carefully:
1) var = 'C'
2) temp = array[index];
3) array[index] = var;
4) var = temp;
5) index++;
6) repeat from (2) until out of elements.


I'd also suggest you should check for over- and under-run on your array.
And you are going to have real problems very soon, unless you increase the space you allocate to arr - a single integer is not going to be anywhere near enough...
 
Share this answer
 
Comments
Member 13277493 30-Dec-18 4:17am    
thanks!
OriginalGriff 30-Dec-18 4:22am    
You're welcome!
OriginalGriff 30-Dec-18 4:25am    
Just so you know: the advanced version needs only one loop: starting at the tail end and working backwards means you don't need a temporary variable.
Member 13277493 30-Dec-18 5:36am    
thanks, I will think about it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900