C++, Zany Zack, Arrays, and Functions.

I have been assigned the following problem:

Zack the Zany (formerly the Employee of the Month) has been reassigned night duty at
Super Club. Zack is back to being easily bored, and has decided to play a little game to
pass the time using the stock of 1000 drink tumblers that the store hasn’t put on sale yet.
Each tumbler also comes with a lid. Here’s how the game works:

1. First Zack goes through and takes the lid off all 1000 tumblers.
2. He then returns to the first tumbler and puts the lid back on every other one.
3. Then he returns to the first tumbler and checks every third one:
If the lid is off, he puts it back on the tumbler.
If the lid is on, he takes it off the tumbler.
4. He repeats the process for every fourth tumbler and lid, every fifth tumbler and lid,
etc.
5. At the end, what tumblers have lids on them?

You must use an array for this assignment and an enumerated type.


My problem is the program compiles with no errors but as soon as I try to run it, my computer starts complaining. I'm not sure what I'm doing wrong. If someone could offer me a suggestion, that would be great!


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
#include<iostream>
using namespace std;

void tumbler (int array[], int size){
enum Status{ON, OFF};

//Zack takes off or puts on lids of tumblers divisible by 2, 3, 4, 5, etc.
for (int count = 0; count < size; count ++){ //count for all 1000 tumblers
for (int i = 1; i < size; i ++){ //count for divisiblity
if ( i%count == 0 && array[i] == ON){
array[count] = OFF;
}
else if (i%count == 0 && array[i] == OFF){
array[count] = ON;
}

}

} 
cout<<array[size];
}

int main(){
const int size = 1000;
int array[size]; 
enum Status{ON, OFF};
//Zack takes all the lids off
array[1000] = OFF;
//function call
tumbler(array, size);


return 0;
}
array[1000] = OFF;

This does not set all the elements of array to off. It attempts to access the 1001st element of array, but that element does not exist, so you clobber memory that you shouldn't be touching.

The easiest way to do this would be to reverse your ON/OFF values since we can easily 0-initialize an array, but initializing an array to anything else requires that you supply values for every element in the array.

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
#include<iostream>
using namespace std;

enum Status { OFF, ON };

void tumbler(Status array[], int size)
{

    //Zack takes off or puts on lids of tumblers divisible by 2, 3, 4, 5, etc.
    for (int count = 0; count < size; count++){ //count for all 1000 tumblers
        for (int i = 1; i < size; i++){ //count for divisiblity
            if (i%count == 0 && array[i] == ON){
                array[count] = OFF;
            }
            else if (i%count == 0 && array[i] == OFF){
                array[count] = ON;
            }

        }

    }
    cout << array[size];
}

int main()
{
    const int size = 1000;
    Status array[size] = {}; // zero initialize array.

    tumbler(array, size);
}


Topic archived. No new replies allowed.