/* Program 7.3.1 from C++ Programming Lecture notes  */

/* Author: Rob Miller and William Knottenbelt
   Program last changed: 28th September 2013    */

/* This program illustrates the use of dynamic arrays. It prompts
   the user for a list of integers, then outputs their average to
   the screen. */ 

#include <iostream>
#include <cstdlib>
using namespace std;

/* Function to compute the average value of the integer 
   elements in an array "list[]" of length "length" */
float average(int list[], int length);

/* MAIN PROGRAM */
int main()
{
	int no_of_integers, *number_ptr;
	
	cout << "Enter number of integers in the list: ";
	cin >> no_of_integers;
	
	number_ptr = new (nothrow) int[no_of_integers];
	if (number_ptr == NULL)
	{
		cout << "Sorry, ran out of memory.\n";
		exit(1);
	}
	
	cout << "type in " << no_of_integers; 
	cout << " integers separated by spaces:\n"; 
	for (int count = 0 ; count < no_of_integers ; count++)
		cin >> number_ptr[count];
	cout << "Average: " << average(number_ptr,no_of_integers) << "\n";
	
	delete [] number_ptr;
	
	return 0;
}
/* END OF MAIN PROGRAM */

/* DEFINITION OF TWO ARGUMENT FUNCTION "average" */
float average(int list[], int length)
{
	float total = 0;
	int count;
	for (count = 0 ; count < length ; count++)
		total += float(list[count]);
	return (total / length);
}
/* END OF FUNCTION DEFINITION */
