Sierra_ONE

EXERCISE

Sep 18th, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | Source Code | 0 0
  1. 1. Hash Function 1: Return the digit in the hundreds place of an integer 𝑥
  2.  
  3. int hash1(int x) {
  4.     // Extract the hundreds digit by dividing by 100 and taking modulo 10
  5.     return (x / 100) % 10;
  6. }
  7.  
  8. 2. Hash Function 2: Sum of all digits of an integer x, return the remainder when divided by 19
  9.  
  10. int hash2(int x) {
  11.     int sum = 0;
  12.    
  13.     // Calculate the sum of the digits
  14.     while (x > 0) {
  15.         sum += x % 10;  // Add the last digit
  16.         x /= 10;        // Remove the last digit
  17.     }
  18.    
  19.     // Return the remainder when sum is divided by 19
  20.     return sum % 19;
  21. }
  22.  
  23. 3. Hash Function 3: Sum of ASCII values of the letters in a name, return the remainder when divided by 49
  24.  
  25. #include <string.h>  // Required for string operations
  26.  
  27. int hash3(char name[]) {
  28.     int sum = 0;
  29.     int length = strlen(name);  // Get the length of the string
  30.    
  31.     // Sum the ASCII values of all characters in the string
  32.     for (int i = 0; i < length; i++) {
  33.         sum += (int)name[i];  // Add ASCII value of each character
  34.     }
  35.    
  36.     // Return the remainder when sum is divided by 49
  37.     return sum % 49;
  38. }
  39.  
Tags: dsa
Advertisement
Add Comment
Please, Sign In to add comment