nebie : what is the problem with this one?

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

  1. avi

    avi Guest

    /* 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
    #1
    1. Advertising

  2. avi

    Sam Guest

    > 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
    #2
    1. Advertising

  3. 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
    #3
  4. avi

    Sam Guest

    >> 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
    #4
    1. Advertising

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.
Similar Threads
  1. DC
    Replies:
    2
    Views:
    483
  2. news.frontiernet.net
    Replies:
    6
    Views:
    1,129
    news.frontiernet.net
    Apr 16, 2004
  3. loveNUNO
    Replies:
    2
    Views:
    926
    loveNUNO
    Nov 20, 2003
  4. Prophet

    nebie question - making page searchable

    Prophet, Aug 3, 2005, in forum: Javascript
    Replies:
    3
    Views:
    79
  5. Dave Schwimmer
    Replies:
    11
    Views:
    195
Loading...

Share This Page