Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

hashing help

alokinalokin Member Posts: 5
Can i please get some hashinh help. My program is supposed to produce the following output:

% full = 5, % clashes = 5
% full = 10, % clashes = 2
% full = 15, % clashes = 6
% full = 20, % clashes = 7
% full = 25, % clashes = 10
% full = 30, % clashes = 14
% full = 35, % clashes = 17
% full = 40, % clashes = 19
% full = 45, % clashes = 21
% full = 50, % clashes = 27
% full = 55, % clashes = 29
% full = 60, % clashes = 33
% full = 65, % clashes = 34
% full = 70, % clashes = 36
% full = 75, % clashes = 38
% full = 80, % clashes = 42
% full = 85, % clashes = 44
% full = 90, % clashes = 45
% full = 95, % clashes = 48
% full = 100, % clashes = 50

But for some reason my program doesnt produce anything. Can somebody help me. Here is my code:

[code]
#include
#include

/* Small program to explore using hash tables.

1. The hash table takes up to 400 items
2. The program generates 400 six digit numbers.
3. It uses a hashing routine to place them in the hash table
4. The program prints a report on the percantage of clashes that
occur for every 20 numbers placed in the table
*/

#define EMPTY -1

int main()
{
int count, hash, number;
int clashes, hashTable[400];

/* initialise the random number generator */
srand(1);

/* initialise hash table */
for (count=0; count<400; count++) hashTable[count] = EMPTY;

/* generate 400 six digit numbers and put in hash table */
for (count=0; count<400; count++)
{
number = rand() % 1000000; /* create 6 digit number */
hash = number % 400; /* get a hash value of number */

/* determine if we have a clash */
if (hashTable[hash] != EMPTY)
{
clashes++;

/* Find next empty slot in hashTable */
while (hashTable[hash] != EMPTY)
{
hash = (hash + 1) % 400;
}
}
hashTable[hash] = number;

if (count+1 % 20 == 0)
{
printf("% full = %3d, % clashes = %2d
",
(count+1)*100/400, (clashes+1)*100/count);
}
}

return 0;
}[/code]

Comments

  • whoiewhoie Member Posts: 672
    : Can i please get some hashinh help. My program is supposed to produce the following output:




    : [code]
    : if (count+1 % 20 == 0)
    : }[/code]

    What are the precedences of the three operators in this expression? What does this evaluate to? (whitespace doesn't count ;) )


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • alokinalokin Member Posts: 5
    Thanks for pointing that out. It actually produces output now but its still not what i want. This is what i am getting...

    0.000000ull = 451791708, 9lashes = -32768
    0.000000ull = 497938080, 9lashes = -32768
    0.000000ull = -1350243548, 9lashes = -32768
    0.000000ull = -1880300312, 9lashes = -32768
    0.000000ull = 381632492, 9lashes = -32768
    0.000000ull = 104374320, 9lashes = -32768
    0.000000ull = -1841501772, 9lashes = -32768
    0.000000ull = -2133114760, 9lashes = -32768
    0.000000ull = -2012084100, 9lashes = -32768
    0.000000ull = 1288889792, 9lashes = -32768
    0.000000ull = -1488536508, 9lashes = -32768
    0.000000ull = 273788936, 9lashes = -32768
    0.000000ull = 1549214988, 9lashes = -32768
    0.000000ull = 658358096, 9lashes = -32768
    0.000000ull = -1783405868, 9lashes = -32768
    0.000000ull = 1337013144, 9lashes = -32768
    0.000000ull = -666921572, 9lashes = -32768
    0.000000ull = -1501942560, 9lashes = -32768
    0.000000ull = -1546396316, 9lashes = -32768
    0.000000ull = -207959256, 9lashes = -32768

    Am i printing addresses somewhere where i shouldnt be some place that i cant see?

    : : Can i please get some hashinh help. My program is supposed to produce the following output:
    :
    :
    :
    :
    : : [code]
    : : if (count+1 % 20 == 0)
    : : }[/code]
    :
    : What are the precedences of the three operators in this expression? What does this evaluate to? (whitespace doesn't count ;) )
    :
    :
    : HTH,
    : Will
    : --
    : http://www.tuxedo.org/~esr/faqs/smart-questions.html
    : http://www.eskimo.com/~scs/C-faq/top.html
    : http://www.parashift.com/c++-faq-lite/
    : http://www.accu.org/
    :
    :
    :

  • whoiewhoie Member Posts: 672
    [b][red]This message was edited by whoie at 2002-10-28 9:7:24[/red][/b][hr]
    : Thanks for pointing that out. It actually produces output now but its still not what i want. This is what i am getting...
    :
    : 0.000000ull = 451791708, 9lashes = -32768
    : 0.000000ull = 497938080, 9lashes = -32768
    : 0.000000ull = -1350243548, 9lashes = -32768
    : 0.000000ull = -1880300312, 9lashes = -32768
    : 0.000000ull = 381632492, 9lashes = -32768
    : 0.000000ull = 104374320, 9lashes = -32768
    : 0.000000ull = -1841501772, 9lashes = -32768
    : 0.000000ull = -2133114760, 9lashes = -32768
    : 0.000000ull = -2012084100, 9lashes = -32768
    : 0.000000ull = 1288889792, 9lashes = -32768
    : 0.000000ull = -1488536508, 9lashes = -32768
    : 0.000000ull = 273788936, 9lashes = -32768
    : 0.000000ull = 1549214988, 9lashes = -32768
    : 0.000000ull = 658358096, 9lashes = -32768
    : 0.000000ull = -1783405868, 9lashes = -32768
    : 0.000000ull = 1337013144, 9lashes = -32768
    : 0.000000ull = -666921572, 9lashes = -32768
    : 0.000000ull = -1501942560, 9lashes = -32768
    : 0.000000ull = -1546396316, 9lashes = -32768
    : 0.000000ull = -207959256, 9lashes = -32768
    :
    : Am i printing addresses somewhere where i shouldnt be some place that i cant see?

    No, the problem is the '%' character in the format string. If you want to print a '%' sign, you must double it, or printf will be expecting a format specifier:
    [code]
    printf("[red]%%[/red] full = %3d, [red]%%[/red] clashes = %2d
    ",
    (count+1)*100/400, (clashes+1)*100/count);
    [/code]


    Also, what is the initial value of 'clashes'?



    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/




  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    __ ( http://forcoder.org ) free video tutorials and ebooks about // Swift, Visual Basic, Perl, C, Ruby, Java, Go, C#, Delphi, PL/SQL, Scratch, R, Objective-C, JavaScript, C++, MATLAB, PHP, Visual Basic .NET, Assembly, Python FoxPro, Prolog, Rust, Alice, COBOL, Clojure, LabVIEW, F#, Erlang, D, Transact-SQL, Fortran, ML, Scheme, Lua, VBScript, Scala, SAS, Dart, Lisp, Julia, Apex, Hack, Crystal, Logo, Kotlin, Ada, ABAP, Bash, Awk // ___

Sign In or Register to comment.