Question

Implement a hash function that hashes pictures, where a picture is a 500 by 500 array...

Implement a hash function that hashes pictures, where a picture is a 500 by 500 array of Pixels and a Pixel is a struct with three components r, g, b of type unsigned char.

unsigned int hash(Pixel pic[][500], unsigned int tableSize);

Hint: Convert each pixel to a nonnegative integer, then combine the 25,000 numbers to get a hash value.

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

Points to consider:

  1. For the above function for testing, we are forming a random pic with r, g, b or unsigned.
  2. Then we have to find the hash value, and for which we are using the sum % 256 as the hash function
  3. The sum would be of all the r,g,b of each pixel.

Code

#include<iostream>
using namespace std;

struct Pixel
{
   unsigned char r, g, b;  
};
unsigned int hash(Pixel pic[][500])
{
   int hash = 0;
   for(int i = 0;i<500;i++)
   {
       for(int j = 0;j<500;j++)
       {
           Pixel cur = pic[i][j];
           hash += (unsigned int)cur.r + (unsigned int)cur.g + (unsigned int)cur.b;
           hash %= 256;
       }
   }
   return hash;
}
int main()
{
   Pixel pic[500][500];
   for(int i = 0;i<500;i++)
   {
       for(int j = 0; j < 500;j++)
       {
           Pixel cur;
           cur.r = (unsigned char)j;
           cur.g = (unsigned char)j;
           cur.b = (unsigned char)j;
           pic[i][j] = cur;
       }
      
   }
   cout<<"Hash Value of the above Pic is: "<<hash(pic)<<endl;
}

Code Snippet

1 2 #include<iostream> using namespace std; struct Pixel minn unsigned char r, g, b; 8_unsigned int hash(Pixel pic[](500]) in

Sample Output:

- 0 X C:\Users\visha OneDrive\Documents\C++\hashFunction.exe Hash Value of the above Pic is: 8 Process exited after 4.388 sec

Friend, That was a nice question to answer
If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like it if you think effort deserves like.
Thanks

Add a comment
Know the answer?
Add Answer to:
Implement a hash function that hashes pictures, where a picture is a 500 by 500 array...
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
  • 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)...

  • in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an...

    in java Original: Sound Visualization Write a program that uses StdAudio and Picture to create an interesting two-dimensional color visualization of a sound file while it is playing. Be creative! Additional Notes: double[] data = StdAudio.read(<filename>); -> Takes a sound file and store it as a 2D array of real numbers. Picture pic = new Picture(300,200); -> Creates a picture 300 pixels wide and 200 pixels high Color c= new Color(<red>,<green>,<blue>); -> Each value of <red>, <green>, <blue> is between...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

  • Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...

    Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

  • Task The task for this assignment is to have the following user-defined data type: struct rgb...

    Task The task for this assignment is to have the following user-defined data type: struct rgb { unsigned char red; unsigned char green; unsigned char blue; }; be able to be: read in from a stream (e.g., std::cin), i.e., write: std::istream& operator >>(std::istream& is, rgb& colour); (see below) written out to a stream (e.g., std::cout), i.e., write: std::ostream& operator <<(std::ostream& os, rgb const& colour); (see below) stored in a container, e.g., std::vector<rgb>, std::array<rgb,16>; (see below) processed via algorithms (and other...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

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