Unknown error in array checksum program

Pages: 12
@alex, I tried the subtract thing but as you read above, it doesn't work. Converting the first into an integer also fails:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace 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
    unsigned int 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
Last edited on
kemort wrote:
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.
Yeah idk why you are using a string...

but lets entertain the idea, so you have a string containing "123091209040" right? and you want to add each index as a result?

do you see the major flaw here? 123 is indistinguishable from 1,2,3 or 12,3 or 123 or 1,23, do you get my point?

change the string to a simple int array
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
epic fail!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace 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
    unsigned int 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;
}


input: 20
1155 338660 907 738 9636834 8920203 58218775 35452 318 3448664 7 348100 1680359 564724 4457 852567 256434 85 43 901624
Output: 7443920
expected: 1791920
Last edited on
why are you using a do while? a simple for loop will suffice

for (int i = 0; i < a; i++)
{
cin >> array[index]
result +=array[index]
}
result = result*seed;
result = result&limit;

*you do know the mod wont give you proper division? it will give you the remainder
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>
using namespace 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 . . .
Last edited on
closed account (48T7M4Gy)
Yeah idk why you are using a string...


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.
I only used a string because that was the only value fitting for an array, according to c++.

Int function fails.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace 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
    unsigned int 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;
}


Intput: 26
8542 67 659720725 98318 390 701 86503 722328 70948672 554 8588 9 992874758 596826 273450491 29 33 66865134 426173839 3 57021218 337 8737 3 333041 98426891
Output: 6239192
Expected: 5406530
closed account (48T7M4Gy)
The limit for an integer has been exceeded.
Holy man of holy words, you solved it. I keep on forgetting that int has a limit.
Topic archived. No new replies allowed.
Pages: 12