We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Great solution by 'jorgechiquinv', adding some comments for posterity if you're rusty with C++ like me :-)
#include<iostream>usingnamespacestd;// Every C++ program starts with the main methodintmain(){// Declare a variable, N for the number of // values entered in from stdinintN;// Declare a variable i for incrementing inti=0;// Read in the initial value from cin into N, // for the number of elements in the arraycin>>N;// Declare a pointer of type int, called A, // and allocated new memory for it of size Nint*A=newint[N];// Setup a while loop for as long as cin // is reading in variables, all into the array, Awhile(std::cin>>A[i++]);// Setup another while loop to output (cout) every // value in A in reverse, using --N to decrement the // the value of N so long as N is true (not equal to zero)while(std::cout<<A[--N]<<' '&&N);// Un-allocate the memory given to Adelete[]A;// Return zero to indicate successreturn0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Arrays Introduction
You are viewing a single comment's thread. Return to all comments →
Great solution by 'jorgechiquinv', adding some comments for posterity if you're rusty with C++ like me :-)