2

We have an int 2D array as follow:

int matrix[4][4] =
{{1,2,3,4}
{5,6,7,8}
{9,10,11,12}
{13,14,15,16}};

By convention if we wants to print out the array by order we can:

    for (int x=0; x<4; x++)
    {
        for (int y=0; y<4; y++) 
        {                                        
            cout << matrix[x][y] << "  ";
        }   
        cout << endl;               
    }

Output:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

My question is: How can we traverse the 2D array in zigzag order. For example, print out the array values as such:

 1  2  3  4  
 8  7  6  5
 9 10 11 12
16 15 14 13
user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 2
    If you vote this down, kindly leave a comment here why u do so. So I can improve next time. – user3437460 Apr 04 '14 at 15:59
  • 1
    This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself. – πάντα ῥεῖ Apr 04 '14 at 15:59
  • @πάνταῥεῖ What other information do you need from this question? This question is as straight forward as you see here. – user3437460 Apr 04 '14 at 16:03
  • You are expected to show some efforts of your own here, what you have tried etc. So isn't a _Gimme teh codez plz!_ service. – πάντα ῥεῖ Apr 04 '14 at 16:05
  • @πάνταῥεῖ But I have seen nemerous questions in SO which accumulated over 500 positive votes without even stating what they had done. Some questions are as simple as one line. One of the numerous example is http://stackoverflow.com/questions/11694546/divide-a-number-by-3-without-using-operators – user3437460 Apr 04 '14 at 16:09
  • I am ok for anyone to vote down my questions if I really asked an open ended or duplicated bad question. But please don't vote my questions down blindly. – user3437460 Apr 04 '14 at 16:11
  • _'... One of the numerous example is ...'_ If you convince anyone that your question will be adopted by the community wiki, but as it looks I doubt so ... – πάντα ῥεῖ Apr 04 '14 at 16:11
  • 1
    +1, for fighting back :-) – iavr Apr 04 '14 at 17:23
  • Is Morton Code aka Z-Curve a solution to you? – Sebastian Mach Apr 04 '14 at 17:25
  • Please unhold this question. It has been reworded. – user3437460 Apr 04 '14 at 18:27

4 Answers4

3

How about

bool r=false;
for (int x=0; x<4; x++)
{
    for (int y=0; y<4; y++)
    {
        cout << matrix[x][r ? 3-y : y] << "  ";
    }
    cout << endl;
    r = !r;
}
iavr
  • 7,547
  • 1
  • 18
  • 53
  • I think that the ? counts as an if() instruction. But let the OP decide. Before the OP edited the question, the instructions were to not use if() in the solution. – PaulMcKenzie Apr 04 '14 at 17:22
  • @PaulMcKenzie Oh, I never saw that constraint. And I tried to have minimal changes compared to the code of the question, so it's not necessarily efficient, elegant or anything. – iavr Apr 04 '14 at 17:25
  • Gave you +1 anyway. Don't see it in the OP's post now. – PaulMcKenzie Apr 04 '14 at 17:25
1

Here is a solution (before you edited the original question, you asked for a solution to the original problem without using if() or conditionals):

#include <iostream>
using namespace std;
int main()
{
    int matrix[4][4] =
        {{1,2,3,4},
        {5,6,7,8},
        {9,10,11,12},
        {13,14,15,16}};

    bool backwards = false;        
    int incre = 1;
    for (int x=0; x < 4; x++)
    {
        int index = 3 * backwards;
        for (int y=0; y<4; y++, index += incre) 
            cout << matrix[x][index] << "  ";
        incre = -incre;
        backwards = !backwards;
        cout << endl;               
    }
}

The trick is that you want the column count to increase one row, decrease on the next row, etc. That's the reason for the incre variable.

The "backwards" is just a boolean that tells us if we're going backwards or forwards, thus setting up the proper index to start from.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

Here's a solution with a single for-loop.

#include <iostream>
using namespace std;

int matrix[][4] = { { 0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}};

int main() {
  for (int x=0; x<16; x++) {
    cout << matrix[x/4][ x%4 * ((x/4+1)&1) + (3-x%4)*((x/4)&1) ] << "  ";
    if (x%4==3) cout << endl;
  }
  return 0;
}

The first [] simply gives you the row by dividing by 4 -> i/4. The second [] gives the column in 2 steps. It takes the reminder after dividing by 4 -> x%4 and multiplies by 1 if it's an even row -> (x/4+1)&1 then adds the reminder in reverse order -> 3-x%4 multiplied by 1 if it's an odd row -> (x/4)&1

agbinfo
  • 793
  • 5
  • 17
0

A much simpler and easily understandable approach

#include <iostream>
using namespace std;
int main() {
    // your code goes here
    int matrix[4][4] = { { 0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}};
    bool forward = true;
    int j;
    for(int i = 0,k=0;i<4;i++)
    {
        if(forward)
        {
            for(j=0;j<4;j++)
            {
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
            forward = false;
        }
        else
        {
            for(j=3;j>=0;j--)
            {
                cout<<matrix[i][j]<<" ";
            }
            cout<<endl;
            forward = true;
        }
    }
    return 0;
}