Edit: In case it is of help, variable carr is true if the crane is carrying a box else it is false. Variable curr is the current position of the crane (zero-indexed).
So the bug in the program is the break statement in cases 3 and 4. Put the break statement out of the if condition.
Updated Source-Code
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, h;
cin >> n;
cin >> h;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int a;
int curr = 0;
bool carr = false;
bool t = true;
while (t) {
cin >> a;
switch (a) {
case 1:
if (curr != 0) curr -= 1;
break;
case 2:
if (curr != n - 1) curr += 1;
break;
case 3:
if (!(carr) && arr[curr] != 0) {
carr = true;
arr[curr] -= 1;
}
break;
case 4:
if (carr && arr[curr] < h) {
carr = false;
arr[curr] += 1;
}
break;
default:
t = false;
break;
}
}
for (int i = 0; i < n; i++) cout << arr[i] << ' ';
return 0;
}
Refer the link Video Game for solution in other programming language.
Suggestion: Next time when you upload your code, do make it readable, because your code is messy even though you have formatted the code.
Writing clean and understandable code is important both as a software developer and while posting on a online forum for help.