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
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
void getInputStreamArray(ifstream& inStream, int array[], int array_size);
void low_temp(int array[], int& low, int size);
void high_temp(int array[], int& high, int size);
void average_temp(int array[], double& average, int size);
int main(){
int array_size = 30;
int array[30];
ifstream inStream;
ofstream outStream;
int low = 0;
int high = 0;
double average;
getInputStreamArray(inStream, array, array_size);
low_temp( array, low, array_size);
high_temp(array, high, array_size);
average_temp( array, average, array_size);
cout << "Monthly Low:" << low;
cout << "\nMonthly High: " << high;
cout << "\nMonthly Average: " << average;
return EXIT_SUCCESS;
}
void getInputStreamArray(ifstream& inStream, int array[], int array_size)
{ // the input file name is taken here
inStream.open("athens_low_temps.txt");
if(inStream.fail()) // checks if you input an invalid file
{
cout << "Opening of your file has failed\n";
cout << "Please make sure the file is in your current directory\n";
getInputStreamArray(inStream, array, array_size);
}
for(int i = 0; i < array_size; i++)// loads the array
{
cout << "\n" << array[i];
inStream >> array[i];
}
inStream.close();
}
void low_temp(int array[], int& low, int size){
low = 100000; // low temp initilized high to make sure it is replaced instantly
int temp;
for(int i = 0; i < (size + 1); ++i)
{
temp = array[i]; // loads the temp of the day into the array
if(temp < low) // checks if the temp is lower
{
low = temp;
}
}
}
void high_temp(int array[], int& high, int size){
high = -100000; //high temp initilized low to make sure it is replaced instantly
int temp;
for(int i = 0; i < (size + 1); ++i)
{
temp = array[i]; // loads the temp of the day into the array
if(temp > high) // checks if the temp is higher
{
high = temp;
}
}
}
void average_temp(int array[], double& average, int size){
//average, total of tempts to be average and temp of each day as set by the array
average = 0;
int total = 0;
int temp = 0;
for(int i = 0; i < (size + 1); ++i)
{
temp = array[i];
total += temp;
}
average = (total/size);
}
|