I am running this code on online compiler:
#include <iostream>
using namespace std;
int main () {
int x_coordinate = 0, y_coordinate = 0;
int numberOfMoves;
cin >> numberOfMoves;
// cout << "i went here" << x_coordinate << numberOfMoves;
string direction[numberOfMoves] = {"right", "up", "left", "down", "right"};
int dir[numberOfMoves] = {0, 1, 2, 3, 0};
for (int i = 1; i <= numberOfMoves; i++) {
// cout << "i went here";
int distance = (i%5)*10;
int currentDirection = dir[i % 5];
switch (currentDirection) {
case 0:
x_coordinate += distance;
break;
case 1:
y_coordinate += distance;
break;
case 2:
x_coordinate -= distance;
break;
case 3:
y_coordinate -= distance;
break;
default:
break;
}
}
cout << x_coordinate << y_coordinate << "\n";
return 0;
}
I get a segmentation error
. Can someone help me with this problem?
After reading numberOfMoves
the code is stopped to execute, I believe. But I do not know for sure.