Search code examples
c++for-loopnumber-formatting

Generating odd numbers with specificaly exchanged even numbers


for short I didnt code for so long I forgot how to code in cpp (Im disapointed in myself...)

So the point is I want to print 4 page (A6) per sheet both sided on A4 paper. While I need to leave page number correct on the other side. So to do it I need to write specific page numbers (for example for A4: 1,3,5,7,4,2,8,6, to print it correctly).

While generate odd numbers is eazy there is problem with generating after 4 odd numbers, 4 even which must be exchanged specific ways like on example.

How can I make code for this specific problem to genetare numbers this way for document with 200 pages ?


Solution

  • You don't have to generate anything. Just put the pattern above 1,3,5,7,4,2,8,6 into an array and use that.

    For example

    int main()
    {
        static const int offset[]={1,3,5,7,4,2,8,6};
        for (int page = 0; page < 200; page += 8)
        {
            for (int i = 0; i < 8; ++i)
                cout << "Page #" << page+offset[i] << '\n';
        }
    }