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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 12; //array size
//function prototypes
double totalRain(double[], const int );
double largestRain(double[], const int);
double smallestRain(double[], const int);
void showData(double[], const int, double , double);
char largestMonth(double[], const int, double);
char smallestMonth(double[], const int, double);
int main()
{
double rainfall[SIZE], //array with 12 elements
total, //total rain variable
average, //average rain variable
largest, //largest amount of rainfall
smallest, //smallest amount of rainfall
Lrain,
Srain;
char response,
file[20];
int count; //loop counter variable
ifstream inputFile; //inputFile stream object
//numeric output formatting
cout << fixed << showpoint << setprecision(2);
do
{
// user must enter the file name with rainfall data
string file;
cout << "\nenter the filename for rainfall data (or enter -1 to quit): \n";
getline(cin, file);
//open rainfall file
inputFile.open(file);
count = 0;
//read numbers from file into an array
while( count < SIZE && inputFile >> rainfall[count])
count++;
//close file
inputFile.close();
//display rainfall total
total = totalRain( rainfall, SIZE );
cout << " \n total: "<< total << " inches.\n";
//display rainfall averages
average = total / SIZE;
cout << " average: "<< average << " inches.\n";
//display largest rainfall amount
largest = largestRain( rainfall, SIZE );
Lrain = largestMonth( rainfall, SIZE, largest );
cout << " largest: "<< largest << " inches in " << Lrain <<" \n";
//display smallest rainfall amount
smallest = smallestRain( rainfall, SIZE );
Srain = smallestMonth( rainfall, SIZE , smallest );
cout << " smallest: "<< smallest << " inches in " << Srain<<" \n";
//display month and rainfall data
showData( rainfall, SIZE, smallest, largest);
fflush(stdin);
//ask user if they wish to continue
cout << "\nrun again (y/n)?\n";
cin >> response;
fflush(stdin);
}while( response == 'y' || response == 'Y' );
if( response == 'n' || response == 'N' )
cout << "programmer: ynzon \n";
system( "pause" );
return 0;
}
//total rinfall function
double totalRain(double nums[], const int SIZE )
{
double sum = 0;
for( int count = 0; count < SIZE; count++)
sum += nums[count];
return sum;
}
//highest rainfall amount function
double largestRain( double nums[], const int water )
{
double highest;
int count;
highest = nums[0];
for( count = 1; count < SIZE; count++ )
{
if( nums[count] > highest )
highest = nums[count];
}
return highest;
}
//lowest rainfall amount function
double smallestRain( double nums[], const int water )
{
double smallest;
int count;
smallest = nums[0];
for( count = 1; count < SIZE; count++ )
{
if( nums[count] < smallest )
smallest = nums[count];
}
return smallest;
}
// month and rainfall data function
void showData( double nums[], const int SIZE, double S, double L )
{
string months[12] = { " january" , " february" , " march" , " april" ,
" may" , " june" , " july" , " august" , " september"
, " october" , " november" , " december" };
cout << "\n month" << "\t" << " rainfall\n";
cout << "-------"<< "\t" << " --------\n";
for ( int count = 0; count < SIZE; count++ )
{
if (nums[count] == S)
cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << " (smallest)"<< endl;
else if (nums[count] == L)
cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << " (largest)"<< endl;
else
cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << endl;
}
}
// display largest month spelled out
char largestMonth(double nums[], const int SIZE , double LM )
{
double Lmonths;
string months[12] = { " january" , " february" , " march" , " april" ,
" may" , " june" , " july" , " august" , " september"
, " october" , " november" , " december" };
for ( int count = 0; count < SIZE; count++ )
{
if( nums[count] == LM )
LM = months[count];
}
return Lmonths;
}
// display smallest month spelled out
char smallestMonth(double nums[], const int SIZE , double SM )
{
double Smonths;
string months[12] = { " january" , " february" , " march" , " april" ,
" may" , " june" , " july" , " august" , " september"
, " october" , " november" , " december" };
for ( int count = 0; count < SIZE; count++ )
{
if( nums[count]== SM )
SM = months[count];
return Smonths;
}
}
|