0

okay so basically yes I need help with my homework Im not even going to lie lol^^; so one thing we have to do is get all these methods to work together in harmony till the complier program we house our labs in says pass image of complier showing the pass or fail messages

here is another image of the complier and how it will show the pass or fail based on the result when ran

other image of the complier showing the pass or fail of the result of the functions entered as it communicates to the ..thing

so i have the following things i need help on , so the default since it shows pass i believe i did that correctly and there are some other things in this i still need help on so and yes i have been putting in effort myself i just well pointers have always screwed me up xD thanks in advance .

template<typename Type>
class DynArray {

    friend class TestSuite; // Giving access to test code

    Type* mArray;
    unsigned int mSize;
    unsigned int mCapacity;

public:

    // Default constructor
    //      Creates a new object
    // In:  _startingCap        An initial capacity  to start the array at (optional)
    DynArray(unsigned int _startingCap = 0)
    {
    
        
        mArray = new Array[strlen(_startingCap) + 1];
        strcpy_s(mArray, strlen(_startingCap) + 1, _startingCap);

    }
    

    // Destructor
    //      Cleans up all dynamically allocated memory
    ~DynArray()
    {
        delete[] mArray;
    }

    // Copy constructor
    //      Used to initialize one object to another
    // In:  _da             The object to copy from
    DynArray(const DynArray& _da)
    {
        *this = _da;
    }

    // Assignment operator
    //      Used to assign one object to another
    // In:  _da             The object to assign from
    //
    // Return: The invoking object (by reference)
    //      This allows us to daisy-chain
    DynArray& operator=(const DynArray& _da) {
    
    }

    // Overloaded [] operator
    //      Used to access an element in the internal array (read-only)
    // In:  _index          The index to access at
    //
    // Return: The item at the specified index (by reference)
    const Type& operator[](int _index) const 
    {
    
    }

    // Overloaded [] operator
    //      Used to access an element in the internal array (writeable)
    // In:  _index          The index to access at
    //
    // Return: The item at the specified index (by reference)
    Type& operator[](int _index) {
        
    }

    // Get the current number of elements actively being used
    //
    // Return: The current number of elements used
    int Size() const {
        
    }

    // Get the current capacity of the internal array
    //
    // Return: The capacity of the array
    int Capacity() const {
        
    }

    // Clear the class for re-use
    //          Should clean up all dynamic memory and leave the object the same as if the default constructor had been called
    void Clear() {
        
    }

    // Add an item to the end of the array
    //          Should resize the array if needed
    // In:  _data           The item to be added
    void Append(const Type& _data) {
        
    }

    // Resizes the internal array, and copies all data over
    // In: _newCapacity     The new capacity of the array
    //  NOTE:   If 0 is passed, the array should double in size
    //          If _newCapacity < mCapacity, do nothing
    //
    //  SPECIAL CASE: If mCapacity is 0, then it should be set to 1
    void Reserve(unsigned int _newCapacity = 0)
    {
        
        if (_newCapacity < mCapacity)
        {
            continue;
        }
        if (mCapacity = 0)
        {
            mCapacity = 1;
        }
    }

};
#endif
  • i think that "continue" is pretty close for the one thing at the bottom – yallneedsomebleach Aug 06 '20 at 04:12
  • `if (mCapacity = 0)` You're missing an equals sign for comparison `==`. Right now it is assigning 0 every time. You can't continue outside a loop either. – Retired Ninja Aug 06 '20 at 04:14
  • omg bless <3 @ Retired Ninja lol i didn't even see that wow – yallneedsomebleach Aug 06 '20 at 04:14
  • oh the fields already set up are the way the code was given to us so that's why there are fields but they are empty – yallneedsomebleach Aug 06 '20 at 04:17
  • One good coding habit for c++ that will help avoid the accidental assignment issue called out by @RetiredNinja is to put your constant variable on the left side of the == operator. `if (0 = mCapacity)` will throw a compiler error since you can't assign a value to 0. – Durstann Aug 06 '20 at 04:41
  • Sorry to disappoint but the default constructor is not right, even though you pass. `strlen(_startingCap)`. Starting cap is an integer, you cannot use strlen on an integer. In fact since this code is for an array of arbitrary type you should not be using `strlen` anywhere in your code, because it's a string function and this code has nothing to do with strings. – john Aug 06 '20 at 04:44
  • 1
    From the code you've given, do not attempt to implement any of the other functions until you get the creation and copy semantics correct. This means that you implement the constuctor, copy constructor, destructor, and then the assignment operator. Then test copying the DynArray around to make sure you don't get memory leaks / crashes / etc. Once you have that, then and only then do you attempt things like appending, clearing, etc. – PaulMcKenzie Aug 06 '20 at 05:16
  • I think Paul is making an important point. Don't rely on this testing harness you've been given. As well as using that, write your own tests to check the code you've just written. Only when you're happy with those try your code on the official tests. – john Aug 06 '20 at 05:19
  • ooo yea this assignment is a toughie lol – yallneedsomebleach Aug 06 '20 at 05:54

2 Answers2

0

Let's fix the one you have attempted. It illustrates many misunderstandings you have.

So think about what is required.

  1. We have to create an array with the given capacity, so that means allocating the given number of elements and assigning them to the mArray pointer

  2. We have to set the arrays capacity to be the given value.

  3. After creating the arrays size will be zero, so we have to set the size to zero.

Putting that together we get

DynArray(unsigned int _startingCap = 0)
{
    // allocate memory for the given capacity
    mArray = new Type[_startingCap];
    // set the array capacity to the given value
    mCapacity = _startingCap;
    // set the size to zero
    mSize = 0;
}

I don't know where to start with your code. You've clearly copied some string code from somewhere and tried to adapt it to the new situation. But this code has nothing to do with strings, and even forgetting about the difference between strings and arrays the code you copied was doing something completely different.

Do you understand the concepts of size and capacity, and the difference between them? The code you wrote above suggests you don't. You're really going to have to understnad that concept before you go any further.

The size of an array is how many valid elements an array has. Simple enough.

The capacity of an array is how much memory it has allocated. An array can have a larger capacity than its size (but obviously it can't have less). The extra capacity allows an array to grow (i.e. increase it's size) without having to reallocate the memory that holds the array elements.

john
  • 85,011
  • 4
  • 57
  • 81
  • oh so like i said the code that's in the brackets is code We were Given as in template code provided for us – yallneedsomebleach Aug 06 '20 at 05:00
  • and we have to work within using those sorta as a skeleton rather to build up off of – yallneedsomebleach Aug 06 '20 at 05:01
  • @yallneedsomebleach The code given is not even legal. What is `Array`, it's not decalred anywhere. `strlen(_startingCap)` is not legal because `_startingCap` is an integer, not a string. etc. etc. Something has gone badly wrong if that is the code you are supposed to use. I would talk to your teacher because that code makes no sense at all. – john Aug 06 '20 at 05:03
  • @yallneedsomebleach Apologies for thinking that code was yours, when it was code you'd bean given. – john Aug 06 '20 at 05:05
  • @yallneedsomebleach Were you also given this code `*this = _da;`? Because that code is also wrong. Something strange going on here. – john Aug 06 '20 at 05:07
  • you're fine lol and oh you meant the big string stuff? yea haha that was me lol i thought you meant the params and everything already set in place – yallneedsomebleach Aug 06 '20 at 05:08
  • the this = da .. i had tried something the teacher did for lecture code but ah yea that didn't go well ugh – yallneedsomebleach Aug 06 '20 at 05:09
  • @yallneedsomebleach -- `this = da;` -- get rid of that line and actually implement the copy constructor. To be honest, your teacher has it backwards -- the copy constructor should be fully implemented first, and then the assignment operator would be the last thing to implement. – PaulMcKenzie Aug 06 '20 at 05:10
  • @yallneedsomebleach I think you need to spent a little more time analysing what you are trying to achieve. Throwing vaguely related code at a problem is not the way to program. It's almost never going to work. – john Aug 06 '20 at 05:11
  • i have tried to make the array null by assigning it to null ptr then trying it by having something point to it ??? but it still pops up the message that it needs to be assigned to null – yallneedsomebleach Aug 06 '20 at 05:11
  • @yallneedsomebleach It's not clear what you mean, you need to show the actual code. It's very hard to understand descriptions of code – john Aug 06 '20 at 05:13
  • okay i am still a low level it says on overflow so i'll answer it to show the picture and everything – yallneedsomebleach Aug 06 '20 at 05:15
  • @PaulMcKenzie okay consider it done i guess from what he has told us he has moved things around to try different things out ,... why ? idk he just said some things might be out of order – yallneedsomebleach Aug 06 '20 at 05:21
  • // Copy constructor // Used to initialize one object to another // In: _da The object to copy from DynArray(const DynArray& _da) { std::copy << _da; } i put this for my copy constr at least that's what i looked up and that's what it seemed to look like but ;; – yallneedsomebleach Aug 06 '20 at 05:25
  • Ok, but you should implement the fixes suggested in this answer first to fix your constructor. Then the copy constructor, to make it simple, should not use the assignment operator, but instead actually make the copy on its own, as if there was no assignment operator available. Once you have that, it will literally take less than 5 minutes to write a fully working assignment operator, believe it or not. – PaulMcKenzie Aug 06 '20 at 05:26
  • awesome *0* let me do these things real quick thx btw all of you~ T T – yallneedsomebleach Aug 06 '20 at 05:27
  • @yallneedsomebleach -- To give you a hint, use [copy/swap](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) when implementing the assignment operator. Basically just 5 lines of code consisting mostly of `swap` calls, and the assignment operator will magically work. – PaulMcKenzie Aug 06 '20 at 05:31
  • let's see the complier or the assignment the way the teacher has it he wants it set as null – yallneedsomebleach Aug 06 '20 at 05:46
0

here is a updated verison with the suggestions implemented ^U^ template class DynArray {

friend class TestSuite; // Giving access to test code

Type* mArray;
unsigned int mSize;
unsigned int mCapacity;

public:

// Default constructor
//      Creates a new object
// In:  _startingCap        An initial capacity  to start the array at (optional)
DynArray(unsigned int _startingCap = 0)
{

    
    mArray = new Type[_startingCap];
    mCapacity = _startingCap;
    mSize = 0;

}


// Destructor
//      Cleans up all dynamically allocated memory
~DynArray()
{
    delete[] mArray;
}

// Copy constructor
//      Used to initialize one object to another
// In:  _da             The object to copy from
DynArray(const DynArray& _da)
{
    operator = (_da)
}

// Assignment operator
//      Used to assign one object to another
// In:  _da             The object to assign from
//
// Return: The invoking object (by reference)
//      This allows us to daisy-chain
DynArray& operator=(const DynArray& _da) {

}

// Overloaded [] operator
//      Used to access an element in the internal array (read-only)
// In:  _index          The index to access at
//
// Return: The item at the specified index (by reference)
const Type& operator[](int _index) const 
{

}

// Overloaded [] operator
//      Used to access an element in the internal array (writeable)
// In:  _index          The index to access at
//
// Return: The item at the specified index (by reference)
Type& operator[](int _index) {
    
}

// Get the current number of elements actively being used
//
// Return: The current number of elements used
int Size() const 
{
    return Size;
}

// Get the current capacity of the internal array
//
// Return: The capacity of the array
int Capacity() const {
    
}

// Clear the class for re-use
//          Should clean up all dynamic memory and leave the object the same as if the default constructor had been called
void Clear() {
    
}

// Add an item to the end of the array
//          Should resize the array if needed
// In:  _data           The item to be added
void Append(const Type& _data) {
    
}

// Resizes the internal array, and copies all data over
// In: _newCapacity     The new capacity of the array
//  NOTE:   If 0 is passed, the array should double in size
//          If _newCapacity < mCapacity, do nothing
//
//  SPECIAL CASE: If mCapacity is 0, then it should be set to 1
void Reserve(unsigned int _newCapacity = 0)
{
    
    if (_newCapacity < mCapacity)
    {
        continue;
    }
    if (mCapacity == 0)
    {
        mCapacity = 1;
    }
}

};

  • Your copy constructor is effectively identical to the version you have before, and is bugged in exactly the same way. Write the copy constructor from first principles, it needs to allocate some memory and then copy over the data from `_da`. Instead of looking for some code that will just magically work, think about what the copy constructor needs to do in terms of allocating memory and copying data and then implement that. There's no shortcuts here. – john Aug 06 '20 at 06:15
  • oh i see let me try – yallneedsomebleach Aug 06 '20 at 06:23
  • so i have since it copies i copied the defined info on the top of the project (the unsinged int part ) and put it in my copy like so // Copy constructor // Used to initialize one object to another // In: _da The object to copy from DynArray(const DynArray& _da) { mArray = new Type[_startingCap]; mCapacity = _startingCap; mSize = 0; } – yallneedsomebleach Aug 06 '20 at 06:27
  • So in that code where are you copying from `_da`? The code you've written doesn't even mention `_da`. – john Aug 06 '20 at 06:34
  • In fact the code doesn't even compile because `_startingCap` is an undeclared variable. How are compiling this code? Can't you tell that it doesn't compile? – john Aug 06 '20 at 06:37
  • I'm going to be completely honest. You haven't shown that you have any understanding of what you are doing. Everything I've seen you do so far is just, copy some code from somewhere, put it in a new place, and cross your fingers and hope it works. You need to **analyse** what you need to do, and come up with **original** code that does what your analysis told you needs doing. I completely understand that both of those things are hard, but that's what programming is. I think you need to find someone locally, who can go through these things in detail with you, because you have a lot to learn. – john Aug 06 '20 at 06:43
  • Look at my answer above. I laid out what the constructor needs to do, three bullet points, 1, 2, and 3. That's the analysis phase. And then I wrote some code that did each of the three things, that's the original code. It's that approach you need to take for each of the methods that you need to implement. – john Aug 06 '20 at 06:50