Weird pointer issue...constructing BST using array of pointers - c++

So I am trying to create a BST using an array of pointers. My algorithm is correct (tested a version that doesn't use pointers), but when using the below code, the following occurs:
If I add the first element, it is added to position 1 of the array.
If I add a second element, for some reason, position 1 of the array is overwritten to this element, and then the program continues (else part) and attempts to insert it again.
EG. (traced the program with a bunch of couts)
1. Call add(5, 1)
inserting 5 into position 1
position 1 is now 5
2. Call add(4, 1)
position 1 is now 4
moving right
inserting 4 into position 3
position 1 is now 4
...
template <typename Item> void ABTree<Item>::add(Item input, int index){
if (array[1]==0){
array[1] = &input;
size++;
}else{
if (input < *array[index]){
if (array[2*index] == 0){
array[2*index] = &input;
size++;
}else
add(input, 2*index);
}else{
if (array[(2*index)+1] == 0){
array[(2*index)+1] = &input;
size++;
}else
add(input, (2*index)+1);
}
}

There are a whole lotta troubles in the code you've provided.
Assigning address of the temporary variable is
inadmissible. The "input" variable ends up right after the add
function returns. Storing its address than is meaningless and leads
to almost inevitable crash. In particular, when you're adding second element to your array, those condition checks:
if (array[1]==0)
if (input < *array[index])
yields undefined result, and therefore undefined control flow.
I don't know type of the container you're using but I assume it to
be a kind of vector or even plain C array. If my guess is true, you
should perform a bounds check before accessing array's elements with
the index.
No reallocation of the memory accupied by the array is performed.
Or, alternatively, if you're using a constant size array, no check
is performed (see 2).
So, basing on the output, you've got the following sequence when adding second element:
On the check that head is empty - false;
On the check if (input < *array[index]) - false, since array[index] contains garbage, this is why "inserting 4 into position 3" despite we should add 4 to the position 2;
On the check if (array[(2*index)+1] == 0) - again false, since this element contains garbage too (am I right that you didn't initialize your array with zeros?);
So, recursive call to the add routine happens...

I altered the code to accept a pointer to the item to be inserted instead.
if (array[1]==0){
array[1] = input;
size++;
}else{
if (*input < *array[index]){
if (array[2*index] == 0){
array[2*index] = input;
size++;
}else
add(input, 2*index);
}else{
if (array[(2*index)+1] == 0){
array[(2*index)+1] = input;
size++;
}else
add(input, (2*index)+1);
}
}

Related

Is there a way to find the INDEX of the minimum value in an array using a recursive function? C++

I need to find the Index of the minimum number in an array using a recursive function in c++ the function can only get 2 parameters: the pointer to the array and the size of it.
int smallest(int arr[], int num);
I managed to do this but with a helper variable that is static and declared outside the function here is what I got:
static int flag = 0;
int smallest(int* arr, int num) {
if (flag == num - 1)
return flag;
if (arr[num - 1] > arr[flag]) {
return smallest(arr, num - 1);
} else {
flag++;
return smallest(arr, num);
}
}
Now my question is can I do this without the static variable or any other variable other than the num? here is what I got so far:
int smallest(int arr[], int num) {
if (arr != &arr[num - 1])
if (*arr < arr[num - 1])
smallest(arr, num - 1);
else
smallest(arr + 1, num);
return num - 1;
}
It doesn't return the index of the minimum value but it does get to its adress, the function ends when the array pointer address is the same as the address of the minimum value. how can I get it to return the index?
I'm a student and I'm pretty new to C++ I appreciate the help! thanks!
===
edit:
this is originally from a homework assignment but the constraint to not use external help variables or functions is mine! and I'm curious to know if its even possible.
Because this is obviously homework, I'm not going to reveal the ACTUAL answer in entirety, but I'll give some (hopefully) good advice.
With recursion, think first of what your end condition is. That should be an array of 1 element. You return the index 0 in that case because of the array you have, it's the only element, so return the index of it.
if(num == 1)
{
return 0;
}
So then how is that useful to you? Compare it to exactly one other element. That's how you break this down. Your array turns into "one element and MANY" or "It's just one element." If it's just one, return the only value. If it's many, call yourself recursively from the second element (index 1) onward:
int restOfArraySmallest = smallest(arr+1, num-1) + 1;
You need the +1 because you've offset where it starts from. But you know the value in restOfArraySmallest is the index into YOUR arr of the smallest value of everything except the first element. Because your recursive calls don't include the first element, just the rest.
Hopefully that's enough to get you the rest of the way.
Edit: Because the OP has responded and said it wasn't essential to their homework assignment, here's the entire function:
// Recursively finds the index of the smallest element of the passed-in array
int smallest(int* arr, int num)
{
if(num <= 1)
{
return 0; // If we're in the last element, the smallest is the only element
}
// More than one element, so find the least element in all the elements
// past the first element of the array
// Note: the index returned is offset from our current view of the array,
// so add +1 to any returned result so we can index directly from it
int restOfArraySmallest = smallest(arr+1, num-1) + 1;
// Compare the first element in the array to the smallest of the entire rest
// of the array:
if(arr[0] < arr[restOfArraySmallest])
{
// The first element is smaller, it's the smallest, so return that index
return 0;
}
else
{
// The element already found is smaller, so return that index. It's already
// correctly offset
return restOfArraySmallest;
}
}
And that's it. If there's duplicate entries for smallest, it will favor the LAST one.
The trick with recursion is to NOT keep external variables. What you pass as the arguments and RETURN BACK are all the information you have. For some algorithms, it's enough.
Note, if you use recursive functions with datasets big enough, you will eventually get a Stack Overflow. Not the website, the crash type. This algorithm is pretty light in that only one extra variable and the two arguments themselves get allocated each pass, but it adds up eventually.

C++ if(Array[x].length > 0)

I'm trying to make a simple database program that gives users the ability to add, remove, and edit string variables.
The issue I ran into: Assigning each added variable to the next empty array element.
-I'm using static arrays for now until I learn more about dynamic arrays.
-Since this is out of context I recreated the code here to make more sense, sorry if I missed anything.
//static array declaration
std::string names[5];
//string selection from main function
std::string stringSel = "Item";
//boolean condition for loop
bool completed = false;
//if (names[x].length > 0) then increment x by 1 until names[x].length == 0 and then set that array element to the value from stringSel
int x = 0
while(completed == false)
{
//if the length of the element is greater than zero characters, increment the element
if (names[x].length > 0)
{
x++;
}
//if the length of the element is not greater than zero characters, set the string to that element
else
{
//The empty element is assigned the string and the entire array prints out
names[x] = stringSel;
std::cout << stringSel << " was added to the database.\nThis is the printout of all current items: " << names;
completed = true;
}
}
The error I'm getting is coming from my if-statements condition. The '.' is underlined red
1 IntelliSense: a pointer to a bound function may only be used to call the function
names[x].length
std::string::length() is a member function, not a data member, so to access it you need to make a function call like:
names[x].length()

Determining length for an array allocation

This is the snippet of code that I'm puzzled about. I'm checking for how long an incoming string is. I've appended * in order to have a sentinel value to stop the while loop. Yet, I'm consistently getting a length value that is inclusive of the * and I don't understand why, since the while loop with the nested if ought to stop prior to the *. Can someone point out what I'm doing wrong and why I'm having this issue?
void conversion(string romanIn)
{
length=0;
romanIn.append("*");
while(item!="*")
{
if(item != "*")
{
item = romanIn[length];
length++;
}
cout<<item;
}
you are naturally going to get a +1 the first time through the loop because you aren't initializing the variable "item". Also make it a do while instead of a while loop.
Try this:
do
{
// This line moves out of the if statement
item = romanIn[length];
if(item != "*")
{
length++;
}
cout<<item;
}while(item!="*")
What is the initial value of item?
Let's assume it's 0. You enter the loop
item == 0 != marker, so you enter the if as well, and you say
item = romanIn[0], length++
If romanIn[0] == "*" you will exit the loop, but your length now says 1 which includes the marker

It is safe to use i+1 in O(n) searching?

I'm asking myself if it is safe to use i+1 to check if the next number is the same as current... Example :
int search(int el, int* a, int n) {
if(a == NULL && n<0)
{
return NULL;
}
for(int i=0; i<n; i++)
{
if((a[i] == el )&& (a[i+1] != el)) // here I check next element
{
return i; // if I find it, return a position.
}
}
return -1; // else return -1
}
If we have an array with length 4, then a[i+1] will be passed an array length, but program will still work.
No, accessing elements out of bounds is Undefined Behavior.
Your program may "seem" to work, but you cannot expect anything out of it. It could work on your machine, and crash on another one. Or it could work on all machines now, but not tomorrow. You should check if the index is out-of-bounds before using the subscript operator.
You may consider changing your cycle so that it never accesses out-of-bounds elements:
for (int i = 0; i < n - 1; i++)
// ^^^^^
Then, you would have to take care of the last element in the array separately, as a special case.
That's not correct, when i reaches its last value (n-1) you check the value of a nonexistent element (the C standard allows you to have a pointer to one-after-last element, but not to dereference it).
You can fix your code like this:
if((a[i] == el ) && ((i == n-1) || (a[i+1] != el)))
If n is the last element in the array, then i + 1 is safe in this case. If n is the number of elements in the array, i + 1 may appear to work most of the time, but it is not safe.
In that case, you're accessing an element outside the bounds of the array, which may do anything from giving you incorrect results to crashing your program. Most of the time it will appear to work, though, making the problem very hard to debug when it happens.
It is not clear to me what you're asking. Even if array access is expensive, accessing both a[i] and a[i+1] remains of O(N) complexity. What you can't do is adding complexity that's based on i (for example an additional loop from 0 to i), or modifying i (for example decrementing it based on some condition).
The problem, as others have already pointed out, is that the last element is compared to a nonexistent datum: either you'll get an error and a possible crash, or the program will appear to be working - and actually work most of the time - until the time where that unknown and possibly random last-and-one item will trigger the comparison, and yield an unexpected result.
You should check that the array size is at least 1 (that ought to be a special case anyway, can't run a compare on a single element!) and then loop only up to n-1. Or you could save the previous value in a temporary variable; depending on the platform, this will be a (possibly much faster) register, or a (possibly much slower) stack location. In most cases I'd just state my intention of comparing with the next element, as you did, and let the compiler sort it out.
No.
int main(void)
{
char pumpkin[8];
int a[4];
int i, p;
a[0] = 3760;
a[1] = 100001;
a[2] = 595959;
a[3] = 1886221680;
pumpkin[0] = 'p';
pumpkin[1] = 'u';
pumpkin[2] = 'm';
pumpkin[3] = 'p';
for (i = 0; i < 4; i++) {
p = search(a[i], a, 4);
if (p >= 0)
printf("Found it at position %d: %d.\n", p, a[i]);
else
printf("Value not found.\n");
}
return 0;
}
In my machine:
$ gcc -std=c11 -o boundserror boundserror.c
$ ./boundserror
Found it at position 0: 3760.
Found it at position 1: 100001.
Found it at position 2: 595959.
Value not found.
What happened ? Compiler wrote value 1886221680 both in a[3] and where a[4] would be if it existed. About the program working on your machine: read about the works on my machine concept.
http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html

Copy elements of an old array of pointers into new array of pointers?

I need some assistance with a C++ project. What I have to do is remove the given element from an array of pointers. The technique taught to me is to create a new array with one less element and copy everything from the old array into the new one except for the specified element. After that I have to point the old array towards the new one.
Here's some code of what I have already:
I'm working with custom structs by the way...
Data **values = null; // values is initialized in my insert function so it is
// populated
int count; // this keeps track of values' length
bool remove(Data * x) {
Data **newArray = new Data *[count - 1];
for (int i = 0; i < count; i++) {
while (x != values[i]) {
newArray[i] = values[i];
}
count -= 1;
return true;
}
values = newArray;
return false;
}
So far the insert function works and outputs the populated array, but when I run remove all it does is make the array smaller, but doesn't remove the desired element. I'm using the 0th element every time as a control.
This is the output I've been getting:
count=3 values=[5,6,7] // initial insertion of 5, 6, 7
five is a member of collection? 0
count=3 values=[5,6] // removal of 0th element aka 5, but doesn't work
five is a member of collection? 0
count=4 values=[5,6,5] // re-insertion of 0th element (which is stored in
five is a member of collection? 0 // my v0 variable)
Could anyone nudge me in the right direction towards completing this?
First of all, your code is leaking memory like no good! Next you only copy the first element and not even that if the first element happens to be the one you want to remove. Also, when you return from your function, you haven't changed your internal state at all. You definitely want to do something along the lines of
Data** it = std::find(values, values + count, x);
if (it != values + count) {
std::copy(it + 1, values + count, it);
--count;
return true;
}
return false;
That said, if anybody taught you to implement something like std::vector<T> involving reallocations on every operation, it is time to change schools! Memory allocations are relatively expensive and you want to avoid them. That is, when implementing something like a std::vector<T> you, indeed, want to implement it like a std::vector<T>! That is you keep an internal buffer of potentially more element than there are and remember how many elements you are using. When inserting a new element, you only allocate a new array if there is no space in the current array (not doing so would easily result in quadratic complexity even when always adding elements at the end). When removing an element, you just move all the trailing objects one up and remember that there is one less object in the array.
Try this:
bool remove(Data * x)
{
bool found = false;
// See if x is in the array.
for (int i = 0; i < count; i++) {
if (x != values[i]) {
found = true;
break;
}
}
if (!found)
{
return false;
}
// Only need to create the array if the item to be removed is present
Data **newArray = new Data *[count - 1];
// Copy the content to the new array
int newIndex = 0;
for (int i = 0; i < count; i++)
{
if (x != values[i])
newArray[newIndex++] = values[i];
}
// Now change the pointers.
delete[] values;
count--;
values = newArray;
return true;
}
Note that there's an underlying assumption that if x is present in the array then it's there only once! The code will not work for multiple occurrences, that's left to you, seeing as how this is a school exercise.