# nebie : what is the problem with this one?

Discussion in 'C Programming' started by avi, Jun 8, 2005.

1. ### aviGuest

/* NumberGame.cpp : to find duplicates in a array */

#include "stdafx.h"

bool findDuplicates(int *, int);

typedef struct bst{
struct bst *left;
struct bst *right;
int val;
}bst;

bool insertv(bst*, int);

int _tmain(int argc, _TCHAR* argv[])
{
int iArrayhasd[]={1,2,3,2,14};

int size = sizeof iArrayhasd/sizeof iArrayhasd[0];

bool ans = findDuplicates(iArrayhasd, size);

return 0;
}

bool findDuplicates(int *arr, int size)
{
bool found = false;

bst *root;

for(int i=0;i<size;i++)
{
found = insertv(root,*(arr+i));

if (found)
{
printf("Duplocate value : %d", *(arr+i));
}

}

return found;
}

bool insertv(bst*root1, int cal)
/* I know that root1 is getting pass by value - what is the sol? */
{

if (root1 == NULL)
{
root1 = (bst*)malloc(sizeof(bst));
root1->left = NULL;
root1->right = NULL;
root1->val = cal;
return false;
}

if (root1->val == cal)
return true;

if (root1->val < cal)
return insertv(root1->right,cal);
else
return insertv(root1->left,cal);
}

avi, Jun 8, 2005

2. ### SamGuest

> bst *root;
>
> for(int i=0;i<size;i++)
> {
> found = insertv(root,*(arr+i));

The problem is, the value of root is not equal to NULL at this point. You
may need to give it an initial value NULL.

Sam, Jun 8, 2005

3. ### those know me already won't ask my nameGuest

avi wrote:
> /* NumberGame.cpp : to find duplicates in a array */
>
> #include "stdafx.h"
>
> bool findDuplicates(int *, int);
>
> typedef struct bst{
> struct bst *left;
> struct bst *right;
> int val;
> }bst;
>
> bool insertv(bst*, int);
>
> int _tmain(int argc, _TCHAR* argv[])
> {
> int iArrayhasd[]={1,2,3,2,14};
>
> int size = sizeof iArrayhasd/sizeof iArrayhasd[0];
>
> bool ans = findDuplicates(iArrayhasd, size);
>
> return 0;
> }
>
> bool findDuplicates(int *arr, int size)
> {
> bool found = false;
>
> bst *root;
>
> for(int i=0;i<size;i++)
> {
> found = insertv(root,*(arr+i));
>
> if (found)
> {
> printf("Duplocate value : %d", *(arr+i));
> }
>
> }
>
> return found;
> }
>
> bool insertv(bst*root1, int cal)
> /* I know that root1 is getting pass by value - what is the sol? */

redefine the function as

bool insertv(bst**root1, int cal)

{

if (*root1 == NULL)
{
*root1 = (bst*)malloc(sizeof(bst));
(*root1)->left = NULL;
*root1->right = NULL;
(*root1)->val = cal;
return false;
}

if ((*root1)->val == cal)
return true;

if ((*root1)->val < cal)
return insertv(&(*root1)->right,cal);
else
return insertv(&(*root1)->left,cal);
}

and rewrite function as

bool findDuplicates(int *arr, int size)
{
bool found = false;

bst *root = 0;

for(int i=0;i<size;i++)
{
found = insertv(&root,*(arr+i));

if (found)
{
printf("Duplocate value : %d", *(arr+i));
}

}

return found;
}

hope it help u

baumann@pan

those know me already won't ask my name, Jun 8, 2005
4. ### SamGuest

>> bst *root;
>>
>> for(int i=0;i<size;i++)
>> {
>> found = insertv(root,*(arr+i));

> The problem is, the value of root is not equal to NULL at this point. You
> may need to give it an initial value NULL.

It seems that my suggestion is wrong, as the insertv function is supposed to
create a list when root is equal to NULL.

Sam, Jun 8, 2005

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.