Basically, I need to "checksum" for an array of numbers, the process explained in the comments of this code. However, I keep on getting the wrong result and I'm not sure why.
/* choose some arbitrary number as seed - 113;
choose some big number as the limit for checksum - 10000007;
initialize result value to 0;
initialize array index to the start of array;
add the member pointed by index to result;
multiply result by seed;
take the result by modulo limit (if necessary);
increment index to point to next element of array;
if array is not exhausted, return to array index*/
#include <iostream>
usingnamespace std;
int main(){
int seed = 113;
int limit = 10000007;
int result = 0;
int index;
cin>>index;
do { int array[index];
cin>> array[index];
result = ((result + array[index])*seed) % limit;
index --;
} while (index>0);
cout<< result;
}
/*
choose some arbitrary number as seed - 113;
choose some big number as the limit for checksum - 10000007;
initialize result value to 0;
initialize array index to the start of array;
add the member pointed by index to result;
multiply result by seed;
take the result by modulo limit (if necessary);
increment index to point to next element of array;
if array is not exhausted, return to array index
*/
#include <iostream>
#include <string>
int checksum(std::string array)
{
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
int result = 0; // initialize result value to 0
size_t index = 0; // initialize array index to the start of array
do
{
result += array[index]; // add the member pointed by index to result
result *= seed; // multiply result by seed
result %= limit; // take the result by modulo limit (if necessary)
++index; // increment index to point to next element of array
}
while (index < array.size());// if array is not exhausted, return to array index
return result;
}
int main()
{
std::string text;
std::cout << "Enter some text: ";
std::getline(std::cin, text);
std::cout << "checksum: " << checksum(text) << "\n";
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a;
string array;
cin>>a;
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
int result = 0; // initialize result value to 0
int index = 0; // initialize array index to the start of array
do
{ cin>>array[index];
result += array[index]; // add the member pointed by index to result
result *= seed; // multiply result by seed
result %= limit; // take the result by modulo limit (if necessary)
++index; // increment index to point to next element of array
}
while (index <a); // if array is not exhausted, return to array index
cout<< result;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a;
string array;
cin>>a;
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
unsignedint result = 0; // initialize result value to 0
int index = 0; // initialize array index to the start of array
do
{
cin>>array[index];
result += array[index]; // add the member pointed by index to result
result *= seed; // multiply result by seed
result %= limit; // take the result by modulo limit (if necessary)
++index; // increment index to point to next element of array
}
while (index <a); // if array is not exhausted, return to array index
cout<< result;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a,b;
cin>>a;
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
unsignedint result; // initialize result value to 0
int index = 0; // initialize array index to the start of array
do
{
cin>>b;
result += b; // add the member pointed by index to result
result *= seed; // multiply result by seed
result %= limit; // take the result by modulo limit (if necessary)
++index; // increment index to point to next element of array
}
while (index <a); // if array is not exhausted, return to array index
cout<< result;
}
int a;
string array;
cin>>a;
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
unsignedint result = 0; // initialize result value to 0
int index = 0; // initialize array index to the start of array
do
{
cin>>array[index];
result += array[index]; // add the member pointed by index to result
Characters have ASCII values that don't correspond to the number itself. For example '1' (the character) has the value 49. When you add '1', you'll first want to convert it to an integer.
You can do '1' - '0' and it will return 1. '9' - '0' will return 9, and so on.
I would never tell you to ignore a poster on here, but ignore asl1. asl1 is just a troll.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a;
cin>>a;
string array;
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
unsignedint result=0; // initialize result value to 0
int index = 0; // initialize array index to the start of array
do
{
cin>>array[index];
int b = static_cast<int>(array[index]);
result += b; // add the member pointed by index to result
result *= seed; // multiply result by seed
result %= limit; // take the result by modulo limit (if necessary)
++index; // increment index to point to next element of array
}
while (index <a); // if array is not exhausted, return to array index
cout<< result;
}
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a;
cin>>a;
string array;
int seed = 113; // choose some arbitrary number as seed - 113
int limit = 10000007; // choose some big number as the limit for checksum - 10000007
unsignedint result=0; // initialize result value to 0
int index = 0; // initialize array index to the start of array
do
{
cin>>array[index];
result += array[index]-'0'; // add the member pointed by index to result
result *= seed; // multiply result by seed
result %= limit; // take the result by modulo limit (if necessary)
++index; // increment index to point to next element of array
}
while (index <a); // if array is not exhausted, return to array index
cout<< result;
}
but... if the array is empty, how in the world am I getting any results at all? (not to mention results high up in the millions, negating the "oh, you just added seeds and divided by limit" argument.
but... if the array is empty, how in the world am I getting any results at all?
If you look at a random place in memory, there are values in it. That's what you're doing. Processing values at a random place in memory. (And, incidentally, evoking undefined behavior so any result at all is acceptable according to the standard.)
Read in the string before you enter the loop. Read in the entire string. Then use index to iterate through each character, add it to result, and do your calculations.