Thursday, August 8, 2013

Rainfall statistics_using arrays

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//double MONTH_YEAR[12];
double rainfall[12];
int index;

void showUserChoice();
void rainFall(double rainfall[],int index);




void rainfallInputError();
void calculateYearlyRainFall(double& sum, double& average, double rainfall[],int index);
void displayYearlyRainFall(double sum, double average);
void selectMaximunValue(double largestRainFall);
void selectMinimunValue(double lowestRainFall);
int main()
{
//declare variables.
double sum=0, average=0, avg =0;
double largestRainFall=0;
double lowestRainFall=0;

//display month selection.

showUserChoice();


//create loop for info input.
for (index = 0; index <12; index++)
{
rainFall(rainfall,index);

while(rainfall[index]<0)
{
rainfallInputError();
rainFall(rainfall,index);

}
}
//calculate total rainfall and average rainfall(display information).
calculateYearlyRainFall(sum, average, rainfall,index);

 displayYearlyRainFall(sum, average);


selectMaximunValue(largestRainFall);
////////////////////////////////////////////////////////////////////////////////////////////////////////
selectMinimunValue(lowestRainFall);

      system("pause");
      return 0;

}

void showUserChoice()
{
//display month selection.
cout<<'\n';
cout<<'\t'<<'\t'<<"***Please enter the rainfall for each month***"<<endl;
cout<<'\n';
cout<<"        **************************************************************"<<endl;
cout<<'\t'<<"*"<<" 1.January"<<setw(15)<<"4.April"<<setw(15)<<"7.July"<<setw(18)<<"10.October"<<"  *"<<endl;
cout<<'\t'<<"*"<<" 2.February"<<setw(12)<<"5.May"<<setw(18)<<"8.Agost"<<setw(18)<<"11.November"<<" *"<<endl;
cout<<'\t'<<"*"<<" 3.March"<<setw(16)<<"6.June"<<setw(21)<<"9.September"<<setw(14)<<"12.December"<<" *"<<endl;
cout<<"        **************************************************************"<<endl;
cout<<'\n';

}

void rainFall(double rainfall[],int index)




{
cout<<"Month Number: "<<(index+1)<<endl;
cin >> rainfall[index];


}

void rainfallInputError()
{
cout<<'\t'<<'\t'<<'\t'<<"***Wrog Data Input***"<<'\n';
cout<<'\t'<<'\t'<<"Your input can't be 0 or a negative number."<<endl;
}

void calculateYearlyRainFall(double& sum, double& average, double rainfall[],int index)
{
sum = 0;
average = 0;
for (index = 0; index < 12; index++)
sum = sum + rainfall[index];
average = sum / 12;

}
void displayYearlyRainFall(double sum, double average)
{
cout<<"The total rainfall for this year is: "<<sum<<" inches "<<endl;
cout<<"The monthly average is : "<<average<<" inches "<<endl;

}
void selectMaximunValue(double largestRainFall)
{
int maxIndex=0;

for (index = 0; index < 12; index++)
if (rainfall[maxIndex] < rainfall[index])
maxIndex = index;
largestRainFall = rainfall[maxIndex];
if(maxIndex+1==1)
cout<<"The highest rainfall was in January with "<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==2)
cout<<"The highest rainfall was in February"<<" with "<<largestRainFall<<" inches "<<endl;
if(maxIndex+1==3)
cout<<"The highest rainfall was in March"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==4)
cout<<"The highest rainfall was in April"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==5)
cout<<"The highest rainfall was in May"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==6)
cout<<"The highest rainfall was in June"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==7)
cout<<"The highest rainfall was in July"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==8)
cout<<"The highest rainfall was in Agost"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==9)
cout<<"The highest rainfall was in September"<<" with "<<largestRainFall<<" inches "<<endl;

if(maxIndex+1==10)
cout<<"The highest rainfall was in October"<<" with "<<largestRainFall<<" inches "<<endl;

if (maxIndex+1==12)
cout<<"The highest rainfall was in December "<<" with "<<largestRainFall<<" inches "<<endl;
}
void selectMinimunValue(double lowestRainFall)
{
int minIndex=0;
for (index = 0; index < 12; index++)
if (rainfall[minIndex] > rainfall[index])
minIndex = index;
lowestRainFall = rainfall[minIndex];

if(minIndex+1==1)
cout<<"The lowest rainfall was in January"<<" with "<<lowestRainFall<<" inches "<<endl;


if(minIndex+1==2)
cout<<"The lowest rainfall was in February"<<" with "<<lowestRainFall<<" inches "<<endl;


if(minIndex+1==3)
cout<<"The lowest rainfall was in March"<<" with "<<lowestRainFall<<" inches "<<endl;


if(minIndex+1==4)
cout<<"The lowest rainfall was in April"<<" with "<<lowestRainFall<<" inches "<<endl;

if(minIndex+1==5)
cout<<"The lowest rainfall was in May"<<" with "<<lowestRainFall<<" inches "<<endl;

if(minIndex+1==6)
cout<<"The lowest rainfall was in June"<<" with "<<lowestRainFall<<" inches "<<endl;





if(minIndex+1==7)
cout<<"The lowest rainfall was in July"<<" with "<<lowestRainFall<<" inches "<<endl;

if(minIndex+1==8)
cout<<"The lowest rainfall was in Agost"<<" with "<<lowestRainFall<<" inches "<<endl;

if(minIndex+1==9)
cout<<"The lowest rainfall was in September"<<" with "<<lowestRainFall<<" inches "<<endl;

 if(minIndex+1==10)
cout<<"The lowest rainfall was in October"<<" with "<<lowestRainFall<<" inches "<<endl;

if (minIndex+1==11)
cout<<"The lowest rainfall was in November"<<" with "<<lowestRainFall<<" inches "<<endl;

if (minIndex+1==12)
cout<<"The lowest rainfall was in December"<<" with "<<lowestRainFall<<" inches "<<endl;

}

No comments:

Post a Comment