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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  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 →

  • DylJFern
    4 months ago+ 0 comments

    //Dynamic allocation of an array

    int *arrPtr{}, userInput{};
    
    cin >> userInput; 
    arrPtr = new int[userInput]; //allocate array at runtime
    
    for(size_t i{1}; i<=userInput; i++) { 
    	cin >> arrPtr[i-1]; //store elements starting at index 0
    }
    
    for(size_t j{1}; j<=userInput; j++) {
    	cout << arrPtr[userInput-j] << " ";
    }
    
    delete [] arrPtr; //free allocated storage (good practice)
    

    //Pre-defined array with a fixed size (based on constraints)

    int userInput{}, arrFixed[1000]{};
        
    cin >> userInput;
        
    for(size_t i{1}; i<=userInput; i++) {
    		cin >> arrFixed[i-1]; 
    }
    
    for(size_t j{1}; j<=userInput; j++) {
    		cout << arrFixed[userInput-j] << " ";
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy