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

/* Author: Rob Miller and William Knottenbelt
   Program last changed: 30th September 2001    */

/* This program inputs integers into an integer array,
and then sums the first n numbers of the array. */ 

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

const int NO_OF_ELEMENTS = 10;

/* Function to sum the first n numbers of an array. */
int sum_of(int a[], int n);


/* START OF MAIN PROGRAM */
int main()
{
	int list[NO_OF_ELEMENTS];
	int count;
	int no_of_elements_to_sum = 5;
	
	for (count = 0 ; count < NO_OF_ELEMENTS ; count++)
	{
		cout << "Enter value of element " << count << ": ";
		cin >> list[count];
	}
	
	cout << "\nHow many elements do you want to add up? ";
	cin >> no_of_elements_to_sum;
	
	cout << "\n\n";
		
	cout << "The sum of the first " << no_of_elements_to_sum << " element";
	if (no_of_elements_to_sum > 1) 
		cout << "s";
	cout << " is " << sum_of(list, no_of_elements_to_sum) << ".\n";
	
	return 0;
}
/* END OF MAIN PROGRAM */

/* DEFINITION OF FUNCTION sum_of */
int sum_of(int a[], int n)
{
	if (n < 1 || n > NO_OF_ELEMENTS)
	{
		cout << "\nError - can only sum 1 to "; 
		cout << NO_OF_ELEMENTS << " elements\n";
		exit(1);
	}
	else if (n == 1)
		return a[0];
	else
		return (a[n-1] + sum_of(a,n-1));
}
/* END OF FUNCTION DEFINITION */
