QUESTION 1 Write a program named hw7pr1.cpp which reads a line into a string and
ID: 3571499 • Letter: Q
Question
QUESTION 1
Write a program named hw7pr1.cpp which reads a line into a string and calculates a hash function value as follows: Assign each character to an unsigned long, in a vector of unsigned longs. For example, an input of ABC will produce a vector of {65, 66, 67}. Shift each unsigned long left by its index; in other words, v[0] is shifted left by 0 bits, v[1] is shifted left by 1 bit and so on. This changes {65, 66, 67} to {65, 132, 268}. Now exclusive or all three numbers using the ^ operator (also named xor). This calculates 65 xor 132 xor 268, which is 457. Print the hash value (457) and repeat until end of file.
QUSTION 2
Explanation / Answer
Answer Q1-
hw7pr1.cpp
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
unsigned long ComputeHash(string inputString)
{
vector<unsigned long> testVector(200);
for(int i=0;i<inputString.length();i++)
{
testVector[i]=(long)inputString[i];
}
vector<unsigned long> shiftedVector(200);
unsigned long hashValue = 0;
for(int j=0;j< inputString.length();j++ )
{
shiftedVector[j] = testVector[j]<<j;
hashValue = hashValue ^ shiftedVector[j];
}
return hashValue;
}
int main()
{
string STRING=" ";
ifstream infile;
// input.txt is an input file for which hash values are calculated for each line.This file should be in same directory as // your project directory
infile.open ("input.txt");
while(getline(infile,STRING)) // To get you all the lines.
{
unsigned long result=ComputeHash(STRING);
cout<<result;
}
infile.close();
system ("pause");
return 0;
}
Question 2:- more information needed-unable to find code in section 23.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.