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;
}nametype;

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

typedef struct ctype
{
student stud;
int next;
}celltype;

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*/
}Group;

typedef Group * Dictionary;

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

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.

``````int Hash(celltype x)
{
int sum;
unsigned long ID, i;
ID=x.stud.ID;
for(i=100000;i>0;i%=10)
{
sum += ID/i;
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);
}``````