nebie : what is the problem with this one?

A

avi

/* 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);
}
 
S

Sam

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.
 
T

those know me already won't ask my name

avi said:
/* 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
 
S

Sam

bst *root;
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top