
Patrick B. answered 02/04/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
int hash ( char key[], int * listSize)
{
int i,sum;
sum=0; //must initialize the running total to zero
int n=strlen(key); //use local var to store the string length
// rather than calling the function each time thru the loop
for (i=0; i<n; i++)
{
sum += key[i];
}
return ( sum /(*listSize)); //must DE-REFERENCE the pointer; also Sum is mis-spelled.. typo?
}
int main()
{
char key[6];
strcpy(key,"HELLO"); // total of these ASCII values is H+E+L+L+O = 72+69+76+76+79 = 372
int listSize=25;
//using the AMPERSAND & to pass the ADDRESS of the listSize var
int hashVal = hash(key,&listSize); //372/25 = 14
printf(" hash value is %d \n",hashVal);
}