Data Structure in C++ Testing hash functions I tried this homework and there are
ID: 3842306 • Letter: D
Question
Data Structure in C++
Testing hash functions
I tried this homework and there are errors.
Please check my code and fix errors.
:
#include<iostream>
#include<fstream>
#include<functional>
#include<string>
#include<vector>
#include<cstdint>
#include<cmath>
#include<boost/math/distributions/chi_squared.hpp>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::ifstream;
using std::pow;
uint16_t x;
class hash_table{
public:
hash_table(){
hashes.resize(65536);
}
void h_length(string s){
hashes.at((s.length() % int(pow(2,16))))++;
}
void h_fchar(string s){
char fchar = s.front();
hashes.at(fchar)++;
}
void h_checksum(string s) {
int sum = 0;
for(int i = 0; i < s.length(); ++i){
sum += char(s[i]);
hashes.at(sum)++;
}
}
int checksum(string s){
int k = 0;
for(int i = 0; i < s.length(); ++i){
k = (k + 256 + s.at(i)) % 65536;
}
return k;
}
int frac(int k){
int m = 65536;
double d;
float i = j * 0.6180339887
float f = modf(i, &d);
float fr = f*m;
return fr;
}
void h_remainder(string s) {
int i = (checksum(s) % 65413);
hashes.at(i)++;
}
void h_multiplicative {
int i = frac(checksum(s));
hashes.at(i)++;
}
float chi_sq(){
boost::math::chi_squared c2d(65535.0);
float c2 = 0;
float expected_num = 99171/65536;
for(int i = 0; i < hashes.size(); i++){
c2 += ((pow((expected_num - hashes.at(i)), 2)) / expected_num);
}
float p = boost::math::cdf(c2d, c2);
return p;
}
private:
vector<int> hashes;
//vector<value> table;
}
int main () {
ifstream myfile;
myfile.open("words.txt");
string word;
cout << "String length: p = " << << endl;
cout << "First character: p = " << endl;
cout << "Checksum: p = " << endl;
cout << "Remainder: p = " << endl;
cout << "Multiplicative: p = " << endl;
return 0;
}
Explanation / Answer
please find the below code:
#include<iostream>
#include<fstream>
#include<functional>
#include<string>
#include<vector>
#include<cstdint>
#include<cmath>
#include<boost/math/distributions/chi_squared.hpp>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::ifstream;
using std::pow;
uint16_t x;
class hash_table{
public:
hash_table(){
hashes.resize(65536);
}
void h_length(string s){
hashes.at((s.length() % int(pow(2,16))))++;
}
void h_fchar(string s){
char fchar = s.front();
hashes.at(fchar)++;
}
void h_checksum(string s) {
int sum = 0;
for(int i = 0; i < s.length(); ++i){
sum += char(s[i]);
hashes.at(sum)++;
}
}
int checksum(string s){
int k = 0;
for(int i = 0; i < s.length(); ++i){
k = (k + 256 + s.at(i)) % 65536;
}
return k;
}
int frac(int k){
int m = 65536;
double d;
float i = j * 0.6180339887
float f = modf(i, &d);
float fr = f*m;
return fr;
}
void h_remainder(string s) {
int i = (checksum(s) % 65413);
hashes.at(i)++;
}
void h_multiplicative {
int i = frac(checksum(s));
hashes.at(i)++;
}
float chi_sq(){
boost::math::chi_squared c2d(65535.0);
float c2 = 0;
float expected_num = 99171/65536;
for(int i = 0; i < hashes.size(); i++){
c2 += ((pow((expected_num - hashes.at(i)), 2)) / expected_num);
}
float p = boost::math::cdf(c2d, c2);
return p;
}
private:
vector<int> hashes;
//vector<value> table;
}
int main () {
ifstream myfile;
myfile.open("words.txt");
string word;
cout << "String length: p = " << << endl;
cout << "First character: p = " << endl;
cout << "Checksum: p = " << endl;
cout << "Remainder: p = " << endl;
cout << "Multiplicative: p = " << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.