#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 = array[index]-'0';
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;
}
Intput: 26
304 90595170 997181 195 186258091 7356 832170578 533 2697 2163 1186 26 598792210 962295110 829561344 155871 37 7 3139 36256738 24845 5 4 423389931 4306319 201886
Output: 5278572
Expected: 677917
@ kemort, that would but nice for "pseudo-code" and looks as if it would almost work, but for some weird reason it does NOT work!! With your code and the following input:
28
9763 544 37320 825 45 54933302 3 1435 37 72344920 7269 48582 783844 68 7 5010 6991093 1 3 291299749 8577967 74645175 28 345478567 741872 1 56305 32486
The output is an insane: 2897635442148016
and expected is a "normal": 9645031
Correct me if I'm wrong, and I'm sure you will, but isn't this the intent of the OP code?
OP wrote:
Basically, I need to "checksum" for an array of numbers
I don't think you've quite captured the intent. I don't know why std::string has been involved in this code at all. Judging from the comments in the snippet in the OP, I would imagine a checksum for a sequence of numbers is actually what is meant rather than the checksum of a series of digits.
I actually cant even think of the logic on how to be able to distinguish which elements in the string contains which number, i dont even think thats possible
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int a;
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;
int array[a]; // 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;
}
Do you even need to use a string? How about just entering in each number one at a time?
You could enter each number (stored in an INT variable) and do your arithmetic that way if it isn't necessary to use string.
And alex, don't be rude. OP is asking for help. Let's not berate. That will just encourage OP to not ask for help again.
OP also needs the modulo. OP literally put that right on the line that uses it.
@alex067 There are many functions that allow you tell if a character is a number.
if ((str[index] - '0' >=0 && str[index] - '0' <= 9)) will work
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string str("102345,,.,769.q2s");
for (auto i = 0; i < str.length(); i++)
{
std::cout << "str[" << i << "] = " << str[i] << " | Is a number? " << (str[i] - '0' >= 0 && str[i] - '0' <= 9) << std::endl;
}
}
produces
str[0] = 1 | Is a number? 1
str[1] = 0 | Is a number? 1
str[2] = 2 | Is a number? 1
str[3] = 3 | Is a number? 1
str[4] = 4 | Is a number? 1
str[5] = 5 | Is a number? 1
str[6] = , | Is a number? 0
str[7] = , | Is a number? 0
str[8] = . | Is a number? 0
str[9] = , | Is a number? 0
str[10] = 7 | Is a number? 1
str[11] = 6 | Is a number? 1
str[12] = 9 | Is a number? 1
str[13] = . | Is a number? 0
str[14] = q | Is a number? 0
str[15] = 2 | Is a number? 1
str[16] = s | Is a number? 0
Press any key to continue . . .
Yeah you're quite right about the string not being required.
I simply did a cut and paste from what I read as the latest permutation of the posts. When I looked at the OP, the array, even though it is not declared correctly, appeared to be significant. (It should be a dynamic array)
But to make it clear there is no significance in using a string other than that.
The points I was concentrating on were the intent/purpose/algorithm itself given the array of inputs (which is grossly long but may be necessary, who knows) and indirectly at the simpler use of for loops as mentioned by alex067 above.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
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 a; // initialize array index to the start of array
cin>>a;
for (int i = 0; i < a; i++) {
int b;
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) // increment index to point to next element of array
// if array is not exhausted, return to array index
}
cout<< result;
}