Write on C
You are located in front of a stone wall with steps (two dimesional array) and each step has its own value- random integer between 1 and 500. You need to reach the top of the wall. To reach the top of the wall you need to do certain condition the top row should have a step with a value the difference between it and the step we are currently on should be less than or equal to a preset value K. Show on the screen "You won" if you can reach the top of the wall and which steps you climb. You start from a row with value of 0 for each step.
That's what I do, but sometimes doesn't work
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, x = 1, y = 0, max = 0, n, k, br, flag = 0;
int arr[3][3] = { { 0, 385, 16 },
{ 370, 170, 117 },
{ 469, 246, 288 }
};
int st[3][3];
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("
");
}
printf("
");
printf("Insert K: ");
scanf("%d", &k);
printf("
");
while (x < 3) {
flag = 0;
for (y = 0; y < 3; y++) {
if ((arr[x][y] - max) <= k) {
max = arr[x][y];
printf("Step %d
", arr[x][y]);
flag = 2;
break;
}
}
if (flag == 0) {
printf("You lose");
return 0;
}
x++;
}
printf("You won ");
return 0;
}