hw9-soln - ECE 209 Homework 9 Fall 2007 Homework 9 This...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE 209 Fall 2007 Homework 9 Homework 9 This homework will not be collected/graded NAME: __________________________________________________________ UNITY ID: _______________@ncsu.edu SECTION: ________________ (001/002) Print this file and answer in the space provided. Staple in the top left corner. Do not fold. 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ECE 209 Fall 2007 Homework 9 1. Write two “hash” functions for strings. (See Section 14.1.) Each maps the input string into an integer index between 0 and 26, inclusive. (a) The first hash function, called hashFirst , simply looks at the first character in the string. int hashFirst(char* s); If the first character of the string is in the range ‘A’ through ‘Z’, the return value is the distance from A. (In other words, ‘A’ = 0, ‘B’ = 1, ‘C’ = 2, …, ‘Z’ = 25. NOTE: Use arithmetic, e.g., “x - ‘A’” to compute this, not some long series of if’s or a switch statement.) If the character is in the range ‘a’ through ‘z’, the return value is the distance from ‘a’. If the first character is not a letter, then return 26. int hashFirst(char *s) { char firstChar = *s; if ((firstChar >= ‘A’) && (firstChar <= ‘Z’)) return firstChar - ‘A’; else if ((firstChar >= ‘a’) && (firstChar <= ‘z’)) return firstChar - ‘a’; else return 26; } 2
Background image of page 2
ECE 209 Fall 2007 Homework 9 (b) The second hash function, called hashXOR , uses the entire string for the computation. int hashXOR(char* s); The hash is computed by XORing all the characters together, and then return the result modulo 27. Example: The hash of “abc” would be: (‘a’ ‘b’ ‘c’) mod 27 = (0x61 0x62 0x63) mod 27 = 0x60 mod 27 = 15. This is similar to, but not the same as, Program 14.1. NOTE: Remember that the XOR operator is ^, and the modulo operator is %. It doesn’t matter whether the null character at the end is included in the computation, because anything XORed with zero is itself. int hashXOR(char *s) { int h = 0; while (*s != ‘\0’) { h = h ^ *s++; } return h % 27; } 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ECE 209 Fall 2007 Homework 9 2. Write a program that reads in a text file (named on the command line), computes the hash of each word using both hashFirst
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/24/2008 for the course ECE 209 taught by Professor Tuck during the Spring '08 term at N.C. State.

Page1 / 9

hw9-soln - ECE 209 Homework 9 Fall 2007 Homework 9 This...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online