Filling an array backwards using for loop | Sololearn: Learn to code for FREE!

+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?

11/19/2016 1:56:36 PM

Kasem Chaisieng

2 Answers

New Answer

+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