/* Filename: array1.cpp
Author: Br. David Carlson
Date: January 6, 2000
Modified: June 26, 2000; August 20, 2001; July 16, 2009; Nov 8, 2009;
Jan 21, 2013
This program prompts the user to enter integers into an array (by
first asking the user for the number of integers to be entered).
The number of integers can be no more than 10. It then computes
and prints the average of the integers.
Tested with:
Microsoft Visual Studio 2005
Microsoft Visual Studio 2008
g++ under Linux
*/
#include
using namespace std;
const int ArrayMax = 10; // Change this to whatever is appropriate.
typedef int ArrayType[ArrayMax];
// Function prototypes:
float FindAverage(ArrayType NumArray, int Count);
void FillArray(ArrayType NumArray, int & Count);
int main(void)
{
int NumItems;
ArrayType MyArray;
float Avg;
FillArray(MyArray, NumItems);
Avg = FindAverage(MyArray, NumItems);
cout << endl << "The average is: " << Avg << endl << endl;
return 0;
}
/* Given: Nothing other than the global constant ArrayMax.
Task: To ask the user for the Count of how many integers to enter,
insisting on a number from 1 to ArrayMax for this Count,
and to get the user to enter these integers.
Return: NumArray The array of integers entered by the user.
Count The number of such integers entered.
*/
void FillArray(ArrayType NumArray, int & Count)
{
int k;
cout << "How many items would you like to enter? ";
cin >> Count;
// Input-checking while loop:
while ((Count < 1) || (Count > ArrayMax))
{
cout << "Use a number from 1 to " << ArrayMax << endl;
cin >> Count;
}
for (k = 0; k < Count; k++)
{
cout << "Enter array item for index " << k << " ";
cin >> NumArray[k];
}
}
/* Given: NumArray An array of Count integers stored contiguously
from index 0 onward.
Count The number of integers in NumArray.
Task: To find the average of the numbers in NumArray.
Return: This average in the function name.
*/
float FindAverage(ArrayType NumArray, int Count)
{
int k;
float Sum;
Sum = 0.0;
for (k = 0; k < Count; k++)
Sum = Sum + NumArray[k];
if (Count > 0)
return Sum / Count;
else
return 0.0;
}