how to print an array backwards

后端 未结 6 1439
太阳男子
太阳男子 2021-01-17 09:20

The user enteres a number which is put in an array and then the array needs to be orinted backwadrds

int main()
{
    int numbers[5];
    int x;

    for (i         


        
6条回答
  •  天涯浪人
    2021-01-17 09:44

    You're very close. Hope this helps.

    #include 
    using namespace std;
    
    int main(int argc, char *argv[]) {
        int numbers[5];
        /* Get size of array */
        int size = sizeof(numbers)/sizeof(int);
        int val;
    
        for(int i = 0; i < size; i++) {
            cout << "Enter a number: ";
            cin >> val;
            numbers[i] = val;
        }
    
        /* Start index at spot 4 and decrement until k hits 0 */
        for(int k = size-1; k >= 0; k--) {
            cout << numbers[k] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

提交回复
热议问题