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...
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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();
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.
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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);
}
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.
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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);
}
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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);
}
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.
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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);
}
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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);
}
#include <iostream>
#include <algorithm>
usingnamespace 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
longint 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);
}
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)
{
constint sentinel = -1;
int count = 0;
int value;
while (count < array_size && cin >> value && value != sentinel)
array[count++] = value;
return count;
}
#include <iostream>
#include <algorithm>
usingnamespace 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)
{
constint 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
longint 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);
}
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);
}
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...