Data Structures and Algorithms - Arrays - Solution - insert()

Hello, I am currently starting out the Data structures and Algorithms course, but I am trying to follow along with C++ in XCode. I keep getting a warning that states “11db”. I’m not exactly sure what’s wrong but i have put a note where I know the problem is occurring from which is in my insert function. Anyone have a clue on how I can fix this problem? This is what my code currently looks like:

#include “Array.hpp”
#include

using namespace std;

Array::Array(int size)
{
setSize(size);
}

int Array::getSize()
{
return size;
}

void Array::setSize(int size)
{
if(size < 1)
throw invalid_argument(“size”);
this->size = size;
}

void Array::insert(int number)
{
if(count < size)
numbers[count] = number;
// count++;
//this is where we’ve been defeated so far…
}

void Array::print()
{
for (int i = 0; i < size; i++)
cout << numbers[i] << endl;
}

There can be many possible things. Memory may not have been assigned or cleared properly.

Show your class structure. Write codes of Array.hpp, Array.cpp and main.cpp inside ` `` (3 backticks, without spaces) ```

“”"
#ifndef ARRAY_ARRAY
#define ARRAY_ARRAY

class Array
{
public:
Array() = default;
Array(int size);
int getSize();
void setSize(int size);
void insert(int number);
void print();
private:
int size = 0;
int count = 0;
int numbers;
};

#endif
“”"
#include “Array.hpp”
#include

using namespace std;

Array::Array(int size)
{
setSize(size);
}

int Array::getSize()
{
return size;
}

void Array::setSize(int size)
{
if(size < 1)
throw invalid_argument(“size”);
this->size = size;
}

void Array::insert(int number)
{
if(count < size)
numbers[count] = number;
// count++;
//this is where we’ve been defeated so far…
}

void Array::print()
{
for (int i = 0; i < size; i++)
cout << numbers[i] << endl;
}
“”"
#include “Array.hpp”

int main()
{
Array numbers{6};

numbers.insert(5);
numbers.insert(6);
numbers.insert(7);
numbers.insert(8);
numbers.insert(9);
numbers.insert(10);
numbers.print();

return 0;

}

Declare numbers as an array in Array.hpp and remove comments from count in Array.cpp.

Sorry, I’m just realizing I sent the wrong code. The actual code isn’t much different but here is that code (I still haven’t figured out why I am getting the error (11db).


#ifndef ARRAY_ARRAY
#define ARRAY_ARRAY

class Array
{
public:
Array() = default;
Array(int size);
int getSize();
void setSize(int size);
void insert(int number);
void print();
private:
int size = 0;
int count = 0;
int numbers;
};

#endif

‘’
#include “Array.hpp”
#include

using namespace std;

Array::Array(int size)
{
setSize(size);
}

int Array::getSize()
{
return size;
}

void Array::setSize(int size)
{
if(size < 1)
throw invalid_argument(“size”);
this->size = size;
}

void Array::insert(int number)
{
//if (size == count)
//int largerNumbers = (size * 2);

numbers[count++] = number; // items can be incremented as so

}

void Array::print()
{
for (int i = 0; i < size; i++)
cout << numbers[i] << endl;
}
‘’
‘’’
#include “Array.hpp”

int main()
{
Array numbers{6};

numbers.insert(5);
numbers.insert(6);
numbers.insert(7);
numbers.insert(8);
numbers.insert(9);
numbers.insert(10);
numbers.print();

return 0;

}
‘’’

Also, I already have numbers declared as an array. This website just makes it look like a checkbox though. lol

It was so simple that it was missed.

Above array declaration is static. If we use a static array declaration we need to mention its size. It would be constant throughout the program. You need to use a pointer instead.

Perform following changes:

  • Define numbers as a pointer
  • Use new operator to set size of array (dynamic memory allocation)
  • Manually free memory assigned to the pointer
  • Set pointer to nullptr

The error is not 11db it is lldb and it is not an error, it is a debugger.

P.S. - Backtick (`) is located below Escape button, above Tab button.

```
code
` ``

1 Like

Thank you so much! I went ahead and went back to the topic of pointers in the course. Thank you again for the helpful changes, this was very helpful! Appreciate it lots :slight_smile: