Home > Net >  How to make square of numbers in opposite way?
How to make square of numbers in opposite way?

Time:10-22

I have made a code that makes this kind of output:

   1   2   3   4   5   6
   7   8   9  10  11  12
  13  14  15  16  17  18
  19  20  21  22  23  24
  25  26  27  28  29  30
  31  32  33  34  35  36
  37  38  39  40  41  42
  43  44  45  46  47  48
#include <iostream>
using namespace std;
int main() {
   // dimensions:
   int x=6,y=8;
   int sum=0;
   int numery[8][6]={};


   for (i = 0; i < y; i  ) {

        for (j = 0; j < x; j  ) {
            numery[i][j]=  sum;
            if (numery[i][j]<=9) cout << " ";
            cout << numery[i][j] << " ";
        }

        cout << endl;
    }
    return 0;
}

But I don't know how to edit it to get this kind of output:

   6   5   4   3   2   1
   7   8   9  10  11  12
  18  17  16  15  14  13
  19  20  21  22  23  24
  30  29  28  27  26  25
  31  32  33  34  35  36
  37  38  39  40  41  42
  43  44  45  46  47  48

I can think of making if statement for each i%2==1 that it should go backwards, but I don't know how to make program do such thing. Otherwise it should go normally. So if it find an even row it should go like 7 8 9 10 11 12, whereas if it's i%2==1 it should go like 6 5 4 3 2 1 and so on. Some suggestions?

CodePudding user response:

One solution would be the following (I assume that you do not really need to store the numbers). Notice that setw() can be used to print the numbers with fixed width:

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    // dimensions:
    int x=6,y=8;

    for (int i = 0; i < y; i  )
    {
        for (int j = 0; j < x; j  )
        {
            cout << setw(3) << ((i%2) ? i*x j 1 : i*x x-j) << " ";
        }
        cout << endl;
    }
    return 0;
}

CodePudding user response:

First get rid of the array. It is unnecessary.

#include <iostream>
using namespace std;

int number(int i,int j,int width) { return j i*width  1 ;}

int main() {
   // dimensions:
   int x=6,y=8;

   for (int i = 0; i < y; i  ) {
        for (int j = 0; j < x; j  ) {
            auto n = number(i,j,x);
            if (n <= 9) cout << " ";
            cout << n << "  ";
        }
        cout << endl;
    }
    return 0;
}

This has same output as your code.

Next consider what happens to j when you go reverse. You should think that through, the answer is: You replace j with width-j-1.

int number_reverse(int i,int j, int width) { return number(i,width-j-1,width); }

Eventually you just need to call the right function in each iteration of the outer loop:

#include <iostream>
using namespace std;

int number(int i,int j,int width) { return j i*width  1 ;}

int number_reverse(int i,int j, int width) { return number(i,width-j-1,width); }
int main() {
   // dimensions:
   int x=6,y=8;

   for (int i = 0; i < y; i  ) {
        for (int j = 0; j < x; j  ) {
            auto n = (i%2) ? number_reverse(i,j,x) : number(i,j,x);
            if (n <= 9) cout << " ";
            cout << n << "  ";
        }
        cout << endl;
    }
    return 0;
}
  •  Tags:  
  • c
  • Related