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

**URL:** <https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568>\
**Category:** C++\
**Created:** [November 11, 2023, 10:43am UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568 "2023-11-11T10:43:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![grissinibread](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@grissinibread](https://forum.codewithmosh.com/u/grissinibread)\
**Post date:** [November 11, 2023, 10:43am UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/1 "2023-11-11T10:43:41Z")

</div>

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;  
}

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [November 13, 2023, 2:39pm UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/2 "2023-11-13T14:39:52Z")

</div>

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) ```

---

<div class="post-metadata">

**Author:** ![grissinibread](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@grissinibread](https://forum.codewithmosh.com/u/grissinibread)\
**Post date:** [November 14, 2023, 6:38pm UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/3 "2023-11-14T18:38:28Z")

</div>

“”"  
#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;

```

}

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [November 15, 2023, 12:30am UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/4 "2023-11-15T00:30:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![grissinibread](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@grissinibread](https://forum.codewithmosh.com/u/grissinibread)\
**Post date:** [November 16, 2023, 11:00pm UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/5 "2023-11-16T23:00:23Z")

</div>

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;

```

}  
‘’’

---

<div class="post-metadata">

**Author:** ![grissinibread](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@grissinibread](https://forum.codewithmosh.com/u/grissinibread)\
**Post date:** [November 16, 2023, 11:01pm UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/6 "2023-11-16T23:01:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![POMPOMBALLS](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/pompomballs/32/6610_2.png) [@POMPOMBALLS](https://forum.codewithmosh.com/u/POMPOMBALLS)\
**Post date:** [November 17, 2023, 3:43pm UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/7 "2023-11-17T15:43:10Z")

</div>

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](https://lldb.llvm.org) and it is not an error, it is a debugger.

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

_```_  
_code_  
_` ``_

---

<div class="post-metadata">

**Author:** ![grissinibread](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@grissinibread](https://forum.codewithmosh.com/u/grissinibread)\
**Post date:** [November 17, 2023, 5:51pm UTC](https://forum.codewithmosh.com/t/data-structures-and-algorithms-arrays-solution-insert/23568/8 "2023-11-17T17:51:59Z")

</div>

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 🙂
