Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hash Assignment In this assignment, you will consider the problem of organizing

ID: 3817824 • Letter: H

Question

Hash Assignment

In this assignment, you will consider the problem of organizing a collection of computer user-ids and passwords. Each time a user logs in to the system by entering his or her user-id and a secret password, the system must check the validity of this user-id and password to verify that this is a legitimate user.

Because this validation must be done many times each day, it is necessary to structure this information in such a way that it can be searched rapidly. Moreover, this must be a dynamic structure because new users are regularly added to the system and some users deleted from the system.

Upon execution of your program, it should first read the user-ids and passwords from a file and create a hash using the user-id as a key (assume unique user-ids for convenience). Note: Initially it will be empty.

Once the hash has been built, it should display the following menu:

(1) Add new user

(2) Delete user

(3) Verify user

(4) Print users

(5) Quit

Option (1) and (2) simply add/delete new/existing users.

When option (3) is selected, the user is supposed to enter a user-id and a password.

Then, you should search the tree and a print message like "Valid User" or "Invalid User".

When option (4) is selected, the users and their passwords should be printed out in alphabetical order.

Finally, when option (5) is selected, the elements of hash should be stored to a file and execution should be terminated.

Test all options.

Print out File.

Write your conclusions.

Include code.

Screen prints of successful execution.

PreviousNext

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define SIZE 30

struct UserItem{
   char data[10];  
   int key;
};

struct UserItem* hashArray[SIZE];
struct UserItem* dummyItem;
struct UserIntem* item;

int hashCode(int key) {
   return key % SIZE;
}

struct UserItem *search(int key) {
   int hashIndex = hashCode(key);
   while(hashArray[hashIndex] != NULL) {
      if(hashArray[hashIndex]->key == key)
         return hashArray[hashIndex];
      ++hashIndex;
      hashIndex %= SIZE;
   }       

   return NULL;       
}

void insert(int key,char *data) {

   struct UserItem *item = (struct UserItem*) malloc(sizeof(struct UserItem));
   item->data = *data;
   item->key = key;
   int hashIndex = hashCode(key);
   while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
      ++hashIndex;
      hashIndex %= SIZE;
   }

   hashArray[hashIndex] = item;
}

struct UserItem* delete(struct UserItem* item) {
   int key = item->key;
   int hashIndex = hashCode(key);
   while(hashArray[hashIndex] != NULL) {
      if(hashArray[hashIndex]->key == key) {
         struct UserItem* temp = hashArray[hashIndex];
         hashArray[hashIndex] = dummyItem;
         return temp;
      }
      ++hashIndex;
      hashIndex %= SIZE;
   }     

   return NULL;       
}

void display() {
   int i = 0;
   for(i = 0; i<SIZE; i++) {
      if(hashArray[i] != NULL)
         printf(" (%d,%s)",hashArray[i]->key,hashArray[i]->data);
      else
         printf("No data");
   }

   printf(" ");
}

int main() {
   int id, i;
   char content[10];
   char *data;
   int ch;
   struct UserItem *item;
   data = content;
   dummyItem = (struct UserItem*) malloc(sizeof(struct UserItem));
   dummyItem->data = -1;
   dummyItem->key = -1;

   FILE *fp;
   fp = fopen("Input.txt", "r");
   if (fp != NULL) {
   while (fscanf(fp, "%d %s", &id, data){
  insert(id, data);
printf("1.Add New User ");
   printf("2.Delete User ");
   printf("3.Verify User ");
   printf("4.Print User ");
   printf("5.Quit ");
   scanf("%d", &ch);
   switch(ch)
case 1:
  scanf("%id %s", &id, data);
  insert(id,data);
        case 2:
  scanf("%d", &id);
                item = search(id)
                if (item != NULL)
                    delete(item);
                    printf("User deleted ");
                else
                   printf("User not found ");
        case 3:
  scanf("%d", &id);
                item = search(id)
                if (item != NULL)
                    printf(" Valid User ");
                else
                   printf("Invalid User ");
        case 4:
                 display();
case 5:
                fp = fopen("Input.txt", "w");
                for (i=0; i<SIZE; i++)
                    fprintf(fp, "%d %s ",hashArray[i]->key,hashArray[i]->data);
                fclose(fp);
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote