B
ben
this programme from sedgewick's 'algorithms in c parts 1-4' "is a
sample client program that ... uses a symbol table to find the distinct
values in a sequence of keys (randomly generated or read from standard
input), then prints them out in sorted order."
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
#include "st.h"
void main(int argc, char *argv[])
{
int n, maxn = atoi(argv[1]), sw = atoi(argv[2]);
Key v;
Item item;
stInit(maxn);
for( n = 0; n < maxn; n++ ) {
if( sw )
v = itemRand();
else if( itemScan(&v) == EOF )
break;
if( stSearch(v) != NULL )
continue;
key(item) = v;
stInsert(item);
}
stSort(itemShow);
printf("\n%d keys %d distinct keys\n", n, stCount() );
}
it crashes -- Bus Error -- when there's no input on the command line.
when some arguments are given it reliably gives incorrent, rubbish
results but that's probably a problem in another part of the code not
shown.
from the code shown, it's not at all surprising that it goes wrong when
no arguments are given is it? i'm not sure if i should be changing the
code as the above code is exactly how it is in the book, but it should
be checking if argv[1] exists before using it shouldn't it?, and if it
doesn't exist then the random part should be called? or is the problem
something else, maybe not in the above code? i don't know. how does
anyone think it should be?
thanks, ben.
sample client program that ... uses a symbol table to find the distinct
values in a sequence of keys (randomly generated or read from standard
input), then prints them out in sorted order."
#include <stdio.h>
#include <stdlib.h>
#include "item.h"
#include "st.h"
void main(int argc, char *argv[])
{
int n, maxn = atoi(argv[1]), sw = atoi(argv[2]);
Key v;
Item item;
stInit(maxn);
for( n = 0; n < maxn; n++ ) {
if( sw )
v = itemRand();
else if( itemScan(&v) == EOF )
break;
if( stSearch(v) != NULL )
continue;
key(item) = v;
stInsert(item);
}
stSort(itemShow);
printf("\n%d keys %d distinct keys\n", n, stCount() );
}
it crashes -- Bus Error -- when there's no input on the command line.
when some arguments are given it reliably gives incorrent, rubbish
results but that's probably a problem in another part of the code not
shown.
from the code shown, it's not at all surprising that it goes wrong when
no arguments are given is it? i'm not sure if i should be changing the
code as the above code is exactly how it is in the book, but it should
be checking if argv[1] exists before using it shouldn't it?, and if it
doesn't exist then the random part should be called? or is the problem
something else, maybe not in the above code? i don't know. how does
anyone think it should be?
thanks, ben.