I am trying to create hashing function the algorithm requires me to take each letter of the string and converting it to ascii, multiply it by a number based on its position in the string (for example Input is STACK; ascii for S would be 344 multiply that value by 9^9 then ascii for T would be 347 multiply that value by 8^8.. etc), and then add the values together.

I understand how to access the values in the array to create the sum and can convert to ASCII. I don't understand how i would access the array to perform the calculations in-between.

my code is below :

``````#include <iostream>
#include <string>

using namespace std;

int main()
{
string input ;

cin >> input;

cout << "Your name is " << input ;

for (int x=0; x < input.size(); x++)
{

}

cin.get();
return 0;

}
``````
2
Contributors
2
Replies
19
Views
5 Years
Discussion Span
Last Post by tinstaafl

so the calculation would be something like : (1047^7) + (996^6) + (52 * 5^5) etc

With this code using your example("STACK") the out put is

``````Enter your first name please : STACK
19571

int main()
{
string input ;
cin >> input;
cout << "Your name is " << input << endl;
for (int x=0; x < input.size(); x++)
{
//This sets the number to multiply by itself, starting at 9 and decreasing by 1 for character in the string.
long Powr = (x * -1) + 9;
finalanswer += input[x] * (Powr * Powr);
}