Checksum after sorted array

After completing the bubble sort array problem (how difficult!) I moved on to a similar problem, wanting me to get the checksum after I sort the array. However, there is still an error in my code, despite combining my checksum program with the bubble sort program...

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
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            }
size--;} while (swapped == true);
cout<<swaps<<" ";
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {
        cin >> array[i];
        int num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
int checksum (int array[])
{   int arraylength = array.size();
     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0;// initialize array index to the start of array
    for (int i = 0; i < arraylength; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;             
}
    return result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array);
}


Error:
solution.cc: In function 'int checksum(int*)':solution.cc:31:29:
error: request for member 'size' in 'array', which is of non-class type 'int*' { int arraylength = array.size();
Last edited on
When using a raw array there are no member functions like size, this is why you need to pass the size of the array into the function. Just like you did in the bubble sort function.

If you were using a std::vector instead of the raw array you would be able to use that containers size() member function.
Last edited on
I think I found a mistake in your code. In the GetArray function you have two different variables named num.
Last edited on
@peter, the first is to initialize it so that the do while code will start, the second is to retrieve the array numbers.
what da hay... now I have a blank output...
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
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            }
size--;} while (swapped == true);
cout<<swaps<<" ";
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {cin >> array[i];
        int num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
int checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;             
}
    return result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}


sample input :224 98660 702 75633 4444 4388 784 3107 709 5339 995 38263 49498 5481 1 6003 50 884 4 9821 27973 425 2 6099 53521 4 57 5204 467 7 7627 100 87 -1
You have an infinite loop in GetArray.
but the number equals an ever increasing array, so it will keep on going through all elements until it reaches -1...?
On line 24 you define a variable called num. This variable goes out of scope at the next } (line 26).

The num variable that you use in the if condition on line 26 is the variable that you defined on line 21. This variable is never modified so it will always have the value zero. 0 is not equal to -1 so the loop never stops running.

If you on line 24 want to modify the already existing num variable instead of creating a new one you should remove the type name in front of the variable name.

 
int num = array[i];
Last edited on
my output is nonsensical.

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
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            }
size--;} while (swapped == true);
cout<<swaps<<" ";
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {cin >> array[i];
        num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
int checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;             
}
    return result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}


input: 45 77464 10 952 0 58421 31 21 78 71 6 49 262 348 5 4794 49849 15 692 6 566 10 49 6 43 37 6 41949 39836 340 39792 883 76749 4 145 -1
output: 310 (what da heck is this?)
Expected: 33 9134548
Considering you only have 1 cout in the entire program, where are you expecting to get two numbers in your output?

And when you say "Expected: 33 9134548", what are those numbers?
Last edited on
the number of swaps, and the checksum. Couting the checksum doesn't seem to work, either:
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
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            }
size--;} while (swapped == true);
cout<<swaps<<" ";
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {cin >> array[i];
        num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
void checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;             
}
    cout<< result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}
Last edited on
I don't think 33 represents the number of swaps, I think 33 represents the number of passes over the array that it takes to complete the sort. Moving swaps++ from its current location on line 11 to just outside the for loop should get you closer to the output you're looking for.

A big problem I see right now is the fact that the -1 you use to signal the end of input is actually being put into your array. I don't think this is what you want.

Without seeing a description of how your checksum algorithm is supposed to operate, it is difficult to pin down why the output doesn't match your expectation.
aha! It really just wanted to swap all the values only ONE TIME! And THEN, return the checksum of the returning array! Thus,
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
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            } 
            if (swaps<40) //it's not gonna give me more than 40 values
            cout<<swaps-1<<" ";
size--;} while (swapped == true);
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {cin >> array[i];
        num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
void checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;             
}
    cout<< result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}


returns
29 -8396358

With the input
6210 4366 1917 6648 9 9 0 373 580 4 477 85522 2480 3322 42 184 1129 3473 25 4 1 25 9 6451 688 9 0 9312 46 73 699 16857 -1

While the correct answer was
29 4222361

So the "-1" at the end causes me problems. I tried creating statements concerning, "if (c==-1) break;" but that only gets an output of "0".
This also doesn't work:
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
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            } 
            if (swaps<50)
            cout<<swaps-1<<" ";
size--;} while (swapped == true);
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {cin >> array[i];
        num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
void checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0,c=0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        if (c<0) {goto GOTCHA;}
        result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;
}
    GOTCHA: cout<< result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}


breaking 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            } 
            if (swaps<50)
            cout<<swaps-1<<" ";
size--;} while (swapped == true);
} 

int GetArray (int array[], size_t array_size)
{
    int i=0, num =0;
    do 
    {cin >> array[i];
        num = array[i];
        i++;
    } while (num!=-1);
    return i;
}
void checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0,c=0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        int c = array[b];
        if (c==-1)
        {break;}
        else
        {result += c; // add the member pointed by index to result
        result *= seed;         // multiply result by seed
        result %= limit;    // take the result by modulo limit (if necessary) 
        b++;}
}
   cout<<result;
}

int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}
Last edited on
If -1 is in the array, and -1 is the smallest number in the array, then sorting the array will make -1 the first element of the array. Then, if you terminate the loop when you find -1, you will have processed 0 elements in the array.

Instead of trying to work around an error you made earlier, fix the error you made earlier and ensure -1 doesn't make it into the array.

1
2
3
4
5
6
7
8
9
10
11
12
int GetArray(int array [], size_t array_size)
{
    const int sentinel = -1;

    int count = 0;
    int value;

    while (count < array_size && cin >> value && value != sentinel)
        array[count++] = value;

    return count;
}

better, but checksum is somehow off for some reason. Hmm...
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
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
using namespace std;
   void Bubblesort (int size, int array [])
{   int swaps=0;
       bool swapped = true;
    do {swapped = false;
            for (int i = 0; i < size-1; i++)
            {if (array[i] > array[i+1])
                {swap(array[i], array[i+1]);
                swaps++;
                swapped = true;
                } 
            } 
            if (swaps<50)
            cout<<swaps<<" ";
size--;} while (swapped == true);
} 

int GetArray(int array [], size_t array_size)
{
    const int sentinel = -1;

    int count = 0;
    int value;

    while (count < array_size && cin >> value && value != sentinel)
        array[count++] = value;

    return count;
}
void checksum (int array[], int size)
{     int seed = 113;             // choose some arbitrary number as seed - 113
    int limit = 10000007;        // choose some big number as the limit for checksum - 10000007
    long int result=0;             // initialize result value to 0
        int b= 0;// initialize array index to the start of array
    for (int i = 0; i < size; i++) { // increment index to point to next element of array // if array is not exhausted, return to array index
        result += array[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) 
        b++;
} 
cout<< result;
}
int main()
{
     const size_t MAX_ARRAY = 1000;
    int array[MAX_ARRAY];
    size_t number_of_elements = GetArray(array, MAX_ARRAY);
    Bubblesort(number_of_elements, array); 
    checksum(array, number_of_elements);
}

Intput: 46272 10945 2004 9532 60 468 2 9 48241 7 87157 313 504 64825 164 412 3343 550 19 715 1136 8511 77 3021 84 1 6076 212 3 62154 313 1 5552 20 1713 5329 48 64 10 49017 95533 648 7 67400 72 83 -1

output: 42 885996
Expected:42 9559333
Have you started simpler? For example instead using a large array, start with an array of one or two elements instead. Something that is easy to compute by hand. I'd also start, until you have your checksum() program tested, by using a statically populated array that is already sorted to test just the checksum() function.

1
2
3
4
5
6
7
8
9
10
int main()
{
    int array[] = {100,200};
    size_t number_of_elements = 2;

    checksum(array, number_of_elements);

   return(0);
}
Last edited on
hmm... I got 1299500 with that. Not sure if right or wrong.

I have no idea how I would mess up on this program, if possible. I got my checksum program correct (previous problem), and I got my bubblesort program (another previous problem) correct. So why is this problem wrong... hm...
Topic archived. No new replies allowed.