+ 1
Filling an array backwards using for loop
I want to fill an array from the highest index and descending order. A sample code as shown: #include <iostream> using namespace std; int main() { int myArr[5]; for(int x=4; x<1; x--) { myArr[x] = x; cout << x << ": " << myArr[x] << endl; } return 0; } Running the code shows no output, can someone explain why it doesn't work?
2 Answers
+ 5
use for loop as
for(int x=4; x>=0; x--)
myArr[x] = x;
0
Ok, my condition in the for loop was wrong, that's why it's not running. Thanks boss