problem: a list represented using cursor based implementation contains all ACT, BSCS, BSICT, BSIT and BSMATH students. A dictionary is created implemented using open hashing for BSCS and BSIT students. Each grouping the dictionary is a list implemented using cursor based. given below is the definition of the LIST, Dictionary, Virtual heap.

#define maxCells 1000
#define maxGrp 10
typedef struct
char LN[16], FN[24],MI;

typedef struct studtype
unsigned long ID;
nametype name;
char course[8];
int yr;

typedef struct ctype
student stud;
int next;

typedef int LIST;/*LIST is implemented using cursor base*/

typedef struct
LIST Header; /*holds the index of the first element in each group*/
int StudCtr; /*holds the number of elements in each group*/

typedef Group * Dictionary;

typedef struct
celltype heap[maxCells];
int AvailPtr;/*holds the index to the next available cell in the virtual heap*/

problem 1:
write the code of function Hash. the function will return the remainder of when the total sum of all digits of the ID will be divided by maxGrp.
here's my answer:

int Hash(celltype x)
 int sum;
 unsigned long ID, i;
    sum += ID/i;
 return (sum%maxGrp);

my teacher said that the celltype in the parameter is wrong and also sum and i%=10 but he didn't explain why i tried to run it and yes he was right but i don't understand what is wrong so please explain why that is.

celltype is wrong because the function is hashing the student ID. The list is irrelevant to that task. x should have a type of student instead.

Concerning sum, first and foremost it must be initialized. The += operator uses the current value within sum and adds to it, if sum is uninitialized, the result is unpredictable.

Next, modulo is used to get the least significant digit and division removes it. Also, the easiest way to stop a loop that is removing digits is to stop when the number reaches 0. This way the number of digits in the ID no longer matter. If another digit is added, the code does not need to change.

int Hash(student x)
    unsigned long ID = x.ID;
    int sum = 0;
    while (ID) {
        sum += ID % 10;
        ID /= 10;
    return (sum % maxGrp);
This article has been dead for over six months. Start a new discussion instead.