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
|
Description: Program reads numbers from a txt file and displays the lowest number in the array, the highest number in the array,
total sum number of all the numbers in the array and avg number of all the numbers in the array.
Limitations or issues:
Credits: Not Applicable
*/
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
// Function prototypes
int getLowest(int [], int);
int getHighest(int [], int);
int getSum(int [], int);
double getAverage(int [], int);
int main()
{
const int ARRAY_SIZE = 12; //array size
int number[ARRAY_SIZE];
int highest, lowest, sum; //values
double avg; //avg value
int count = 0;
ifstream inputFile; //open stored file
inputFile.open("numbers.txt");
if (!inputFile)
{
cout << "Error opening File.\n";
}
else
{
for (count = 0; count < ARRAY_SIZE; count++)
{
inputFile >> number[count];
highest = getHighest(number, ARRAY_SIZE); //get highest number and display
cout << "The highest value is "<< highest << endl;
lowest = getLowest(number, ARRAY_SIZE); //get lowest number and display
cout << "The lowest value is " << lowest << endl;
sum = getSum(number,ARRAY_SIZE); //get sum and display
cout << "The sum of the numbers is " << sum << endl;
avg = getAverage(number,ARRAY_SIZE); //get avg and display
cout << "The average of the numbers is " << avg << endl;
}//end while
inputFile.close();
}
system("pause");
return 0;
}
//Functions
int getLowest(int number[], int ARRAY_SIZE) //get Lowest Function
{
int lowest, count;
lowest = number[0];
for (count = 1; count < ARRAY_SIZE; count++)
{
if (number[count] < lowest)
lowest = number[count];
}
return lowest;
}
int getHighest(int number[], int ARRAY_SIZE) //get Highest Function
{
int highest, count;
highest = number[12];
for (count = 1; count < ARRAY_SIZE; count++)
{
if (number[count] > highest)
highest = number[count];
}
return highest;
}
int getSum(int number[], int ARRAY_SIZE) //get Sum Function
{
int total = 0;
for (int count = 0; count < ARRAY_SIZE; count++)
{
total += number[count];
}
return total;
}
double getAverage(int number[], int ARRAY_SIZE) //get average Function
{
double total = 0; // Initialize Accumulator
double average; // To hold the average
for (int count = 0; count < ARRAY_SIZE; count++)
{
total += number[count];
average = total / ARRAY_SIZE;
}
return average;
}
|