I thought I had solved it before (I couldn't find any problems and my output for the example was correct) but I got the wrong answer for the small test. After making appropriate changes, I still can't get the right answer and now my program even gets the example wrong!
Can somebody help find what I've done wrong? I've commented the code to try and make it clearer what everything does:
#include<iostream>
usingnamespace std;
int main()
{
unsignedshortint t, n;
bool snapper[31], powered[31];
bool output[10001];
unsignedint k;
cin >> t; //number of test cases
for(int i=1; i <= t; i++){ //loops for each case
snapper[0] = 1; //set the socket to powered and the ON state to use as a snapper
powered[0] = 1;
for(int a=1; a <= 30; a++) //initialise each array to 0
snapper[a] = 0;
for(int a=1; a <= 30; a++)
powered[a] = 0;
cin >> n >> k; //get input for number of snappers and number of snaps
for(int snaps=1; snaps <= k; snaps++){ //loops for each snap
for(int a = 1; a <= n; a++){ //sets each snapper to powered or unpowered
if(powered[a-1] && snapper[a-1]) //if previous one is powered and sending power (ie. ON), it is powered
powered[a] = 1;
else
powered[a] = 0;
}
for(int a = n; a >= 1; a--){ //sets each snapper to ON or OFF state
if(powered[a]){ //if it is powered (recieving power) it changes ON/OFF state
if(snapper[a])
snapper[a] = 0;
else
snapper[a] = 1;
}
}
}
if(powered[n] && snapper[n]) //after all snaps, if specified snapper is receiving and sending power the light is on
output[i] = 1; //output is stored
else
output[i] = 0;
}
for(int i=1; i <= t; i++){
cout << "Case #" << i << ": ";
if(output[i]) //output for each case displayed
cout << "ON" << endl;
else
cout << "OFF" << endl;
}
return 0;
}