Question

In: Computer Science

convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char...

convert this string type   program into interger data type

#include "stdafx.h"
#include
#include

unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table
unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table

#define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value)

#define HASH_SIZE 100 // size of hash table to be used (for testing I suggest making this number much lower)

                                               // data structure used to keep track of hashed data
struct myHashStruct {
   char *ptrBuffer = NULL; // pointer to data stored in hash (you will need to malloc space for string to be stored)
   struct myHashStruct *ptrNextHashData = NULL; // pointer to next item in this hash bucket (or NULL if no more)
};

struct myHashStruct *myHash[HASH_SIZE]; // create an empty hash table structure (note this is basically an arrary of linked list heads)

int main()
{
   char inputBuffer[INPUT_BUFFER_SIZE];

   // initialize the hash table to empty one
   for (int i = 0; i < HASH_SIZE; i++)
   {
       if ((myHash[i] = (struct myHashStruct *)calloc(1, sizeof(struct myHashStruct))) == NULL)
       {
           printf("calloc failed!\n");
           return(-1);
       }
   }

   // add to hash table loop
   while (1)
   {
       printf("enter data to be added to hash table or exit when done\n");

       // get strings from the console and place in hash until nothing entered
       scanf_s("%s", inputBuffer, INPUT_BUFFER_SIZE);

       // stop adding data into hash table when "exit" is entered
       if (strcmp(inputBuffer, "exit") == 0)
           break;

       if (putIntoHashTable(inputBuffer, strlen(inputBuffer)) == HASH_SIZE)
           printf("Error putting into hash table\n");
   }

   // check if data is in hash table
   while (1)
   {
       unsigned int hashIndexNumber = 0;

       printf("Enter data to find, done when complete\n");

       // get strings from the console and check if in hash table
       scanf_s("%s", inputBuffer, INPUT_BUFFER_SIZE);

       // stop adding data into hash table when "done" is entered
       if (strcmp(inputBuffer, "done") == 0)
           break;

       if ((hashIndexNumber = getFromHashTable(inputBuffer, strlen(inputBuffer))) == HASH_SIZE)
           printf("%s not found in hash table\n", inputBuffer);
       else
           printf("%s found in hash table at %u\n", inputBuffer, hashIndexNumber);
   }

   return 0;
}

// FUNCTION : myHashFunction
// DESCRIPTION :
// Calculate a hash value to use in storing the data into the hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash table
//
// RETURNS :
// Hash value calculated ( HASH_SIZE on failure)

unsigned int myHashFunction(char *ptrInputBuffer)
{
   unsigned int calculatedHashValue = 0;

   // add code to create a hashed value here!!!
  
   for (int i = 0; i < HASH_SIZE; i++) // here string can be add based on hash value
   {
      
           calculatedHashValue += ptrInputBuffer[i]; // hash value will be added in buffer of size i
      
   }
printf("Hash Calculated to be :%d \n", calculatedHashValue); // hash value that calculated will be printed
   // make sure if hash value is bigger than the table size, the value wraps
   return calculatedHashValue % HASH_SIZE;
}

// FUNCTION : putIntoHashTable
// DESCRIPTION :
// Put the supplied data into a hash table
// PARAMETERS :
// ptrInputBuffer - a pointer to the buffer to be put into the hash table
// bufferLength - how many characters are in the buffer being put into the hash table
//
// RETURNS :
// Hash value used ( HASH_SIZE on failure)

unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength)
{
   unsigned int hashValue = myHashFunction(ptrInputData);

   // check if the value is in the hash

   // add code to put data into the hash table!!!

   return hashValue;
}

// FUNCTION : getFromHashTable
// DESCRIPTION :
// Read as much data as there is room for from the hash table
// PARAMETERS :
// ptrOutputBuffer - a pointer to the buffer to place the data read from hash table
// bufferLength - maxiumum number of characters that can be read
//
// RETURNS :
// Hash value used ( return HASH_SIZE value on failure)

unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength)
{
   unsigned int hashValue = myHashFunction(ptrOutputData);

   // check if the data is in the hash table here!!!

   return hashValue;
}

// end code * * *   

Solutions

Expert Solution

CODE

#include<stdio.h>

#include <stdlib.h>

unsigned int putIntoHashTable(int inputData); // function to add to hash table

unsigned int getFromHashTable(int inputData); // function to retrieve data from hash table

#define HASH_SIZE 100 // size of hash table to be used (for testing I suggest making this number much lower)

// data structure used to keep track of hashed data

struct myHashStruct {

int data;

struct myHashStruct *ptrNextHashData; // pointer to next item in this hash bucket (or NULL if no more)

};

struct myHashStruct *myHash[HASH_SIZE]; // create an empty hash table structure (note this is basically an arrary of linked list heads)

int main()

{

int input;

// initialize the hash table to empty one

for (int i = 0; i < HASH_SIZE; i++)

{

if ((myHash[i] = (struct myHashStruct *)calloc(1, sizeof(struct myHashStruct))) == NULL)

{

printf("calloc failed!\n");

return(-1);

}

}

// add to hash table loop

while (1)

{

printf("enter data to be added to hash table or -1 when done\n");

// get strings from the console and place in hash until nothing entered

scanf("%d", &input);

// stop adding data into hash table when "exit" is entered

if (input == -1)

break;

if (putIntoHashTable(input) == HASH_SIZE)

printf("Error putting into hash table\n");

}

// check if data is in hash table

while (1)

{

unsigned int hashIndexNumber = 0;

printf("Enter data to find, 0 when complete\n");

// get strings from the console and check if in hash table

scanf("%d", &input);

// stop adding data into hash table when "done" is entered

if (input == 0)

break;

if ((hashIndexNumber = getFromHashTable(input)) == HASH_SIZE)

printf("%d not found in hash table\n", input);

else

printf("%d found in hash table at %u\n", input, hashIndexNumber);

}

return 0;

}

// FUNCTION : myHashFunction

// DESCRIPTION :

// Calculate a hash value to use in storing the data into the hash table

// PARAMETERS :

// input - an integer

// RETURNS :

// Hash value calculated ( HASH_SIZE on failure)

unsigned int myHashFunction(int input)

{

return input * input % HASH_SIZE;

}

// FUNCTION : putIntoHashTable

// DESCRIPTION :

// Put the supplied data into a hash table

// PARAMETERS :

// input - an integer

// RETURNS :

// Hash value used ( HASH_SIZE on failure)

unsigned int putIntoHashTable(int input)

{

unsigned int hashValue = myHashFunction(input);

// check if the value is in the hash

// add code to put data into the hash table!!!

return hashValue;

}

// FUNCTION : getFromHashTable

// DESCRIPTION :

// Read as much data as there is room for from the hash table

// PARAMETERS :

// input - an integer

// RETURNS :

// Hash value used ( return HASH_SIZE value on failure)

unsigned int getFromHashTable(int input)

{

unsigned int hashValue = myHashFunction(input);

// check if the data is in the hash table here!!!

return hashValue;

}

// end code * * *



Related Solutions

convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include...
convert this program that is for string into character input data #include "stdafx.h" #include <string.h> #include <stdlib.h> unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to...
c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; }...
c program //the file's size in bytes unsigned int fileSize(const char* name){     return 0; } //same as fileSize except if the file is a directory then it recursively finds the size of directory and all files it contains unsigned int fileSizeRec(const char* name){     return 0; }
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0;...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Design a C program to print out the unsigned char (integer) values of the 4 bytes...
Design a C program to print out the unsigned char (integer) values of the 4 bytes in sequential order (i.e., the first byte is printed first). Using the code framework provided below, the printing is done by the function void byte_value(int *), in which only pointer variables can be declared and used. #include <stdio.h> ​ void byte_value(int *); ​ int main() { int n = 1; byte_value(&n); printf("Enter an integer: "); if (scanf("%d", &n) == 1) byte_value(&n); return 0; }...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT