{"id":76171,"date":"2024-07-20T19:50:37","date_gmt":"2024-07-20T23:50:37","guid":{"rendered":"https:\/\/bangla.sitestree.com\/?p=76171"},"modified":"2024-07-28T17:02:54","modified_gmt":"2024-07-28T21:02:54","slug":"build-a-number-hash-in-c","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=76171","title":{"rendered":"Build a Number Hash in C"},"content":{"rendered":"\n<p>Create a Hash Table to store numbers. Also, search numbers in that Hash Table. Write Code in C<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">define _CRT_SECURE_NO_WARNINGS<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">include<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">define MaxNumbers 50<\/h1>\n\n\n\n<p>\/\/ to divide with<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">define N 100<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">define Empty 0<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">define STORAGESIZE 100<\/h1>\n\n\n\n<p>int hashTable[STORAGESIZE + 1];<\/p>\n\n\n\n<p>\/\/initialize hash table<br \/>void initializeHashTable() {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int counter = 0; counter &lt; STORAGESIZE; counter++) {\n    hashTable&#91;counter] = 0;\n}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/find a number and return found\/not-found<br \/>int searchHashTable(int numberToFind) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int hashSearchPosition = numberToFind % N + 1;\n\nif (hashTable&#91;hashSearchPosition] == numberToFind) {\n    printf(\"%d %s %d\", numberToFind, \" Found at \", hashSearchPosition );\n    return 1;\n}\nelse {\n\n    for (int pos = hashSearchPosition + 1; pos &lt; STORAGESIZE; pos++) {\n        if (hashTable&#91;pos] == numberToFind) {\n            printf(\"%d %s %d\", numberToFind, \" Found at \", pos);\n            return 1;\n\n        }\n    }\n\n}\n\nprintf(\"Number Not Found\");\nreturn -1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/find next position after a collission<br \/>int findNextAvailablePosition(int index) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for (int pos = index + 1; pos &lt; STORAGESIZE; pos++) {\n    if (hashTable&#91;pos] == 0) {\n        return pos;\n    }\n}\n\nreturn -1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/insert data into hash table<br \/>int insertToHashTable(int l_numberToInsert) {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int hashPosition = l_numberToInsert % N + 1;\n\nif ( hashTable&#91;hashPosition] &gt; 0) {\n    \/\/collision\n    \/\/so find the next position\n    hashPosition = findNextAvailablePosition(hashPosition);\n}\n\n\/\/error finding a position \nif (  (hashPosition &gt;= STORAGESIZE) || ((hashPosition == -1)) ) {\n    \/\/ indicates error\n    return -1;\n}\n\nhashTable&#91;hashPosition] = l_numberToInsert;\n\n\/\/ 1 indicates success\nreturn 1;<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\/\/flow of the work<br \/>int main() {<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/initialize\ninitializeHashTable();\n\n\/\/read data from file\nFILE *in = fopen(\"input.txt\", \"r\");\n\nint numberToInsert = 0; \n\n\/\/convertWordToNumber()\n\n\/\/insertion\n\/\/insert data into the hash table\nwhile ( fscanf(in, \"%d\", &amp;numberToInsert) == 1 ) {\n    insertToHashTable(numberToInsert);\n}\n\n\n\/\/take input from user to search\nprintf(\"\\n%s\\n\", \"Give a number to search\");\nint numberToSearch = 56;\nscanf(\"%d\", &amp;numberToSearch);\n\n\/\/find the number in the hash\nsearchHashTable(numberToSearch);<\/code><\/pre>\n\n\n\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a Hash Table to store numbers. Also, search numbers in that Hash Table. Write Code in C define _CRT_SECURE_NO_WARNINGS include include include define MaxNumbers 50 \/\/ to divide with define N 100 define Empty 0 define STORAGESIZE 100 int hashTable[STORAGESIZE + 1]; \/\/initialize hash tablevoid initializeHashTable() { } \/\/find a number and return found\/not-foundint &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=76171\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1966,1883,182],"tags":[],"class_list":["post-76171","post","type-post","status-publish","format-standard","hentry","category-data-structure-and-algorithms","category---data-structure","category---blog","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":76169,"url":"http:\/\/bangla.sitestree.com\/?p=76169","url_meta":{"origin":76171,"position":0},"title":"Build a Word Hash","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"Store Words in a Hash Table. Also, search a word in that Hash Table define _CRT_SECURE_NO_WARNINGS include include include \/\/#define MaxNumbers 100 define MaxWordLen 50 define Empty 0 define STORAGESIZE 15 \/\/ to divide with define N 15 char hashTable[STORAGESIZE][MaxWordLen]; \/\/initialize hash tablevoid initializeHashTable() {for (int counter = 0; counter\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69017,"url":"http:\/\/bangla.sitestree.com\/?p=69017","url_meta":{"origin":76171,"position":1},"title":"Logical Data Modeling: Logical Database Design Steps: RDBMS #6","author":"Author-Check- Article-or-Video","date":"August 10, 2021","format":false,"excerpt":"Logical Data Modeling Identify major entities Determine relationships between entities Determine primary and alternate keys Determine foreign keys Determine key business rules Add remaining attributes Validate user views through normalization Determine domains Determine triggering operations Combine user views Integrate with existing data models Analyze for stability and growth Translate Logical\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76197,"url":"http:\/\/bangla.sitestree.com\/?p=76197","url_meta":{"origin":76171,"position":2},"title":"Steps to Build a Hash Table: Using Graph: Chaining","author":"Sayed","date":"July 28, 2024","format":false,"excerpt":"\u2022Build the Hash \u2022Read a number\/word from file \u2022Find the position for this number\/word \u2022in the graph (vertical array of nodes) \u2022By dividing with N or similar \u2022Check if any vertex node is there \u2022In that array position (array of graph vertices) \u2022If no vertex is there \u2022Create a vertex\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76199,"url":"http:\/\/bangla.sitestree.com\/?p=76199","url_meta":{"origin":76171,"position":3},"title":"Steps to Search in a Hash Table Built Using Graph: Chaining","author":"Sayed","date":"July 28, 2024","format":false,"excerpt":"\u2022Search the Hash \u2022Read a number\/word \u2022From the user \u2022Find the position for this number\/word \u2022in the graph (vertical array of nodes) \u2022By dividing with N or similar \u2022Check if any vertex node is there \u2022In that array position (array of graph vertices) \u2022If no vertex is there \u2022The word\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76181,"url":"http:\/\/bangla.sitestree.com\/?p=76181","url_meta":{"origin":76171,"position":4},"title":"Struct and Tree Node","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"\/\/ Ref: typedef and struct \/\/ https:\/\/www.w3resource.com\/c-programming-exercises\/c-snippets\/difference-between-typedef-struct-and-struct-definitions-with-example.php#google_vignette \/\/ https:\/\/www.tutorialspoint.com\/cprogramming\/c_pointers.htm \/\/ https:\/\/www.geeksforgeeks.org\/typedef-in-c\/ #pragma warning(disable : 4996) #include <iostream> #include <string.h> \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeData; \/\/ define what a node will look like typedef struct treenode { NodeData data;\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76183,"url":"http:\/\/bangla.sitestree.com\/?p=76183","url_meta":{"origin":76171,"position":5},"title":"Struct and Tree Node Examples","author":"Sayed","date":"July 20, 2024","format":false,"excerpt":"#include <iostream> #include <string.h> #pragma warning(disable : 4996) #define MaxWordSize 100 \/\/ Declare a structure that holds data in a node typedef struct { int num; } NodeDataInt; \/\/ Declare a structure that holds data in a node typedef struct { char word[MaxWordSize + 1]; int freq; } NodeDataChar; \/\/\u2026","rel":"","context":"In &quot;Data Structure and Algorithms&quot;","block_context":{"text":"Data Structure and Algorithms","link":"http:\/\/bangla.sitestree.com\/?cat=1966"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76171"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76171\/revisions"}],"predecessor-version":[{"id":76172,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76171\/revisions\/76172"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76171"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}