Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > nebie : what is the problem with this one?

Reply
Thread Tools

nebie : what is the problem with this one?

 
 
avi
Guest
Posts: n/a
 
      06-08-2005
/* 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);
}

 
Reply With Quote
 
 
 
 
Sam
Guest
Posts: n/a
 
      06-08-2005
> 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.


 
Reply With Quote
 
 
 
 
those know me already won't ask my name
Guest
Posts: n/a
 
      06-08-2005


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

 
Reply With Quote
 
Sam
Guest
Posts: n/a
 
      06-08-2005
>> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nebie Cisco 3845 Router Question chazman Hardware 1 02-04-2011 05:38 AM
nebie problem with c# asp.net syntax.... DC ASP .Net 2 05-11-2005 03:51 PM
Nebie: All-in-One Gestures how to use? lbbss Computer Support 1 08-17-2004 08:52 PM
Nebie to Firefox: Seconday window will not open lbbss Computer Support 3 08-17-2004 06:13 PM
Nebie basic questions re: DVD+R burning lbbs Computer Support 1 01-05-2004 08:34 PM



Advertisments