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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
double getTotal(const double[], int);
double getAverage(const double[], int);
double getLowest(const double[], int);
double getHighest(const double[], int);
int main()
{
const int ARRAY_SIZE = 12;
double numbers[ARRAY_SIZE];
int count = 0;
double total,
average,
lowestNumber,
highestNumber;
string filename;
ifstream inputFile;
cout << "Enter the file name: ";
cin >> filename;
inputFile.open("numbers.txt");
if (inputFile)
{
while (count < ARRAY_SIZE && inputFile >> numbers[count])
count++;
inputFile.close();
}
else
{
cout << "Can't open the file." << endl;
}
int value = count;
cout << "There are " << value << " numbers in the array.";
cout << "The numbers are: ";
for (int index = 0; index < count; index++)
{
cout << numbers[index] << " ";
}
cout << endl;
total = getTotal(numbers, ARRAY_SIZE);
cout << "The total is " << total << endl;
average = getAverage(numbers, ARRAY_SIZE);
cout << "The average is " << average << endl;
lowestNumber = getLowest(numbers, ARRAY_SIZE);
cout << "The lowest number is " << lowestNumber << endl;
highestNumber = getHighest(numbers, ARRAY_SIZE);
cout << "The highest number is " << highestNumber << endl;
return 0;
}
double getTotal(const double numbers[], int ARRAY_SIZE)
{
double total = 0;
for (int count = 0; count < ARRAY_SIZE; count++)
total += numbers[count];
return total;
}
double getAverage(const double numbers[], int ARRAY_SIZE)
{
double average = 0;
double total;
total = getTotal(numbers, ARRAY_SIZE);
average = total / ARRAY_SIZE;
return average;
}
double getLowest(const double numbers[], int ARRAY_SIZE)
{
double lowest;
lowest = numbers[0];
for (int count = 1; count < ARRAY_SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
return lowest;
}
double getHighest(const double numbers[], int ARRAY_SIZE)
{
double highest;
highest = numbers[0];
for (int count = 1; count < ARRAY_SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
return highest;
}
|