/* 
   Author: DMN
   Filename: dynamic_Arrays.cpp
   Description: This program demonstrates how to dynamically allocate an array
                of integers based on the number of integers the user elects to 
                input at runtime.  The program computes the average of the 
                integers entered and dispalys both the numbers entered and their
                               average to the user.
*/ 
 
#include <iostream>
using namespace std;
 
int main() 
{
        int sum = 0, n;
 
        cout << "How many integers do you want to enter? ";
        cin >> n;
        int *intArray;
        intArray = new int[n];    // dynamically allocate an array of n integers 
 
        cout << "Enter your list of " << n << " integers (ie: 19 83 20 09): ";
 
        for (int i = 0; i < n; i++) 
        {
               cin >> intArray[i];
               sum += intArray[i];   // sums holds a running summation of the integers entered
        } 
        
        double avg;
        avg = double(sum)/(n);  // type cast sum to double to avoid integer divison
        
        cout << "You entered: ";
        for (int index = 0; index <n; index++)   // show the user the numbers entered
            cout << intArray[index] << " ";
        cout << endl;
 
        cout << "The average of the " << i << " numbers you entered is: " << avg <<endl;
 
        delete [] intArray;  // returns the dynamically allocated intArray to the free store
        
        return 0;
}

syntax highlighted by Code2HTML, v. 0.9.1