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
|
// Rainfall FP.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void showarray(string [], double [], int);
double getTotal(double [], int);
double getAverage(double [], int);
void sortRainfall(string [], double [], int);
int main ()
{
const int size = 12;
string month [size] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October",
"November", "December"};
double rainfall[size]= {0};
cout<<setw(20)<<"Rainfall Data Program"<<endl;
cout<<setw(20)<<"----------------------"<<endl;
cout<<endl;
cout<< "Please enter the rainfall amount in inches for each month." <<endl;
cout<<endl;
for (int index = 0; index < size; index++)
{
cout<< month[index]<< ": ";
cin>> rainfall[index];
while (rainfall[index] < 0 || rainfall[index] > 100) //Validate data (100 in. is based on the maximum
//record of rainfall in 1 month in the U.S)
{
cout<< "The number you have enterd is invalid." <<endl;
cout<< "Please try again" <<endl<<endl;
cout<< month[index]<< ": ";
cin>> rainfall[index];
}
}
cout<<endl;
showarray(month, rainfall, size);
cout<< "The total rainfall for the year is: "<<getTotal(rainfall, size )<<"in"<<endl;
cout<< "The average monthly rainfall is: "<<getAverage(rainfall, size)<< "in"<<endl;
cout<<endl;
cout<< "The monthly rainfall from highest to lowest is: "<<endl;
cout<< "------------------------------------------------"<<endl;
cout<<endl;
sortRainfall(month, rainfall, size);
showarray(month, rainfall, size);
cout<<endl;cout<<endl;
}
//**********************FUNCTIONS*************************************************
void showarray(string montharray[], double rainfallarray[], int size)
{
cout<< "Month"<<setw(20)<< "Rainfall (in)" <<endl;
cout<< "-----"<<setw(10)<< "----------------" <<endl;
for( int index = 0; index < size; index++)
{
cout<< montharray[index] << setw(10)<< rainfallarray[index] <<endl;
}
cout<<endl;
}
double getTotal(double rainfallarray[], int size)
{
double total = 0;
for (int index = 0; index < size; index++)
total+= rainfallarray[index];
return total;
}
double getAverage (double rainfallarray[], int size)
{
double total = 0;
double average = 0;
for (int index = 0; index < size; index++)
total+= rainfallarray[index];
average=total / size;
return average;
}
void sortRainfall(string month[], double rainfall[], int size)
{
int startScan, maxIndex;
string tempMo;
double maxValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxValue = rainfall[startScan];
tempMo = month[startScan];
for(int index = startScan +1; index < size; index++)
{
if (rainfall[index] > maxValue)
{
maxValue = rainfall[index];
tempMo = month[index];
maxIndex = index;
}
}
rainfall[maxIndex] = rainfall[startScan];
month[maxIndex] = month[startScan];
rainfall[startScan] = maxValue;
month[startScan] = tempMo;
}
}
|