Below is what I have written so far for program that pulls numbers from a text file designated from the user, reads them into an array, uses it to find the average, total(sum), lowest, highest numbers and have the numbers displayed to the user.
// This program reads numbers from a file before adding them up and finding the average.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int getLowest(int nums[], const int SIZE)
{
int index = 0;
int lowest = nums[index];
while (index < SIZE - 2)
{
if (nums[index + 1] < nums[index])
{
lowest = nums[index + 1];
index++;
}
}
return lowest;
}
/*double getTotal(const double numbers[], int size)
{
int index = 0;
int sum;
double total = 0; //Accumulator
for (int count =0; count < size; count++)
{
total += numbers[count];
}
} return sum;*/
int main()
{
const int ARRAY_SIZE = 6; //Array Size
int numbers[ARRAY_SIZE]; //Array with 6 elements
int count = 0; //Loop counter variable
string filename;
ifstream inputFile; //Input File stream object
cout << "Enter a filename you wish to open." << endl;
cin >> filename;
inputFile.open(filename.c_str());
if (inputFile.fail())
{
cout << "Error opening file.\n";
}
else
{
//Process the file.
}
while (count < ARRAY_SIZE && inputFile >> numbers[count])
count++;
inputFile.close();
cout << "The Numbers are: ";
for (count = 0; count < ARRAY_SIZE; count++)
{
cout << numbers[count] << " ";
}
int lowestNum = getLowest(numbers, ARRAY_SIZE);
cout << "The lowest number is " << lowestNum << endl;