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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  1. Practice
  2. C++
  3. Introduction
  4. Arrays Introduction
  5. Discussions

Arrays Introduction

  • Problem
  • Submissions
  • Leaderboard
  • Discussions

    You are viewing a single comment's thread. Return to all comments →

  • mpatriak 1 year ago+ 13 comments

    My C++ Solution

        int numElements;
        cin >> numElements;
        int arr[numElements];
        for (int i = 0; i < numElements; i++)
            cin >> arr[i];
        for (int i = numElements - 1; i >= 0; i--)
            cout << arr[i] << " ";
        return 0;
    
    21|
    Permalink
    • alan_kan 1 year ago+ 1 comment

      Hey I'm just a bit confused. Using the example input, 4 and 1 4 3 2, when you say int numElements, numElements = 4 right? And doing int arr[numElemnts] that creates an array that includes 0 through 3?

      0|
      ParentPermalink
      • mpatriak 1 year ago+ 0 comments

        Right, 4 elements in the array, so that can be used when initializing the array. It will have 4 indices, which are then populated by the second line of input.

        1|
        ParentPermalink
    • devanshudg909 1 year ago+ 0 comments

      in input section when i run in c++ it isnt leaving space

      0|
      ParentPermalink
    • iam_nevets 1 year ago+ 0 comments

      I get the concept but for some reason I don't understand it very well (this question I mean). Can you please put comments in your code to explain properly what's happening here? I would really appreciate it... thanks.

      0|
      ParentPermalink
    • bence_meszaros 1 year ago+ 0 comments
      [deleted]
      0|
      ParentPermalink
    • hmt905 1 year ago+ 0 comments

      if i start loop i=o then compiler display error.when i=1 then ok but again show compiler error because quetion demand i/o using stdin and stdout so how i face difficulty ,please give me solution

      0|
      ParentPermalink
    • nathanlct 12 months ago+ 1 comment

      Hey I am using visual studio and apparently int arr[numElements] wouldn't work (error of "expression must have a constant value")

      0|
      ParentPermalink
      • naveen010gupta 8 months ago+ 0 comments

        because array needs an numeric value whenever you write an array and want to scan some values through condition , assign array to be '0' and size can be managed under loop..

        0|
        ParentPermalink
    • denis_dzinic 10 months ago+ 1 comment

      In first for loop if you replace cin >> arr[i] with following: int x; cin >> x; arr[i] = x;

      That will work otherwise it will fail if you leave your code as it is and try following values it will fail:

      8 6676 3216 4063 8373 423 586 8850 6762

      0|
      ParentPermalink
      • 179tanmay 6 months ago+ 0 comments

        why is it not taking directly input in the array?

        0|
        ParentPermalink
    • naveen010gupta 8 months ago+ 0 comments

      mine is also same but its showing some test case failed and compiler error occure as segmentation error.........

      0|
      ParentPermalink
    • kaushake 7 months ago+ 0 comments

      I did the same thing, but some of the testcases get abort called! Is this because some of the numbers are too big?

      0|
      ParentPermalink
    • dc7024781447 7 months ago+ 0 comments

      i too figured out th same logic but i can not see the output on screen ???

      0|
      ParentPermalink
    • upasanadeka 5 months ago+ 0 comments

      thanks. it worked. but I still am not able to find why my program showed "Segmentation fault" when I wrote the same exact code.

      0|
      ParentPermalink
    • fatimazafarse 5 months ago+ 0 comments

      for (int i = numElements - 1; i >= 0; i--) cout << arr[i] << " "; can you plesae explain this loop's conditions

      0|
      ParentPermalink
    • dharani18199 2 months ago+ 0 comments

      but when i do like this ..im getting runtime errors :(

      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature