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
|
#include <iostream>
#include <fstream>
using namespace std;
//Function Prototypes
int getLowestNum(int[], int);
int getHighestNum(int[], int);
int getTotal(int[], int);
int getAverage(int[], int);
int main()
{
const int ARRAY_SIZE = 12;
int numbers[ARRAY_SIZE];
int lowest, highest, total, average, count=0;
//Read the file
ifstream inputFile;
inputFile.open("numbers.txt");
//Loop to read numbers from file into array
for (count = 0; count < ARRAY_SIZE; count++) {
inputFile >> numbers[count];
}
//find lowest value and display
lowest = getLowestNum(numbers, ARRAY_SIZE);
cout << "Lowest value is: " << lowest << endl;
//find highest value and display
highest = getHighestNum(numbers, ARRAY_SIZE);
cout << "Highest value is: " << highest << endl;
//find sum value and display
total = getTotal(numbers, ARRAY_SIZE);
cout << "The total sum is: " << total << endl;
//find average value and display
average = getAverage(numbers, ARRAY_SIZE);
cout << "The average is: " << average << endl;
//close file
inputFile.close();
return 0;
}
//Function Definition for getLowestNum
int getLowestNum(int numbers[], int ARRAY_SIZE)
{
int lowest;
lowest = numbers[12];
for (int count = 1; count < ARRAY_SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
return lowest;
}
//Function Definition for getHighestNum
int getHighestNum(int numbers[], int ARRAY_SIZE)
{
int highest;
highest = numbers[12];
for (int count = 1; count < ARRAY_SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
return highest;
}
//Function Definition for getTotal
int getTotal(int numbers[], int ARRAY_SIZE)
{
int total = 0;
total = numbers[12];
for (int count = 0; count < ARRAY_SIZE; count++)
{
total += numbers[count];
}
return total;
}
//Function Definition of getAverage
int getAverage(int numbers[], int ARRAY_SIZE)
{
int total = 0;
int average = 0;
for (int count = 0; count < ARRAY_SIZE; count++)
{
total += numbers[count];
}
average = total / ARRAY_SIZE;
return average;
}
|