Sign in
Log inSign up

Struck at a Basic competitive programming question

Harsh Kumar's photo
Harsh Kumar
·Jul 3, 2020·

2 min read

Sir, I have learnt c++ and javascript by myself. I was solving a basic c++ competetive programming question and got stuck . Here is the problem statement: The first line of the input contains N ,where N is the number of integers.The next line contains N integers separated by a space. (i.e we have to make an array by user input and returning reverse array)

Output Format

Print the N integers of the array in the reverse order in a single line separated by a space.

Sample Input:

4

1 4 3 2

Sample Output:

2 3 4 1

and here's my code that is producing some weird results::


#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm> using namespace std;

int main() {

int arr[s];
cin >> s;
for(int i=0; i< s; i++) {
    cin >> arr[s];
}  


for (int i=s; i>=0 ; i--) {
    cout << arr[i] << " ";
}

return 0; }

but this isn't working . In the second for statement I am trying to access the array from the last element and then go to the first element.

with the following sample input ::

Input (stdin):

     5

    1 2 3 4 5

Your Output (stdout)

   5 0 5 4198752 0 4198992 

This logic was working with javascript but is not working with c++.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct