Filling an array backwards using for loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

19th Nov 2016, 1:56 PM
Kasem Chaisieng
Kasem Chaisieng - avatar
2 Answers
+ 5
use for loop as for(int x=4; x>=0; x--) myArr[x] = x;
19th Nov 2016, 2:13 PM
Aditya kumar pandey
Aditya kumar pandey - avatar
0
Ok, my condition in the for loop was wrong, that's why it's not running. Thanks boss
19th Nov 2016, 2:27 PM
Kasem Chaisieng
Kasem Chaisieng - avatar