1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
#include <cstdlib>
#include <iostream>
/****************************************************/
/* File: name of your file with the source code */
/* */
/* Created by: Aaron Criswell */
/* Date: 15 Aug 2011 */
/* */
/* Program to compute average from array */
/* */
/* Inputs: (keyboard) */
/* Ten integers numbers[10] */
/* */
/* Output: */
/* Average of ten user supplied integers */
/* */
/* */
/****************************************************/
using namespace std;
//prototype function
void average(float avg[], int sizeOfArray);
int main()
{
char cont = 'y';
do
{
//declare and initialize variables
int i = 0;
float numbers[10] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
//print instructions and request for user input
cout <<"This program will calculate the average for ten positive integers." << endl;
cout <<"Please enter your numbers now." << endl;
cout << endl;
//cycles through array for user input
for (i=0; i<10; i++)
{
cout <<"Enter integer #" << i+1 << ": ";
cin >> numbers[i];
cin.ignore();
cout << endl;
}
//test to see array data
// for (i=0; i<10; i++)
//{
// cout << numbers[i] << endl;
//}
//call average function
average(numbers, 10);
cout << endl;
cout <<"Would you like to try again? (Y/N): ";
cin >> cont;
cin.ignore();
}
//gives user option to continue
while (cont == 'Y' || cont == 'y');
return EXIT_SUCCESS;
}
void average(float avg[], int sizeOfArray)
{
//declare and initialize variables
int i = 0;
float x = 0.0;
float sum = 0.0;
float n = 0.0;
//loop to add array input, then divide to get average
for (i=0; i<=sizeOfArray; i++)
{
x = avg[i];
sum = sum + x;
n = sum / sizeOfArray;
}
cout << endl;
cout << "The average for your number set is: " << n << endl;
}
|