### How to loop over pairs and odds in the same loop?

Hey,

I question comes in mind is there any option to write a loop (for, while, do while)
that allows looping over pairs then impairs

something like this
 ``123456`` ``````for( ...) cout << i<< endl; std output: 0 2 4 6 8 1 3 5 7 9 ``````
Last edited on
There's something ugly like this.

 ``1234567891011`` `````` bool looped = false; for (int i = 0; i < 10; i+=2) { std::cout << i << '\n'; if (!looped && ( i==limit - 2 || i == limit-1)) { i = -1; looped = true; } }``````
Last edited on
@Repeater

What is limit? It is undefined here.
you can load up a container with the values you want to loop over and do a range-based for loop. This is extra useful when there is no simple pattern to the values, eg 2 digit pairs of pi (31 41 59 ...)

or you can use a pattern if one exists to control a loop variable.

if you need to deal with pairs you can do that too, there is a pair container for that.

can you describe exactly what you want to DO here? We can make that output print a dozen ways, but if you need to do something with values in some way, we need a better understanding.
Last edited on
 What is limit? It is undefined here.

See if you can guess, based on the context at hand.
Repeater, yeah but then why hardcode the 10 then?

Anyway, here's my attempt.
 ``123456789101112131415161718192021222324252627282930`` ``````// Example program #include int unit_step(int n) { return (n >= 0) ? 1 : 0; } void print_sequence(int start, int end, int N) { constexpr static auto u = unit_step; int offset = (N-1)/2 + 1; for (int n = start; n < end; n++) { std::cout << 2 * n * u(n) - (N-1) * u(n - offset) << "\t"; } std::cout << "\n"; } int main() { int N = 10; int cutoff = N; N = N + N % 2; print_sequence(0, N/2, N); print_sequence(N/2, cutoff, N); }``````

Example output, N = 10
 ```0 2 4 6 8 1 3 5 7 9 ```

Example output, N = 11
 ```0 2 4 6 8 10 1 3 5 7 9 ```
Last edited on
 Repeater, yeah but then why hardcode the 10 then?

Who knows. I expect I just threw down some code as an example. If you'd like to change it, please go ahead.
 ``123456789101112`` ``````#include #include #include #include using namespace std; int main () { const int limit = 10; stringstream ss[2]; for( int i = 0, line = 0; i < limit; i++, line = 1 - line ) ss[line] << setw( 3 ) << i; cout << ss[0].str() << '\n' << ss[1].str() << '\n'; }``````
Ooh, alternating the line printed between 0 and 1, very succinct.
@lastchance, You can get rid of line and just use ss[i % 2]
In this instance, yes. But i might not always be running between 0 and 9.
Whatever.
I think the answers show that the cleanest solution is two loops.
 ``1234`` ``````for (size_t i=0; i<10; i+=2) cout << i << ' '; cout << '\n'; for (size_t i=1; i<10; i+=2) cout << i << ' '; cout << '\n';``````

If you're really pressed for space, you could put it in a function:
 ``1234567891011`` ``````void printEveryOther(unsigned from, unsigned 10) { for (size_t i=from; i

Why not this?
 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main() { int a = 12; for (int i = 0; i <= a; i += 2) { cout << i << "\t"; } cout << endl; for (int i = 1; i <= a; i += 2) { cout << i << "\t"; } cin.get(); return 0; }``````
That's way too deconvoluted! :)
Last edited on
it's definitely unusual to iterate over every other index but still care about the indices skipped.

Thus, as lastchance wrote, some form of storage is required. You could output the evens as you go (since this is the first line), storing just the odds, but take care on choosing your starting and ending points. A few ways to do that; for example:

 ``12345678910111213141516171819`` ``````#include #include using namespace std; int main() { const int limit = 10; ostringstream oss; cout << "0 "; for (int i=2; i<=limit; i+=2) { cout << i << ' '; oss << (i-1) << ' '; } cout << '\n' << oss.str() << '\n'; return 0; }``````

 ```0 2 4 6 8 10 1 3 5 7 9 ```
Last edited on
Hey, I could obfuscate with the best of em'. Don't push me. :P
Besides, here is the same in a single loop.
 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main() { int a = 12; for (int i = 0; i <= a; i += 2) { cout << i << "\t"; if (i + 2 > a && i % 2 == 0) { i = -1; cout << endl; } } cin.get(); return 0; }``````
manga wrote:
Hey, I could obfuscate with the best of em'. Don't push me. :P

I'm working on it ...

 ``1234567891011121314151617`` ``````#include #include using namespace std; void output( int imin, int imax, int di, int nlines, int i, int line ) { if ( i > imax ) { line++; cout << '\n'; i = imin + line * di; } if ( line == nlines ) return; cout << setw( 4 ) << i; output( imin, imax, di, nlines, i + nlines * di, line ); } int main() { output( 0, 11, 1, 2, 0, 0 ); }`````` ``` 0 2 4 6 8 10 1 3 5 7 9 11```
@lastchance

I smell a bit of recursion in that one... nice. :)

Just remember to use function calls in your argument list.
 ``12345678910111213141516171819202122`` ``````#include #include using namespace std; void output(int imin, int imax, int di, int nlines, int i, int line) { if (i > imax) { line++; cout << '\n'; i = imin + line * di; } if (line == nlines) return; cout << setw(4) << i; output(imin, imax, di, nlines, i + nlines * di, line); } int fluxCap(int x) { x > 0 ? x = x - x: x = 0; return x; } int main() { output(fluxCap(3), abs(-10 - 1), sizeof(true), sizeof(short), fluxCap(-1), fluxCap(18)); }``````
Last edited on
Registered users can post here. Sign in or register to post.