Question

I am trying to make this go through the array in a spiral order. When it finds 2, it should replace it with 0 and the next number in the spiral order should become 2. So, if my array is

000
200
000

is should become

000
020
000

The variable ok tells me if I found that number 2 and simply modifies the next number to 2. Note that it doesn't loop through it. When It reaches the center of the array, it stops and doesn't go backwards or starts over.

Any ideas why it doesn't work? It simply doesn't modify my array at all.

#include<iostream>
using namespace std;

#define ROWS 3
#define COLS 3

int main()
{
    int arr[ROWS][COLS] = {{2,0,0},
                           {0,0,0},
                           {0,0,0}};


    // Four direction counters of current movement
    // Horizontal right, vertical bottom, horizontal left and vertical top respectively
    int hr, vb, hl, vt, ok=0;

    // levl indicates current depth of our imaginary rectangle into array. Starting value is zero
    // since we are looping on the boundaries and ending value is the inner most rectangle

    int levl;
    for (levl=0; levl < COLS - levl; levl++)
    {
        for(hr=levl; hr < COLS-levl; hr++)   // go right
        {
            if (ok==1)
            {
                arr[levl][hr] == 2;
                ok = 2;
            }

            if ( (arr[levl][hr] == 2) && (ok == 0) )
            {
                arr[levl][hr] == 0;
                ok = 1;
            }

        }

        for(vb=levl+1; vb < COLS-levl; vb++) // go down
        {
            if (ok == 1)
            {
                arr[vb][hr-1] == 2;
                ok = 2;
            }

            if ( (arr[vb][hr-1] == 2) && (ok == 0) )
            {
                arr[vb][hr-1] == 0;
                ok = 1;
            }

        }

        for(hl=vb-1; hl-1 >= levl; hl--)  // go left
        {
            if ( ok == 1)
            {
                arr[vb-1][hl-1] == 2;
                ok = 2;
            }

            if ( (arr[vb-1][hl-1] == 2) && (ok == 0) )
            {
                arr[vb-1][hl-1] == 0;
                ok = 1;
            }


        }

        for(vt=vb-1; vt-1 > levl; vt--)  // go up
        {
            if (ok == 1)
            {
                arr[vt-1][hl] == 2;
                ok = 2;
            }

            if ( (arr[vt-1][hl] == 2) && (ok==0) )
            {
                arr[vt-1][hl] == 0;
                ok = 1;
            }


        }

    }

    cout << endl;
    for(int t = 0;t < 3;t++)
    {

        for(int u = 0;u < 3;u++)
            cout<<arr[t][u]<<" ";

        cout<<endl;
    }

    int a;
    cin>>a;

    return 0;
}
Était-ce utile?

La solution

The reason that your array is not being modified is because you are using "==" instead of "=". So

if ((arr[levl][hr] == 2)&&(ok==0))
{
    arr[levl][hr] == 0;
    ok=1;
}

should be

if ((arr[levl][hr] == 2)&&(ok==0))
{
    arr[levl][hr] = 0;
    ok=1;
}

== Is a comparison operator and = assigns the value. Check your code very carefully and make it more readable for you could be able to find easy mistakes like that :).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top