Return to the BobbyGriggs.com C++ Nook


Chapter 8 - First Array Program Revisited
© 1998 by Bobby Griggs.



// Program name:  Sum of numbers

// Purpose:  To accept input of up to 20 positive integers, 

// store them in an array, recall them from the array, and 

// display the sum.



// Win32 console application



#pragma hdrstop



#include < iostream.h >

#include < conio.h >



const MAX = 21;



void Get_numbers(int numbers[MAX]);

void Compute_sum(int numbers[MAX]);



void main()

{

	int numbers[MAX];



	Get_numbers(numbers);

	Compute_sum(numbers);

	return;

}



void Get_numbers(int numbers[MAX])

{

	int i;



	for (i = 0;i < MAX;i++)

		numbers[i] = 0;

	i = 1;

	do	

	{

		cout << "Enter in number # " << i << " ... ";

		cin >> numbers[i];

		cout << endl;

		if (numbers[i] > 0)

			numbers[0]++;

		i++;

	}

	while ((numbers[i - 1] > 0) && (i < MAX));

	return;

}



void Compute_sum(int numbers[MAX])

{

	int i,sum = 0;



	for (i = 1;i <= numbers[0];i++)

		sum += numbers[i];

	cout << "The sum is " << sum << "!" << endl << endl;

	cout << "Press any key to continue ... " << endl;

	getche();

	return;

}