2

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;
}
mr5
  • 3,438
  • 3
  • 40
  • 57
George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88
  • 1
    Where does the spiral start? If it started at the "center" of the array I would not expect an element on the edge to move to the "center"... Also, your curly-braces are very difficult to follow... – abiessu Nov 12 '13 at 17:44
  • 2
    It starts from the first element of it and goes on until the center. – George Irimiciuc Nov 12 '13 at 17:45
  • possible duplicate of [Looping in a spiral](http://stackoverflow.com/questions/398299/looping-in-a-spiral) – Sergey Kalinichenko Nov 12 '13 at 17:46
  • I don't want to loop through it, I want to replace the first 2 found with 0, and the next 0 with 2. The array is full of 0, and has one single number 2. – George Irimiciuc Nov 12 '13 at 17:48

1 Answers1

4

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 :).

John Odom
  • 1,189
  • 2
  • 20
  • 35