Unknown error in array checksum program

Pages: 12
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* 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>
using namespace 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;
}


Input: 20
641972598 460 9 4096 10 37576 879 4778303 305 4029888 9013429 793634 841709649 774 1234 35124649 789567 80065860 178449395 1

Output: -3359645 (what?! It should be positive!)
Anyone care to explain?
Last edited on
Take a look at this for comparison. Just take it step by step so that it's easy to verify.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
    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";
}
Last edited on
thanks, but when I tried what you did, I failed and got the wrong answer still.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace 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;
}
Last edited on
bump.
Change result to unsigned int result
Still 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;
    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
    unsigned 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;
}


Input: 23
546390 853175 230786438 4937 18665 525460 37 9 93796 39 6309 22622 452934 30373 79 841803 1945 2911574 1454 9539 925 49294 568

Output: 1134714
Expected: 9882242
It is not showing the right number because you are comparing an int to a string

result is an int, array[index] is an array of chars for a string, so what you are seeing is the ascii code

result += array[index] is just adding the ascii code of whatever value is in the first index
@asl1, I AM doing a sum check program.

I tried doing the int thing but it still failed... again... @.@

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


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.
argh!! I'm cursed to never get this right! (Tried casting string into int)

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 = 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;
}
Last edited on
Don't static cast it to an integer.
1
2
3
4
5
6
7
8
9
10
int main()
{
	int a;
	char b = '9';


	std::cout << "static_cast<int>(b) = " << static_cast<int>(b) << std::endl;
	std::cout << "b - '0' = " << b - '0' << std::endl;

}


Produces:

static_cast<int>(b) = 57
b - '0' = 9
Press any key to continue . . .
somehow, I failed again... :/

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;
    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];
        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;
}


Intput: 24
89587947 9348985 611335640 1 67085 3 221350 9154545 1 7 667188407 579541850 7110495 7879 8 89 552158 611326412 982837 2170642 789000 30058 2 1
Output: 7194994
Expected: 7886301
Last edited on
OK. Give an input and an expected output.
array is an empty string. At no point is index a valid index into an empty string.

You are accessing memory you don't own.
Last edited on
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.
Last edited on
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.)
so how do I NOT make the array empty....? Why doesn't "Array[0]" access the first element of the input?
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.
Wow i explained to you in the beginning very clearly whats wrong

array[0], assuming array is the name of the string, will get you a single char from the string.

as i said before, you CANT do result += array[index], this will give you the ASCII CODE ADDITION AND NOT DECIMAL ADDITION

You have to find a way to convert the index ascii code to a decimal

also, if you are doing integer sums, why put it in a string?...

*HINT:

find out the ascii code of the first string index, subtract it with something...
closed account (48T7M4Gy)
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
27
28
29
30
31
32
#include <iostream>
#include <string>

using namespace std;

int main()
{
	const int arrayLimit = 3;
	int strLimit = 0;

	string array[arrayLimit];

	int seed = 113;
	int limit = 10000007;
	int result = 0;

	for (int i = 0; i < arrayLimit; i++)
	{
		cin >> array[i];
		strLimit = array[i].length();

		for (int position = 0; position < strLimit; position++)
		{
			cout << array[i][position];
			result += array[i][position] - '0'; // <-- Oops!
			result *= seed;
			result %= limit;
		}
	}

	cout << result;
}


Correct me if I'm wrong, and I'm sure you will, but isn't this the intent of the OP code?
Last edited on
Pages: 12