------------------------------------------------------------------------------
MC logo
Dynamic Array Allocation
[^] CSc 220 Home
------------------------------------------------------------------------------
[Introduction] [Boolean and Control] [Functions] [Arrays and Pointers] [Dynamic Arrays] [Array Errors] [Command Array] [Standard Containers] [Structs and Classes] [Automatic Pointer] [Multi-File Programs] [Copying Objects] [Templates] [Inheritance] [Plain C I/O] [Type Odds And Ends] [Plain C Strings] [File I/O]
[Dynamic Array Allocation]
dynarr3.cc
#include <iostream>

using namespace std;

// Read in the indicated number of numbers, then print them backwards.
void rne(int arr[], int size)
{
        for(int i = 0; i < size; ++i)
                cin >> arr[i];

        cout << "----------------------------------------------------" << endl;
        for(int i = size-1; i >= 0; --i)
                cout << arr[i] << " ";
        cout << "\n----------------------------------------------------" 
             << endl;

}

main()
{
        // Fixed size.
        int fred[5];
        rne(fred, 5);

        // Allocate dynamically with new.
        cin >> size;
        arr = new int[size];
        rne(arr, size);
        delete [] arr;
}