/* 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);
} 3 1012
> 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.
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
>> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Prophet |
last post by:
Hi,
I have a new web page and it is posted on yahoo.
I would like to make it so that a person can do a search and find my pace.
How do I do this???
|
by: varungupta |
last post by:
Hi Group members !
We are hosting a Apache/PHP/MySql based website at our college since
January,2005. It was working fine since then. The web server is plaed
in our college itself and we are...
|
by: darren |
last post by:
Hi
whell with alot of help from websites and the peoiple on this group
i have just about finished my small program i just have one question
i have include a com component " windows script Host...
|
by: Ammar |
last post by:
Dear All,
I'm facing a small problem.
I have a portal web site, that contains articles, for each article, the end
user can send a comment about the article.
The problem is:
I the comment length...
|
by: DC |
last post by:
Why in the hell am i getting this error?
Compiler Error Message: CS1519: Invalid token 'using' in class,
struct, or interface member declaration
Source Error:
Line 5: <script...
|
by: K.K. |
last post by:
Hi all,
I'm wondering is it possible to retrieve (or save) data from database and
display it in a calender format? If it can be done can you post a sample
code or point me to some web site with...
|
by: B.D. |
last post by:
Can anyone explain way the transformation to upper case doesn't work
correctly in the following code if PROBLEM is defined but works correctly if
it's not defined? I'm using VC 7.1
#include...
|
by: Dave Schwimmer |
last post by:
Is it possible to 'hide' javascript from a user. I am thinking of
putting some fairly proprietary logic client side (to release burden on
server) - but I dont want to make the source freely...
|
by: Rinaldo |
last post by:
Hi,
When I start my program in the debugger, there is no problem, but when not I
get an exception.
It appears in:
private void Upload(string filename, string FTnaam)
{
MessageBox.Show("in...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |