logo

Crowdly

Browser

Add to Chrome

A hash function is a function that maps any arbitrary data into a fixed-size val...

✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.

A hash function is a function that maps any arbitrary data into a fixed-size value within a specified range. The values returned by a hash function are called hash values, or “hashes”. Hash functions are used in a wide variety of applications, including encryption and data integrity.

Part 1 (5 marks)

int string_hash (char* string_1, int starting_value)

Write a C function string_hash that takes in the following input arguments:

  1.  A pointer to character (string_1) representing a C string 
  2.  An integer (starting_value) greater than 0
and returns an integer according to the process outlined below:

  • Step 1: Find the smallest prime number that is greater than starting_value and stores this as the current prime value (current_prime)

  • Step 2: For each character in the string (except the terminating NULL character), calculate the product of the ASCII value of the current character and current_prime. 

  • Step 3: Add the result of the calculation in Step 2 to the accumulator value (accumulator), which is initialized as zero at the start of the function. 

  • Step 4: Calculate the sum of accumulator and current_prime, and then assign the result as the new value of accumulator.

  • Step 5: Find the next smallest prime number that is greater than the current_prime and assign this as the new value of current_prime

  • Step 6: Repeat steps 2-5 until all non-NULL characters in the string have been considered.

  • Step 7: Return the value of accumulator upon exit.

For example: string_hash("Melbourne", 5) should return a value of 20160.

Part 2 (5 marks)

Write a C code inside the main function that performs the following activities:

  • Scan a string (string_1) and integer (starting_value) as input from the user

  • Call the hash_string function written in part 1, using string_1 and starting_value as the inputs

  • Scan a string as input from the user, representing a filename

  • Opens the file and appends an new line to the file which contains string_1, starting_value, and the accumulator in the following format:

<string_1><whitespace><starting_value><whitespace>-<whitespace><accumulator>

Note:

  1. You may also use whatever editor/test environment you like to write your solution.

  2. Your code MUST be submitted as a .txt file below BEFORE the end of the test
  3. Your .txt file should be named as q8_studentID.txt, eg q8_12345678.txt

  4. You should use good programming practices

More questions like this

Want instant access to all verified answers on learning.monash.edu?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome