-1

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.

jsdledezma
  • 23
  • 3
  • https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – πάντα ῥεῖ Jan 17 '21 at 17:38
  • 4
    You have an off by one bug: `for(int i = 0; i <= 10; i++) {` is a bug, Your array has 10 elements not 11. `int a[10][10];` – drescherjm Jan 17 '21 at 17:40
  • 3
    When you write code, start with something small and simple that works perfectly, then add complexity a little at a time, testing at every step. You are painting the snake one stroke at a time, but if you try just the first *two,* you'll see that the second fails. Then if you look carefully at your variables, you'll see where you're going wrong. – Beta Jan 17 '21 at 18:32

1 Answers1

2

Kindly refer to this In first conv function I made a general pattern without snake Then I focused on positions from where the snake connects other rows or column

#include <bits/stdc++.h>
using namespace std;

void conv1(int a[10][10])
{
    int temp=0;
    for(int i=0; i<10; i++)
    {
        if(i%2==0)
        {
            for(int j=temp; j<10; j++)
            {
                a[i][j]=1;
                a[j][i]=1;
            }
        }
        temp=temp+1;
    }
}
/*
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0
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 0
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 0 1 0 1 0 1 0
*/
void conv2(int a[10][10])
{
    int temp=0;
    for(int i=0; i<10; i++)
    {
        if(i%2!=0&&temp==0)
        {
            a[i][9]=1;
            temp=1;
        }
        else if(i%2!=0)
        {
            temp=0;
        }
    }
    for(int i=0; i<10; i++)
    {
        if(i%2!=0&&temp==0)
        {
            a[9][i]=1;
            temp=1;
        }
        else if(i%2!=0)
        {
            temp=0;
        }
    }
    a[9][9]=0;
}

int main(){

    int a[10][10];
    for(int i=0; i<10;i++)
    {
        for(int j=0; j<10; j++)
        {
            a[i][j]=0;
        }
    }
    conv1(a);
    conv2(a);
    for(int i=0; i<10;i++)
    {
        for(int j=0; j<10; j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
}