C++ Number Pong - c++

"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.

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.

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 ...

#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;
}
}

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 ;
}

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.

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++;
}
}

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

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 ...

Related

C++ Array (disregarding a repeat number)

I am a beginner programmer and I need some assistance.
I need to write a program that reads an array of 10 numbers from a user, then scans it and figures out the most common number/s in the array itself and prints them. If there is only one number that is common in the array, only print that number. But, if there's more than one number that appears more than once, print them also in the order they appear in in the array.
For example- 1 2 3 3 4 5 6 7 8 9 - output would be 3
For- 1 2 3 4 1 2 3 4 5 6 - output would be 1 2 3 4
for- 1 1 1 1 2 2 2 3 3 4 - output would be 1 2 3
Now, the problem I've been running into, is that whenever I have a number that repeats more than twice (see third example above), the output I'm getting is the number of iterations of the loop for that number and not only that number once.
Any assistance would be welcome.
Code's attached below-
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int array [10], index, checker, common;
main ()
{
for (index=0; index<10; index++)
{
cin >> array [index];
}
for (index=0; index<10; index++)
{
int tempcount=0;
for (checker=(index+1);checker<10;checker++)
{
if (array[index]==array[checker])
tempcount++;
}
if (tempcount>=1)
cout << array[index]<<" ";
}
return 0;
}
Use appropriate data structures for the task.
Create a std::unordered_map that maps value to number_of_occurrences, and make a single pass over the input data.
Then create another map from number_of_occurrences to value. Sort it, in descending order. Report the first value, plus any additional ones that occurred as many times as the first did.
The reason you are having problems is that anytime a number appears two times or more it will print out. A solution is that you create another variable maxCount, then find the maximum times a number appears. Then loop through the array and print out all the numbers that appears the maximum amount of times.
Hope this helps.
Jake
Rather than writing you a solution, I will try to give you some hints that you can hopefully use to correct your code. Try to keep track of the following things:
Remember the position of the first occurrence of each distinct number in the array.
Count the number of times each number appears
and combine the two to get your solution.
EDIT:
int array[] = {1, 2, 3, 4, 1, 2, 3, 4, 5, 6};
int first [11], cnt[11];
for(int i = 0; i < 11; i++){
first[i] = -1;
cnt[i] = 0;
}
int max = 0;
for(int i = 0; i < 10; i++){
cnt[array[i]]++;
if(max < array[i]) max = array[i];
}
for(int i = 0; i <= max; i++){
if(cnt[i] > 1 && first[i] == -1) {
printf(" %d", i);
first[i] = i;
}
}
You could do something like this. At any index in the array look for previous occurences of that element. If you find that that it is the first occurence of that element, you only need to look if there is an occurence of that element ahead in the array.
Lastly display the element whose frequency(here num) would be greater than 1.
for (int i = 0; i < 10; i++)
{
int presentBefore = 0;
for (int j = 0; j < i; j++) //if any previous occurence of element
{
if (array[i] == array[j]) presentBefore++;
}
if (presentBefore == 0)//if first occurence of the element
{
int num = 1;
for (int j = i + 1; j < 8; j++)// if occurences ahead in the array
{
if (array[i] == array[j]) num++;
}
if(num>1)cout<<array[i]<<" ";
}
}
Here is another solution using STL and std::set.
#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>
int main()
{
int array[12] = { 1, 2, 3, 1, 2, 4, 5, 6, 3, 4, 1, 2 };
std::set<int> dupes;
for (auto it = std::begin(array), end = std::end(array); it != end; ++it)
{
if (std::count(it, end, *it) > 1 && dupes.insert(*it).second)
std::cout << *it << " ";
}
return 0;
}
Prints:
1 2 3 4
I will try to explain how this works:
The original array is iterated from start to finish (BTW as you can see it can be any length, not just 10, as it uses iterators of beginning and end)
We are going to store duplicates which we find with std::count in std::set
We count from current iterator until the end of the array for efficiency
When count > 1, this means we have a duplicate so we store it in set for reference.
std::set has unique keys, so trying to store another number that already exists in set will result in insert .second returning false.
Hence, we print only unique insertions, which appear to be in the order of elements appearing in the array.
In your case you can use class std::vector which allows you to Erase elements, resize the array...
Here is an example I provide which produces what you wanted:
1: Push the values into a vector.
2: Use 2 loops and compare the elements array[i] and array[j] and if they are identical push the the element j into a new vector. Index j is always equal to i + 1 in order to avoid comparing the value with itself.
3- Now you get a vector of the repeated values in the temporary vector; You use 2 loops and search for the repeated values and erase them from the vector.
4- Print the output.
NB: I overloaded the insertion operator "<<" to print a vector to avoid each time using a loop to print a vector's elements.
The code could look like :
#include <iostream>
#include <vector>
std::ostream& operator << (std::ostream& out, std::vector<int> vecInt){
for(int i(0); i < vecInt.size(); i++)
out << vecInt[i] << ", ";
return out;
}
int main() {
std::vector< int > vecInt;
//1 1 1 1 2 2 2 3 3 4
vecInt.push_back(1);
vecInt.push_back(1);
vecInt.push_back(1);
vecInt.push_back(1);
vecInt.push_back(2);
vecInt.push_back(2);
vecInt.push_back(2);
vecInt.push_back(3);
vecInt.push_back(3);
vecInt.push_back(4);
std::vector<int> vecUniq;
for(int i(0); i < vecInt.size(); i++)
for(int j(i + 1); j < vecInt.size(); j++)
if(vecInt[i] == vecInt[j])
vecUniq.push_back(vecInt[j]);
std::cout << vecUniq << std::endl;
for(int i = 0; i < vecUniq.size(); i++)
for(int j = vecUniq.size() - 1 ; j >= 0 && j > i; j--)
if(vecUniq[i] == vecUniq[j])
vecUniq.erase(&vecUniq[j]);
std::cout << vecUniq << std::endl;
std::cout << std::endl;
return 0;
}
The input: 1 2 3 3 4 5 6 7 8 9
The output: 3
The input: 1 2 3 4 1 2 3 4 5 6
The output: 1 2 3 4
The input: 1 1 1 1 2 2 2 3 3 4
The output: 1 2 3
For this problem, you can use a marking array that will count the number of times you a digit is visited by you, it's just like counting sort. let's first see the program :
#include <iostream>
using namespace std;
int print(int a[],int b[])
{
cout<<"b :: ";
for (int index=0;index<10;index++)
{
cout<<b[index]<<" ";
}
cout<<endl;
}
int main ()
{
int a[10],b[11], index, checker, common;
for (index=0; index<10; index++)
{
cin >> a [index];
b[index] = 0;
}
b[10] =0;
for (index=0;index<10;index++)
{
b[a[index]]++;
if (b[a[index]] == 2)
cout<<a[index];
//print(a,b);
}
return 0;
}
As you can see that I have used array b as marking array which counts the time a number is visited.
The size of array b depends upon what is the largest number you are going to enter, I have set the size of array b to be of length 10 that b[11] as your largest number is 10. Index 0 is of no use but you need not worry about it as it will be not pointed until your input has 0.
Intially all elements in array in b is set 0.
Now assume your input to be :: 1 2 3 4 1 2 3 4 5 6
Now value of b can be checked after each iteration by uncommenting the print function line::
b :: 0 1 0 0 0 0 0 0 0 0 ....1
b :: 0 1 1 0 0 0 0 0 0 0 ....2
b :: 0 1 1 1 0 0 0 0 0 0 ....3
b :: 0 1 1 1 1 0 0 0 0 0 ....4
b :: 0 2 1 1 1 0 0 0 0 0 ....5
b :: 0 2 2 1 1 0 0 0 0 0 ....6
b :: 0 2 2 2 1 0 0 0 0 0 ....7
b :: 0 2 2 2 2 0 0 0 0 0 ....8
b :: 0 2 2 2 2 1 0 0 0 0 ....9
b :: 0 2 2 2 2 1 1 0 0 0 ....10
In line 5 you can b's at index 1 has value 2 so it will print 1 that is a[index].
And array a's element will be printed only when it is repeated first time due to this line if(b[a[index]] == 2) .
This program uses the idea of counting sort so if you want you can check counting sort.

Algorithm for Combinations of given numbers with repetition? C++

So I N - numbers I have to input, and I got M - numbers of places for those numbers and I need to find all combinations with repetition of given numbers.
Here is example:
Let's say that N is 3(I Have to input 3 numbers), and M is 4.
For example let's input numbers: 6 11 and 533.
This should be result
6,6,6,6
6,6,6,11
6,6,6,533
6,6,11,6
...
533,533,533,533
I know how to do that manualy when I know how much is N and M:
In example where N is 3 and M is 4:
int main()
{
int N = 3;
int M = 4;
int *numbers = new int[N + 1];
for (int i = 0; i < N; i++)
cin >> numbers[i];
for (int a = 0; a < N; a++)
for (int b = 0; b < N; b++)
for (int c = 0; c < N; c++)
for (int d = 0; d < N; d++)
{
cout << numbers[a] << " " << numbers[b] << " " << numbers[c] << " " << numbers[d] << endl;
}
return 0;
}
But how can I make algorithm so I can enter N and M via std::cin and I get correct resut?
Thanks.
First one short tip: don't use "new" or C-style arrays in C++ when we have RAII and much faster data structures.
For the solution to your problem I would suggest making separate function with recursion. You said you know how to do it manually so the first step in making it into algorithm is to tear down you manual solution step by step. For this problem when you solve it by hand you basically start with array of all first numbers and then for last position you just loop through available numbers. Then you go to the second last position and again loop through available numbers just now with the difference that for every number there you must also repeat the last spot number loop. Here is the recursion. For every "n"th position you must loop through available numbers and for every call the same function for "n+1"th number.
Here is a simplified solution, leaving out the input handling and exact print to keep code shorter and more focused on the problem:
#include <vector>
#include <iostream>
void printCombinations(const std::vector<int>& numbers, unsigned size, std::vector<int>& line) {
for (unsigned i = 0; i < numbers.size(); i++) {
line.push_back(numbers[i]);
if (size <= 1) { // Condition that prevents infinite loop in recursion
for (const auto& j : line)
std::cout << j << ","; // Simplified print to keep code shorter
std::cout << std::endl;
line.erase(line.end() - 1);
} else {
printCombinations(numbers, size - 1, line); // Recursion happens here
line.erase(line.end() - 1);
}
}
}
int main() {
std::vector<int> numbers = {6, 11, 533};
unsigned size = 4;
std::vector<int> line;
printCombinations(numbers, size, line);
return 0;
}
If you have any questions feel free to ask.
Totally there is no need for recursion here. This is a typical job for dynamic programming. Just get the first solution right for n = 1 (1 slot is available) which means the answer is [[6],[11],[533]] and then move on one by one by relying on the one previously memoized solution.
Sorry that i am not fluent in C, yet in JS this is the solution. I hope it helps.
function combosOfN(a,n){
var res = {};
for(var i = 1; i <= n; i++) res[i] = res[i-1] ? res[i-1].reduce((r,e) => r.concat(a.map(n => e.concat(n))),[])
: a.map(e => [e]);
return res[n];
}
var arr = [6,11,533],
n = 4;
console.log(JSON.stringify(combosOfN(arr,n)));
Normally the easiest way to do dynamic nested for loops is to create your own stack and use recursion.
#include <iostream>
#include <vector>
void printCombinations(int sampleCount, const std::vector<int>& options, std::vector<int>& numbersToPrint) {
if (numbersToPrint.size() == sampleCount) {
// got all the numbers we need, print them.
for (int number : numbersToPrint) {
std::cout << number << " ";
}
std::cout << "\n";
}
else {
// Add a new number, iterate over all possibilities
numbersToPrint.push_back(0);
for (int number : options) {
numbersToPrint.back() = number;
printCombinations(sampleCount, options, numbersToPrint);
}
numbersToPrint.pop_back();
}
}
void printCombinations(int sampleCount, const std::vector<int>& options) {
std::vector<int> stack;
printCombinations(sampleCount, options, stack);
}
int main()
{
printCombinations(3, {1,2,3});
}
output
1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
Here is an algorithm to solve this, that does't use recursion.
Let's say n=2 and m=3. Consider the following sequence that corresponds to these values:
000
001
010
011
100
101
110
111
The meaning of this is that when you see a 0 you take the first number, and when you see a 1 you take the second number. So given the input numbers [5, 7], then 000 = 555, 001=557, 010=575 etc.
The sequence above looks identical to representing numbers from 0 to 7 in base 2. Basically, if you go from 0 to 7 and represent the numbers in base 2, you have the sequence above.
If you take n=3, m=4 then you need to work in base 3:
0000
0001
0002
0010
0011
0012
....
So you go over all the numbers from 0 to 63 (4^3-1), represent them in base 3 and follow the coding: 0 = first number, 1 = second number, 2 = third number and 3 = fourth number.
For the general case, you go from 0 to M^N-1, represent each number in base N, and apply the coding 0 = first number, etc.
Here is some sample code:
#include <stdio.h>
#include <math.h>
void convert_to_base(int number, char result[], int base, int number_of_digits) {
for (int i = number_of_digits - 1; i >= 0; i--) {
int remainder = number % base;
number = number / base;
result[i] = '0' + remainder;
}
}
int main() {
int n = 2, m = 3;
int num = pow(n, m) - 1;
for (int i = 0; i <= num; i++) {
char str[33];
convert_to_base(i, str, n, m);
printf("%s\n", str);
}
return 0;
}
Output:
000
001
010
011
100
101
110
111

Calculate the maximum number of consecutive elements without repetitions in a vector. c++

Hi guys i've to calculate the longest sequence of numbers without any repetitions and return the size of the sub-segment.
The point is that im missing something at some point but I don't know where.
int resolverCaso() {
int num;
int cont = 0;
cin >> num;
int var;
TreeMap<int,int> a;
int aux;
int max = 0;
for (int i = 0; i < num; i++) {
cin >> var;
if (!a.contains(var)) {
a[var] = i;
aux = var;
cont++;
}
else {
if (a[aux]==i-1 && var==aux) {
cont = 1;
a = TreeMap<int, int>();
a[var] = i;
}
else {
a.erase(var);
a[var] = i;
}
}
if (cont > max) {
max = cont;
}
}
return max;
}
I've tried the following cases with this outputs and everything seems to be ok.
E:1 2 3 1 2 3 O:3
E:2 2 2 2 O:1
E:4 5 6 7 6 O:4
E:7 8 9 10 7 8 9 11 2 O:6
E:7 8 9 10 10 10 1 2 3 4 O:5
E:3 4 2 3 4 2 8 9 10 11 O:7
E:0 O:0 ( empty vector ).
E:1 O:1
So basically im looking for some sequence that doesn't work with my code.
Thanks.
The problem is with
else {
a.erase(var);
a[var] = i;
}
You need to do more here. Try the sequence 1 3 4 2 3 4 2 8 9 10 11.

Brute Force Permutation Swapping

I've been working on a brute force algorithm to generate all permutations of a given set. Eventually, I want to feed each of these permutations into a nxn matrix to test if it is a valid magic square or not.
--I KNOW THAT THERE IS A WAY TO GENERATE A MAGIC SQUARE EASILY--
That is not what I want to do, though. I'm focusing on the brute force aspect of it.
For a set of 3 elements, it works wonderfully. However, once I use 4 or more elements, I lose out on a few permutations. Just from looking at the output of 4, I am missing 7 permutations.
#include <stdio.h>
#include <iostream>
using namespace std;
//ms = magic square
//n = size
void perm(int ms[], int n) {
int pivot = 0;
int index = 0;
int pivBit = 1;
int fin = 0;
int hold = 0;
//While we are not finished
while (fin == 0) {
//Incriment the index
++index;
if (index >= n) {
index = 0;
}
//if index is equal to the pivot
if (index == pivot) {
//Is this the first time visiting the pivot?
if (pivBit == 0) {
//Are we at the beginning again?
if (index == 0 && pivot == 0)
{
fin = 1;
}
pivBit = 1;
++index;
}
//Second time visiting?
else {
pivBit = 0;
++pivot;
if (pivot >= n) {
pivot = 0;
}
}
}
//If we are out of bounds
if (index >= n) {
index = 0;
}
//swap
hold = ms[index];
ms[index] = ms[pivot];
ms[pivot] = hold;
for (int i = 0; i < n; ++i) {
cout << ms[i];
if (i < n - 1) {
cout << ", ";
}
else {
cout << endl;
}
}
}
}
int main() {
cout << "Are you ready to brute force, my brother?" << endl;
//Set
int magicsquare[] = { 1, 2, 3, 4};
int size = 4;
perm(magicsquare, size);
getchar();
return 0;
}
My output is:
2 1 3 4
3 1 2 4
4 1 2 3
1 4 2 3
1 2 4 3
1 3 4 2
3 1 4 2
3 4 1 2
3 4 2 1
2 4 3 1
2 3 4 1
2 3 1 4
4 3 1 2
4 2 1 3
4 2 3 1
1 2 3 4
2 1 3 4
Looking at it, I can already see that I am missing both 1 4 3 2 and 1 3 2 4.
Where've I gone wrong in my algorithm?
The wiki article on permutation includes a common algorithm used to produce all permutations in lexicographic order, starting with an array of sequentially increasing integers, ending with that array reversed. wiki next permutation.
If dealing with an array of objects, you can generate an array of indices 0 through n-1 and use next permutation on the indices to produce all permutations of the array of objects.
You can also do a web search for next permutation to find similar algorithms. The recursive ones produce all permutations, but not in lexicographic order.
The simplest way to generate all permutations is recursive. For each i, swap the i'th element to the 0 position. Then recursively find all permutations of of the remaining array.
int buf[1000], n; // better to wrap these in a class...
void permute(int *a, int a_len) {
if (a_len == 1) {
for (int i = 0; i < n; i++) printf("%d ", buf[i]);
printf("\n");
} else {
for (int i = 0; i < a_len; i++) {
swap(a, 0, i);
permute(a + 1, a_len - 1);
swap(a, 0, i);
}
}
}
void run(int buf_len) {
for (int i = 0; i < buf_len; i++) buf[i] = i + 1;
n = buf_len;
permute(buf, buf_len);
}
This assumes no repeated elements in the original array. It's not to hard to have it take repeated elements into account.

Is there any best way(math / C++ trick) of Iterate through forward and reverse within a give range

Using modular arithmetic (or) (%) operator in C++ we can cycle through the successive numbers with a range.
For example:
if range is 5 (or) modulo 5 then
we can cycle through
0 1 2 3 4 0 (5) 1(6) 2(7) 3(8) 4(9) 0(10)............0 1 2 3 etc.
Question:
In the similar sense is there any arithmetical relation / C++ trick we can use to move increasing numbers in forward(till upper bound) and decreasing numbers in reverse direction(till lower bound or 0) with a range.
For example:
if range = 5 then
0 1 2 3 4 3 2 1 0 1 2 3 4 3 2 1 0.....................0 1 2 3 etc.
In the below program I used two approaches to iterate forward/reverse within a given range.
But I'm interested in- Is there any best way (C++ trick/Mathematical relation) of iterate through forward and reverse within a given range ?.
#include<iostream>
int main() {
int range = 5;
// 0 1 2 3 4 0 1 2 3 4 .....(Cycle through in the range 0 - 4)
int i = 0;
while(true) {
// 0 1 2 3 4 0 1 2 3 4 .....(cycle through in the range 0 - 4)
std::cout<< i;
i = (i+1)% range; // Modulo
// some break condition
}
// 0 1 2 3 4 3 2 1 0 .......... (Forward and Reverse in the range 0 - 4)
// Method 1:
int j = 0;
bool reverse = false;
while(true) {
if(reverse == false) {
if(j < range) {
std::cout << j;
j = j+1;
}
else {
reverse = true;
j = j-1;
}
}
else {
j = j-1;
std::cout << j;
if(j == 0) {
reverse = false;
j = j + 1;
}
}
// some break condition
}
// 0 1 2 3 4 3 2 1 0 .......... (Forward and Reverse in the range 0 - 4)
// Method 2:
// Using modulo (if the range is big value then this is not good approach)
int limit[8] = {0,1,2,3,4,3,2,1};
int k = 0;
while(true) {
std::cout<< limit[k];
k = (k+1)%8;
// some break condition
}
return 0;
}
You can use the absolute value function like this:
int i = range;
int a = range;
while(true) {
// 0 1 2 3 4 3 2 1 0 .......... (Forward and Reverse in the range 0 - 4)
a = abs(i-range);
std::cout<< a;
i = (i+1)%(range*2); // Modulo
}
Basically, you double the range, subtract half the range (so it goes from -range to +range), then take absolute value.
EDIT: fixed code to start from zero instead of range.
I figured out another solution, although it's without any tricks. This is how I would have done your problem, without the absolute function:
range = 7; //can be whatever
reverse = false;
for(i = 0; i < 1000; i++){
if(i%range != 0){
if(!reverse){
std::cout<< i % range;
}else{
std::cout<< range - (i % range);
}
}
else{
if((i/range)%2 == 1){
reverse = true;
std::cout<< range;
}else{
std::cout<< i % range;
reverse = false;
}
}
}
This will provide you with the output 01234567654321012345676543210 ...
Took me a while to do this, and I know it's not really what you wanted, but just thought I'd share.
Cheers.
Edit: Although absolute is easier, sometimes it's better to write your own code, as you can modify the amount of conditions. I doubt my code is more efficient, but you never know!
Edit2: Forgot to change reverse to true.