Hello, I was wondering if I could get this code explained to me. Also if there w
ID: 3710684 • Letter: H
Question
Hello, I was wondering if I could get this code explained to me. Also if there was anything incorrect, if it could be fixed. If a sample output is possible, can it be done? Thank you!#include <stdio.h> #include <stdlib.h> #define MAXSTRING 100 /* * USAGE: * gcc hashHarness.c -o hashX * ./hashX < inputFile.txt */
/* *djb2 * this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. * another version of this algorithm (now favored by bernstein) uses * xor: hash(i) = hash(i - 1) * 33 ^ str[i]; * the magic of number 33 (why it works better than many other constants, prime or not) * has never been adequately explained. * * ref:http://www.cse.yorku.ca/~oz/hash.html */ unsigned long hashDJB2(char *str) { unsigned long hash = 5381; int c;
while ((c = *str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash; }
/* * K&R's simple but ... * This hash function appeared in K&R (1st ed) but at least the reader was warned: * "This is not the best possible algorithm, but it has the merit of extreme simplicity." * This is an understatement; It is a terrible hashing algorithm, and it could have been * much better without sacrificing its "extreme simplicity." [see the second edition!] * Many C programmers use this function without actually testing it, or checking something * like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise * respectable code, eg. cnews. sigh. * */
unsigned long hashKR(char *str) { unsigned int hash = 0; int c;
while ((c = *str++)) hash += c;
return hash; }
int main(int argc, char *argv[]) {
char *buffer; int bufferlen = MAXSTRING; unsigned long newHash; unsigned long newKRhash; buffer = malloc(MAXSTRING); if (buffer == NULL) { printf("Bad malloc bye! "); return 1; } while (fgets(buffer, bufferlen, stdin)) { // printf("data in:%s", buffer); if (ferror(stdin)) { printf("Error on stdin "); break; } /* * Hash 'em up - commences! */ newHash = hashDJB2(buffer); newKRhash = hashKR(buffer); printf("hash:%016lx:%016lx:%s", newHash, newKRhash, buffer); } printf(" Done! "); return 0; }
Hello, I was wondering if I could get this code explained to me. Also if there was anything incorrect, if it could be fixed. If a sample output is possible, can it be done? Thank you!
#include <stdio.h> #include <stdlib.h> #define MAXSTRING 100 /* * USAGE: * gcc hashHarness.c -o hashX * ./hashX < inputFile.txt */
/* *djb2 * this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. * another version of this algorithm (now favored by bernstein) uses * xor: hash(i) = hash(i - 1) * 33 ^ str[i]; * the magic of number 33 (why it works better than many other constants, prime or not) * has never been adequately explained. * * ref:http://www.cse.yorku.ca/~oz/hash.html */ unsigned long hashDJB2(char *str) { unsigned long hash = 5381; int c;
while ((c = *str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash; }
/* * K&R's simple but ... * This hash function appeared in K&R (1st ed) but at least the reader was warned: * "This is not the best possible algorithm, but it has the merit of extreme simplicity." * This is an understatement; It is a terrible hashing algorithm, and it could have been * much better without sacrificing its "extreme simplicity." [see the second edition!] * Many C programmers use this function without actually testing it, or checking something * like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise * respectable code, eg. cnews. sigh. * */
unsigned long hashKR(char *str) { unsigned int hash = 0; int c;
while ((c = *str++)) hash += c;
return hash; }
int main(int argc, char *argv[]) {
char *buffer; int bufferlen = MAXSTRING; unsigned long newHash; unsigned long newKRhash; buffer = malloc(MAXSTRING); if (buffer == NULL) { printf("Bad malloc bye! "); return 1; } while (fgets(buffer, bufferlen, stdin)) { // printf("data in:%s", buffer); if (ferror(stdin)) { printf("Error on stdin "); break; } /* * Hash 'em up - commences! */ newHash = hashDJB2(buffer); newKRhash = hashKR(buffer); printf("hash:%016lx:%016lx:%s", newHash, newKRhash, buffer); } printf(" Done! "); return 0; }
Hello, I was wondering if I could get this code explained to me. Also if there was anything incorrect, if it could be fixed. If a sample output is possible, can it be done? Thank you!
#include <stdio.h> #include <stdlib.h> #define MAXSTRING 100 /* * USAGE: * gcc hashHarness.c -o hashX * ./hashX < inputFile.txt */
/* *djb2 * this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. * another version of this algorithm (now favored by bernstein) uses * xor: hash(i) = hash(i - 1) * 33 ^ str[i]; * the magic of number 33 (why it works better than many other constants, prime or not) * has never been adequately explained. * * ref:http://www.cse.yorku.ca/~oz/hash.html */ unsigned long hashDJB2(char *str) { unsigned long hash = 5381; int c;
while ((c = *str++)) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash; }
/* * K&R's simple but ... * This hash function appeared in K&R (1st ed) but at least the reader was warned: * "This is not the best possible algorithm, but it has the merit of extreme simplicity." * This is an understatement; It is a terrible hashing algorithm, and it could have been * much better without sacrificing its "extreme simplicity." [see the second edition!] * Many C programmers use this function without actually testing it, or checking something * like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise * respectable code, eg. cnews. sigh. * */
unsigned long hashKR(char *str) { unsigned int hash = 0; int c;
while ((c = *str++)) hash += c;
return hash; }
int main(int argc, char *argv[]) {
char *buffer; int bufferlen = MAXSTRING; unsigned long newHash; unsigned long newKRhash; buffer = malloc(MAXSTRING); if (buffer == NULL) { printf("Bad malloc bye! "); return 1; } while (fgets(buffer, bufferlen, stdin)) { // printf("data in:%s", buffer); if (ferror(stdin)) { printf("Error on stdin "); break; } /* * Hash 'em up - commences! */ newHash = hashDJB2(buffer); newKRhash = hashKR(buffer); printf("hash:%016lx:%016lx:%s", newHash, newKRhash, buffer); } printf(" Done! "); return 0; }
Explanation / Answer
I fixed one issue with your code which uses fgets to read buffer. I recommend using fread instead as, unlike fgets, fread doesn't break on newline character.
Code Explanation:
The program performs following steps,
Updated code is provided below for your reference. Also sample input and sample output are provided.
File: hashHarness.c
#include <stdio.h>
#include <stdlib.h>
#define MAXSTRING 100
/*
* USAGE:
* gcc hashHarness.c -o hashX
* ./hashX < inputFile.txt
*/
/*
*djb2
* this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c.
* another version of this algorithm (now favored by bernstein) uses
* xor: hash(i) = hash(i - 1) * 33 ^ str[i];
* the magic of number 33 (why it works better than many other constants, prime or not)
* has never been adequately explained.
*
* ref:http://www.cse.yorku.ca/~oz/hash.html
*/
unsigned long hashDJB2(char * str) {
unsigned long hash = 5381;
int c;
while ((c = * str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
/*
* K&R's simple but ...
* This hash function appeared in K&R (1st ed) but at least the reader was warned:
* "This is not the best possible algorithm, but it has the merit of extreme simplicity."
* This is an understatement; It is a terrible hashing algorithm, and it could have been
* much better without sacrificing its "extreme simplicity." [see the second edition!]
* Many C programmers use this function without actually testing it, or checking something
* like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise
* respectable code, eg. cnews. sigh.
*
*/
unsigned long hashKR(char * str) {
unsigned int hash = 0;
int c;
while ((c = * str++))
hash += c;
return hash;
}
int main(int argc, char * argv[]) {
char * buffer;
int bufferlen = MAXSTRING - 1;
int readlen;
unsigned long newHash;
unsigned long newKRhash;
buffer = malloc(MAXSTRING);
if (buffer == NULL) {
printf("Bad malloc bye! ");
return 1;
}
while ((readlen = fread(buffer, 1, bufferlen, stdin))) {
buffer[readlen] = '';
// printf("data in:%s", buffer);
if (ferror(stdin)) {
printf("Error on stdin ");
break;
}
/*
* Hash 'em up - commences!
*/
newHash = hashDJB2(buffer);
newKRhash = hashKR(buffer);
printf("hash:%016lx:%016lx:%s ", newHash, newKRhash, buffer);
}
printf("Done! ");
return 0;
}
Sample Input File: inputFile.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
Sample Output:
$ gcc hashHarness.c -o hashX
$ ./hashX < inputFile.txt
hash:7b4e58720bb1215e:00000000000024f9:Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore
hash:7cb1451eaf9750a4:000000000000245f: et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
hash:1bbddc1dbc36fafa:00000000000024b5: aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse ci
hash:4d985c22f845f0e7:0000000000002502:llum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa
hash:b8acee32d6f29d43:00000000000023fe: qui officia deserunt mollit anim id est laborum.
Sed ut perspiciatis unde omnis iste natus error
hash:743d5f9796e0f985:0000000000002560:sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo invent
hash:7baa44e503428a49:0000000000002524:ore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem qui
hash:ec262c5d42b84472:000000000000252d:a voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione v
hash:31fe6f00da8db032:000000000000252d:oluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consecte
hash:14b5a19a63533635:0000000000002470:tur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam ali
hash:b36aea596c1cb0b7:0000000000002592:quam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis susci
hash:aa389c056948e8e8:00000000000024e3:pit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qu
hash:f2f1b0fbaa98947b:00000000000024f6:i in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo
hash:387eb99693b36890:00000000000009ab: voluptas nulla pariatur?
Done!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.