Menu
Coddy logo textTech

Recap - Reversed Array

Part of the Fundamentals section of Coddy's C++ journey — lesson 64 of 74.

challenge icon

Challenge

Easy

Write a program which gets an array of double numbers, and the size of the array as argument and prints a reversed array.

For example, for [1, 2, 3], the expected output is [3, 2, 1].

To iterate backwards you need to start with a higher number than 0 and decrement i:

for (int i = n - 1; i>=0; i--) {...}

Try it yourself

#include <iostream>
#include <vector>

int main() {
    int n;

    std::cin >> n;
    std::cin.ignore();
    double arr[n];

    for (int i = 0; i < n; i++) {
        double val;
        std::cin >> val;
        arr[i] = val;
    }

    double reverseArr[n];
    // Write your code below


    for (int i = 0; i<n; i++) {
        std::cout << reverseArr[i] << std::endl;
    }
    return 0;
}

All lessons in Fundamentals