#include<stdio.h>
#include<math.h>
void insert(int *,int);
int main(int argc, char argv[])
{
int tree[1000];
memset(tree,'\0',1000);
int i=1;
while (!argv[i])
{
insert(tree,atoi(argv[i]));
}
int depth=0;
printf("Enter depth");
scanf("%d",&depth);
int x=pow(2,depth);
int y=2x-1;
int count=0;
for(;x<=y;x++)
{
if((tree[x]!=NULL) && (tree[2x+1]==NULL) && (tree[2x]==NULL))
{
count++;
}
}
printf("Number of leaf nodes is %d", count);
}
void insert(int *tree,int gmail)
{
int i=1;
if(tree[i]==NULL)
{
tree[i]=gmail;
}
else
{
if(gmail>tree[i])
{
insert(tree[2i+1],gmail);
}
else
insert(tree[2i],gmail);
}
}
You can't just write this:
insert(tree[2i+1],gmail);
You need to provide the multiplication operation explicitly, ie: [2*i+1]
or [2*i]
.
Also, as tree[2*i0]
is an int
, not an int*
, you'll need it's address:
insert(&(tree[2*i+1]),gmail);
and:
insert(&(tree[2*i]),gmail);
Alternatively, you could use:
insert(tree + (2*i), gmail);
The other error is the pow
call. pow returns a double value, not int, so you'll need to cast back to an int:
int x = (int) pow(2.0, depth);
Once you do get it to compile, you'll want to fix this:
while (!argv[i])
{
insert(tree,atoi(argv[i]));
}
This will either 1) not execute at all, or 2) give an infinite loop. also, if (!argv[1])
is equivalent to if (argv[i] == NULL)
, so you're trying to read a value if an only if no value is present to read.
#include<stdio.h>
#include<math.h>
void insert(int *,int);
int main(int argc, char argv[])
{
int tree[1000];
memset(tree,'\0',1000);
int i=1;
while (!argv[i])
{
insert(tree,atoi(argv[i]));
}
int depth=0;
printf("Enter depth");
scanf("%d",&depth);
int x=pow(2.0,depth);
int y=2*x-1;
int count=0;
for(;x<=y;x++)
{
if((tree[x]!=NULL) && (tree[2*x+1]==NULL) && (tree[2*x]==NULL))
{
count++;
}
}
printf("Number of leaf nodes is %d", count);
}
void insert(int *tree,int gmail)
{
int i=1;
if(tree[i]==NULL)
{
tree[i]=gmail;
}
else
{
if(gmail>tree[i])
{
insert(tree,tree[2*i+1]);
}
else
insert(tree,tree[2*i]);
}
}
this code has work,only in my compiler the memset function has not recognize what is this function ?
It looks like you are using 2i
and 2x
as a subscript into an array. This is not valid the way you are using it. Try replacing occurrences of 2i
with 2 * i
and 2x
with 2 * x
.
2i+1
isn't valid. try (2*i) + 1
2i
and 2x
are not valid expressions, you should put 2*i
and 2*x
!
Next time post the line where it doesn't compile and some explanations, it will be more easy to read..
精彩评论