long e; // uninitialized
long counter = 42; // initialized
while ( counter <= e )
{
++e;
std::cout << "Do you feel lucky?\n";
}
We have no idea what the value of 'e' is initially, and thus the loop condition on the first time is either true or false. You have been (un)lucky to get false there.
If the condition is true, then incrementing 'e' does keep it true for quite long. As by-product, the counterm grows too and you would dereference the array 'd' out-of-range.
I thought your 'while' condition will be true 42 times. 'e' and 'counterm' aren't both, but I have 'counter' and 'counterm'. 'e' is initially equals 0.
The 'e' is initially 0 only if you explicitly set it 0. Your code did not do that.
Does C++ have function that gives the array elements backwards?
I don't think that you actually want to "get reverse array". You can iterate a range "backwards".
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13
// A
int e = counter;
while ( 0 < e ) {
--e;
std::cout << e << '\n';
}
// B
int e = 1;
while ( e <= counter ) {
std::cout << (counter - e) << '\n';
++e;
}
You did miss the newline. Lets set counter=13 for more impact:
12
11
10
9
8
7
6
5
4
3
2
1
0
One does use index to access an element of the array. What are the indices of an array that have 13 elements? Integer values from 0 to 12. In reverse order that means values from 12 to 0.
The code by JLBorges produces the reverse indices in a third way (and accesses elements from an array too).
@JLBorges:
IMHO, the contents of your example array can cause confusion, because they happen to be the same digits as their array positions.
The program of JLBorges does print its array in both directions in my system. It does not contain 'd' though, so it is best that you show your current code.