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
|
#include <iostream>
using namespace std;
const int MAX_INTEGERS = 10;
void fill_array(int a[], int size, int& number);
double avgArray(const int a[], int number);
void show_avgArray(const int a[], int number);
int findLargest(const int a[], int number);
void showLargest(const int a[], int number);
int main( )
{
using namespace std;
int integers[MAX_INTEGERS], number;
cout << "This program reads from ten integers what the average is and\n"
<< "what the largest number in the list of integers is\n";
fill_array(integers, MAX_INTEGERS, number);
show_avgArray(integers, number);
showLargest(integers, number);
return 0;
}
void fill_array(int a[], int size, int& number)
{
cout << "Enter up to " << size << " integers.\n"
<< "Mark the end of the list with a 0.\n";
int next, index = 0;
cin >> next;
while ((next >= 1) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}
number = index;
}
double avgArray(const int a[], int number)
{
double total = 0;
for (int index = 0; index < number; index++)
total = total + a[index];
if (number > 0)
{
return (total/number);
}
}
void show_avgArray(const int a[], int number)
{
double average = avgArray(a, number);
cout << "Average of the " << number
<< " scores = " << average << endl;
}
int findLargest(const int a[], int number)
{
int smallest;
int largest = a[smallest];
int i;
for (i = smallest; i <= largest; i++)
{
if (a[i] > largest)
largest = a[i];
}
return largest;
}
void showLargest(const int a[], int number)
{
double largest = findLargest(a, number);
cout << "The largest number in the list is: \n"
<< largest << endl;
}
|