Recap - Reversed Array
Part of the Fundamentals section of Coddy's C++ journey — lesson 64 of 74.
Challenge
EasyWrite 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else