Question

For this problem, you have to use following hash function: key modulo the number of buckets.

Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, either ‘i’ or ‘s’, followed by a tab and then an integer, the same format as in the Second Part. For each of the lines that starts with ‘i’, your program should insert that number in the hash table if it is not present. If the line starts with a ‘s’, your program should search the hash table for that value.

Output format: For each line in the input file, your program should print the status/result of that operation. For an insert, the program should print “inserted” if the value is inserted or “duplicate” if the value is already present. For a search, the program should print ”present” or “absent” based on the outcome of the search. Your program should print “error” (and nothing else) if the file does not exist. The program should also print “error” for input lines with improper structure.

Example Execution: Lets assume we have 2 text files with the following contents: file1.txt is empty file2.txt: i 10 i 12 s 10 i 10 s 5 The the results will be: third file1.txt third file2.txt inserted inserted present error duplicate absent /third file3 .txt error

In C, please. Thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Program:

/* C Program that implements Hash table */

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

//Function prototypes
void insertOperation(int[], int, int);
void searchOperation(int[], int, int);

//Main function
int main(int argc, char *argv[])
{
   //Hash table
   int hashTable[10];
   int key, i;
   char operation;

   FILE *fp;
  
   //Opening file in read mode
   fp = fopen(argv[1], "r");

   //If file doesn't exist
   if(!fp)
   {
       printf("\n Error \n");
       return 0;
   }
  
   //Initializing hash table with -1
   for(i=0; i<10; i++)
       hashTable[i] = -1;

   //Reading data from file
   while(fscanf(fp, " %c %d ", &operation, &key) == 2)
   {
       //Calling appropriate function based on type of operation
       switch(operation)
       {
           //Insert operation
           case 'i':
                   insertOperation(hashTable, 10, key);
                   break;
          
           //Search Operation
           case 's':  
                   searchOperation(hashTable, 10, key);
                   break;

           default:  
                   printf("\n Error \n");
                   break;
       }
   }

   //Closing file
   fclose(fp);
}

//Function that inserts a value in to hash table
void insertOperation(int hashTable[], int size, int key)
{
   int hashValue;

   //Calculating hash value
   hashValue = key % size;

   //If there is a vacant space insert the element
   if(hashTable[hashValue] == -1)
   {
       hashTable[hashValue] = key;
       printf("\n Inserted \n");      
   }
   else
   {
       //Bucket is not empty  
       printf("\n Duplicate \n");      
   }          
}

//Function that searches for a value in hash table
void searchOperation(int hashTable[], int size, int key)
{
   int hashValue;
  
   //Calculating hash value
   hashValue = key % size;
  
   //Searching in hash table
   if(hashTable[hashValue] == key)
   {
       printf("\n Present \n");      
   }
   else
   {
       printf("\n Absent \n");      
   }          
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

anil@ubuntu: anil@ubuntu: $ gcc hashing.c anil@ubuntu:- a.out file2.txt Inserted Inserted Present Error Duplicate Absent anil

Add a comment
Know the answer?
Add Answer to:
For this problem, you have to use following hash function: key modulo the number of buckets....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write Linux command for each question. Please use one single commend to do following process. Q1:...

    Write Linux command for each question. Please use one single commend to do following process. Q1: Display all lines of list1.txt and list2.txt containing the letter ‘p', and sort the result. Q2: Append “file1.txt” and “file2.txt”, and redirect to “file3.txt”. Q3: Sort “file3.txt”, and redirect the result to file “file4.txt” Q4: Use shorthand way to change the permission of “file3.txt” to “user - write read execute, group - read execute, other - execute”. Q5: Extract first column from “file3.txt”, and...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • Please write the following code as simple as possible in python: You will need to define...

    Please write the following code as simple as possible in python: You will need to define a function with four arguments. Here is what I used: > def find_matches(file1.txt, output1.txt, strings, value): file1.txt will contain a list of various strings. The program must copy from the first argument, and it should be written in the second argument (the second file, "output1.txt"). The third and fourth arguments will determine which specific strings will be copied over to the second file. For...

  • Java using data structures The objective is to create your own Hash Table class to hold...

    Java using data structures The objective is to create your own Hash Table class to hold a list of employees and their ID numbers. I've provided the TableEntry class which will be each data object in the hash table. The list of employees will be provided as a .txt file and must be read with the code. please create a .txt file called Employees.txt with the info provided so that the java code can read it in. Employees.txt: (No WhiteSpace...

  • Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash f...

    Please complete the following task: Create a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash funct...

    I really need assistance with creating a C++ hash table program for generating a hash from a string. Create a hash function then test and evaluate the results. The set of strings are from a "words.txt" file (there are 45,402 words in the file, no repeating words. Map the words to 45,402 buckets using a hash function). Requirements: Create an array of 45,402 integers and initialize each to zero. These will be used to store how many times each index...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • I need help using python ( spyder) only use files , loops if needed is statement do not use , def...

    i need help using python ( spyder) only use files , loops if needed is statement do not use , def function or any other. Exercise 1: Daily temperature is recorded for some weeks in files (templ.txt", temp2.txt, and temp3.txt; provided in the MOODLE). The first line contains number of weeks and the rest of the lines each represent the week number followed by temperature on the seven days of that week (see samples input files below). Write a python...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT