I am trying to develop the code for an exercise that was assigned to me, but I can't find any way to do what I need to accomplish. The exercise in question consists of changing and printing the values of a 10x10 2d array filled with zeroes generating a pattern like this:
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 1
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 1 1 0 1 1 1 0
It is probably not very noticeable, but starting from the index a[9][0] and ending at a[8][9] of the array it starts to go through changing those zeros of the array, forming a snake-like pattern. Thinking about it for a while, I tried to solve it with this code that I made, but I think it has several errors within it even though the compiler doesn't mark any so evident:
#include <iostream>
using namespace std;
int main()
{
int px = 0;
int py = 9;
int a[10][10];
for(int i = 0; i <= 10; i++) {
for(int j = 0; j <= 10; j++) {
a[i][j] = 0;
}
}
if (py == 9 && px == 0) {
while(py >= 0) {
a[py][px] = 1;
py = py - 1;
}
}
if (py == 0 && px == 0) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 0 && px == 9) {
while(py <= 2) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 2 && px == 9) {
while(px >= 2) {
a[py][px] = 1;
px = px - 1;
}
}
if (py == 2 && px == 2) {
while(py <= 9) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 9 && px == 2) {
while(px <= 4) {
a[px][py] = 1;
px = px + 1;
}
}
if (py == 9 && px == 4) {
while(py >= 4) {
a[px][py] = 1;
py = py - 1;
}
}
if (py == 4 && px == 4) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 4 && px == 9) {
while(py <= 6) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 6 && px == 9) {
while(px >= 6) {
a[py][px] = 1;
px = px - 1;
}
}
if (py == 6 && px == 6) {
while(py <= 9) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 9 && px == 6) {
while(px <= 8) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 9 && px == 8) {
while(py >= 8) {
a[py][px] = 1;
py = py - 1;
}
}
if (py == 8 && px == 8) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
for(int k=0; k<=9; k++) {
for(int l=0; l<=9; l++) {
cout << a[k][l] << " ";
}
cout << endl;
}
return 0;
}
I hope you can help me with this, still I continue learning a little the basic thing of C++, probably it has been that it has escaped me some concept that I don't know or something like that. Thanks in advance.