// ConsoleApplication4.cpp : Defines the entry point for the console application
ID: 3935186 • Letter: #
Question
// ConsoleApplication4.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include /* int _tmain(int argc, _TCHAR* argv[]) { std::vector bins(256); std::string chars{ "jhsdfajhsdfajhg" }; for (auto ch : chars) bins[int(ch)]++; for (int ind = 0; ind < 256; ind++) for (int elcount = 0; elcount < bins[ind]; elcount++) std::cout << char(ind); return 0; } */ unsigned int f(std::string s, unsigned int base) { unsigned int sum = 0; for (auto ch : s) sum += (unsigned int)ch; return sum % base; } int _tmain(int argc, _TCHAR* argv[]) { const unsigned int hashSize = 17; std::vector hashTable(hashSize); std::vector data{ "cat", "dog", "ape" }; for (auto s : data) std::cout << s << "-->" << f(s, hashSize) << std::endl; }
Explanation / Answer
// #include "stdafx.h"
#include
#include
#include
/* int _tmain(int argc, _TCHAR* argv[]) {
std::vector bins(256);
std::string chars
{
"jhsdfajhsdfajhg" };
for (auto ch : chars) bins[int(ch)]++;
for (int ind = 0; ind < 256; ind++)
for (int elcount = 0; elcount < bins[ind]; elcount++)
std::cout << char(ind); return 0;
}
*/ unsigned int f(std::string s, unsigned int base)
{
unsigned int sum = 0; for (auto ch : s)
sum += (unsigned int)ch; return sum % base;
}
int _tmain(int argc, _TCHAR* argv[])
{
const unsigned int hashSize = 17;
std::vector hashTable(hashSize);
std::vector data{ "cat", "dog", "ape" };
for (auto s : data)
std::cout << s << "-->"
<< f(s, hashSize) <<
std::endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.