Rainfall Statistics (arrayManipulation.cpp)
Posted by JanWan
Last Updated: November 27, 2012
  9976

Problem: Rainfall Statistics

Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles.

The program should have the following functions:

initializeArray: Accepts as arguments an array of type double, and the size of the array, of type int. This function should prompt the user to enter the rainfall for the 12 months of the year.

printArray: Accepts as arguments an array of type double, and the size of the array, of type int. This function should print the rainfall amounts for each month.

calculateTotal: Accepts as arguments an array of type double, and the size of the array, of type int.  This function should calculate and display the total rainfall for the year,

calculateAverage: Accepts as arguments an array of type double, and the size of the array, of type int.  This function should return the average monthly rainfall; use the calculateTotal function to compute the sum.

calculateMaximum: Accepts as arguments an array of type double, and the size of the array, of type int. The function should return the month with the highest amount.

calculateLowest: Accepts as arguments an array of type double, and the size of the array, of type int. The function should return the month with the lowest amount.

Input Validation: Do not accept negative numbers for monthly rainfall figures.


J.W. PRODUCTION
   
  
 
 
   

 
 
Persons doing Introduction to Programming, this is your opportunity. Enjoy it...
 
#include <iostream>
#include <cstdlib>
#include<iomanip>
 
using namespace std;
void initializeArray(double (&months)[12], int arraysize);
void printArray(double months[], int arraysize);
double calculateTotal(double months[], int arraysize);
double calculateAverage(double months[], int arraysize);
double calculateMaximum(double months[], int arraysize);
double calculateLowest(double months[], int arraysize);
 
 
int main()
{
    double months[12];
    const int arraysize = 12;
    
    double sum;
    double avg;
    double calmax;
    double calmin;
    
    initializeArray(months, arraysize);
      system("cls");
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
    printArray(months, arraysize);
        sum = calculateTotal( months, arraysize);
        avg = calculateAverage(months, arraysize);
       calmax = calculateMaximum(months, arraysize);
       calmin = calculateLowest(months, arraysize);
cout<<endl<<"Total Rainfall: "<<fixed<<setprecision(2)<<sum<<endl;
   cout<<"Average monthly rainfall: "<<fixed<<setprecision(2)<<avg<<endl;
   cout<<"Highest rainfall: "<<fixed<<setprecision(2)<<calmax<<endl;
   cout<<"Lowest rainfall: "<<fixed<<setprecision(2)<<calmin<<endl;
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
    system("pause");
    
}
 
 
double calculateLowest(double months[], int arraysize)
{
	double minn = months[0];
	for (int count = 0; count < arraysize; count++)
	{
		if (months[count] < minn)
			minn = months[count];
	}
	return minn;
}
 
 
double calculateMaximum(double months[], int arraysize)
{
	double maxx = months[0];
	for (int count = 0; count < arraysize; count++)
	{
		if (months[count] > maxx)
			maxx = months[count];
	}
	return maxx;
}
 
 
double calculateAverage(double months[], int arraysize)
{
   double total = 0;
	double average;
    total = calculateTotal(months, arraysize);
	average = total / arraysize;
	
	return average;
}
 
double calculateTotal(double months[], int arraysize)
{
       double sum = 0;
       for(int i = 0; i < arraysize; i++)
       {
          sum = sum +  months[i];  
       }
       
       
       return sum;
}
 
void printArray(double months[], int arraysize)
{
  string monthNames[] = {"January","Febuary","March","April","May","June",
  "July","August","September","October","November","December"};
	for (int count = 0; count < arraysize; count++)
	{
		cout << monthNames[count] << ": "<<months[count]<<endl;
	}
     
}
 
void initializeArray(double (&months)[12], int arraysize)
{
    string monthNames[] = {"January","Febuary","March","April","May",
    "June","July","August","September","October","November","December"};
   cout << "Please enter the amount of rainfall for each month" << endl;
	for (int count = 0; count < arraysize; count++)
	{
		cout << monthNames[count] << ": ";
		cin >> months[count];
		while (months[count] < 0)
		{
             cout << "Please reenter a positive number for the month of " 
                  << monthNames[count] << endl;
			cin >> months[count];
		}
	}   
}
 
it proper...
 
I made a few changes to the code.

#include <iostream>
#include <cstdlib>
#include<iomanip>
 
using namespace std;
void initializeArray(double months[12], int arraysize);
void printArray(double months[], int arraysize);
double calculateTotal(double months[], int arraysize);
double calculateAverage(double months[], int arraysize);
string calculateMaximum(double months[], int arraysize);
string calculateLowest(double months[], int arraysize);
 
 
int main()
{
    double months[12];
    const int arraysize = 12;
    
    double sum;
    double avg;
    string calmax;
    
    initializeArray(months, arraysize);
      system("cls");
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
    printArray(months, arraysize);
        sum = calculateTotal( months, arraysize);
        avg = calculateAverage(months, arraysize);
       calmax = calculateMaximum(months, arraysize);
cout<<endl<<"Total Rainfall for the Year: "<<fixed<<setprecision(2)<<sum<<endl;
   cout<<"Average monthly rainfall: "<<fixed<<setprecision(2)<<avg<<endl;
   cout<<"Month with Highest rainfall: "<<calmax<<endl;
cout<<"Month with Lowest rainfall: "<<calculateLowest(months, arraysize)<<endl;
cout<<"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
    system("pause");
    
}
 
 
string calculateLowest(double months[], int arraysize)
{
	string minn;
	int checkmin = months[0];
	string monthNames[] = {"January","Febuary","March","April","May"
,"June","July","August","September","October","November","December"};
	
	for (int count = 0; count < arraysize; count++)
	{
		if (months[count] < checkmin)
		{
		    checkmin = months[count];
			minn = monthNames[count];	
		}
	}
	return minn;
}
 
 
string calculateMaximum(double months[], int arraysize)
{
	double maxx = months[0];
	string mx;
	string monthNames[] = {"January","Febuary","March","April","May",
     "June","July","August","September","October","November","December"};
	for (int count = 0; count < arraysize; count++)
	{
		if (months[count] > maxx)
		{
	     	maxx = months[count];
			 mx = monthNames[count];
		}
			
	}
	return mx;
}
 
 
double calculateAverage(double months[], int arraysize)
{
   double total = 0;
	double average;
    total = calculateTotal(months, arraysize);
	average = total / arraysize;
	
	return average;
}
 
double calculateTotal(double months[], int arraysize)
{
       double sum = 0;
       for(int i = 0; i < arraysize; i++)
       {
          sum = sum +  months[i];  
       }
       
       
       return sum;
}
 
void printArray(double months[], int arraysize)
{
  string monthNames[] = {"January","Febuary","March","April","May","June"
,"July","August","September","October","November","December"};
	for (int count = 0; count < arraysize; count++)
	{
		cout << monthNames[count] << ": "<<months[count]<<endl;
	}
     
}
 
void initializeArray(double months[12], int arraysize)
{
    string monthNames[] = {"January","Febuary","March","April","May",
"June","July","August","September","October","November","December"};
cout << "Please enter the amount of rainfall for each month"<<endl;
	for (int count = 0; count < arraysize; count++)
	{
		cout << monthNames[count] << ": ";
		cin >> months[count];
		while (months[count] < 0)
		{
                      cout << "Please reenter a positive number for the 
month of "
 << monthNames[count] << endl; cin >> months[count]; } }    }
 
I'm going 2 reviewed the coding...