1

"Number pong" is what I am trying to do. Ex:

0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 etc

I have tried several different things, incrementing one number, modal operators. I could not figure this out, and I could not figure out correct search words.

So:

int offset = 0;
int number = 0;

while(true) {
    offset++;
    number = offset%5; // idea 1
    number = (offset%5)-5 // idea 2
    number = (offset/5)%5 // idea 3
    number = 5 - (offset%5) // idea 4
}

None of those work, obviously. I get patterns like 0 1 2 3 4 5 0 1 2 3 4 5 or just continuous numbers.

Evan Carslake
  • 2,267
  • 15
  • 38
  • 56

8 Answers8

2

I would wrap this in an if(offset % 10 <= 5) { ... } else { ... } and use your existing ideas.

Regardless you're going to want to work % 10 since that's how long your cycle is.

djechlin
  • 59,258
  • 35
  • 162
  • 290
2

Hint These sequences are very closely related:

0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 ...
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 ...
Barry
  • 286,269
  • 29
  • 621
  • 977
2
#include <iostream>

int main()
{
  int i = 0;
  bool plus = true;
  while(true) {
    std::cout << i << std::endl;
    if (plus) i++; else i--;
    if (i == 5 || i == 0) plus = !plus;
  }
}
Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
2

Is there a requirement to generate the numbers in a single statement with variables and operators?

If not, then use an bool variable which switches its value (true means increasing, false means decreasing) from true to false and vice versa.

i.e.

int start = 0 ;
bool which_way = true ;
int loop_times = 100 ;
while(--loop_times) {
    std::cout << start ;
    start += which_way ? 1 : -1 ;
    if(start % 5 == 0)
        which_way = !which_way ;
}
a_pradhan
  • 3,285
  • 1
  • 18
  • 23
1

Here is a crazy way of outputting the number pong (with set limit)

#include <stdio.h>

int main()
{
    bool bFlip = false; //Decides if number will increase or decrease
    int nLimit = 5; //How far up the number will count.

    //Start at 0, keep going as long as number never reaches past the limit
    //And increase/decrease depending on bFlip
    for(int nNum = 0; nNum  <= nLimit; (bFlip ? nNum++ : nNum--))
    {
        printf("%d ", nNum);

        //When number reaches either end, do a barrel roll!
        if (nNum  % nLimit == 0)
        {
            bFlip = !bFlip;
        }
    }

    return 0;
}

Be warned that this loop will go on forever so if you are going to go with this approach then you will need to set a limit on how many numbers you want to display.

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

Yet another crack at generating the sequence you're after:

#include <iostream>
#include <list>
#include <iterator>

int main() {
    std::list<int> nums = {{0, 1, 2, 3, 4, 5}};
    auto begin = nums.begin();
    auto iterator = nums.begin();
    auto end = nums.end();
    auto loop_times = 100;

    while (--loop_times) {
      while (iterator != end) {
        std::cout << *iterator++;
      }
      iterator--;
      while (iterator != begin) {
        std::cout<< *--iterator;
      }
      iterator++;
    }
}
jdphenix
  • 15,022
  • 3
  • 41
  • 74
1

I'd probably do something like this:

// If you want in the range -val to val
//#define PONG(val, i) (abs(val%(i*4)-i*2) - i)
// If you want the range 0 to val
#define PONG(val, i) (abs(val%(i*2)-i))

int main() {
    for(int i = 0; i < 100; i++) {
        cout << PONG(i, 5) << endl;
    }
}

Prints:

5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 ...
Nick
  • 158
  • 1
  • 7
  • eww, why the unnecessary use of macros? – djechlin May 08 '15 at 20:21
  • There is only one macro, and its a lot cleaner than a function if there is only one line of code to execute – Nick May 08 '15 at 20:22
  • for what definition of "cleaner"? – djechlin May 08 '15 at 20:24
  • Your macro is broken for values like i=2-1. – djechlin May 08 '15 at 20:26
  • Cleaner than `int pong(int val, int i){return abs(val%(i*2)-i);}` looks – Nick May 08 '15 at 20:26
  • Are you just nit picking it now? It's not like I battle tested it with a ton of unit tests – Nick May 08 '15 at 20:27
  • Thanks for the completely unnecessary and useless link on how macros work. Like I said, I didn't battle test it, and if the OP feels the need to, he can move it out into a function. – Nick May 08 '15 at 20:35
  • minus one for posting needlessly bad code / advice. – djechlin May 08 '15 at 20:45
  • I fail to see how its "bad code", it sounds like you are just parroting what you've heard else where with no no clue what your talking about. If you don't want a macro to behave badly, DON'T PASS STUPID INPUT TO IT. – Nick May 08 '15 at 20:49
  • That's an incorrect viewpoint but it's not my job to educate you on this; you can read *Code Complete* or martin fowler's *Refactoring* if you're still curious. – djechlin May 08 '15 at 20:53
  • Seriously, if i want to save a function definition by putting it in a macro it isn't bad practice. I mean if you have `PONG` your first thought should be "This looks like a macro! Maybe I shouldn't do something dumb like x++ or i+1!" If it was something that would span several lines or need to be wrapped in braces or something stupid like that then yeah. – Nick May 08 '15 at 20:59
1

Thanks for the tips. I got it working with a single statement.

int count = 0;
int num = 0;
int out = 0;

while (count++ < 100) {

    cout << abs( (num%10) - 5 ) << endl;
    num++;

}

// Output: 5 4 3 2 1 0 1 2 3 4 5 4 etc
Evan Carslake
  • 2,267
  • 15
  • 38
  • 56