All pastes #474856 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-07 01:42 UTC
rendered paste body
/****************************************************************
Johnathan Raphael Patrick
MAY 5 2007
Project 5 : THIS PROGRAM WILL ASK THE USER,FOR THE AVERAGE RAIN 
FALL FOR EACH MONTH,ENTER THIS INTO AN ARRAY, OUTPUT INTO A NEAT
TABLE,THE MONTH AND IT'S AVERAGE RAINFALL NEXT CALCULATE THE 
AVERAGE FOR ALL 12 MONTHS LASTLY OUTPUT IN A NEAT TABLE, THE 
MONTH, IT'S AVERAGE AND IT'S DIFFERENCE FROM THE AVERAGE 
FOR ALL 12 MONTHS.
*****************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;
// Here are the Function Prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortArray(struct rainfall [], int);
void showArray(double [], int);
int getindex(int array[], int);
const int MONTHS = 12;
const int string_SIZE = 10;

struct rainfall // structure for the rainfall amounts, months, and position of the elements.
{
 double amount;
 double values[MONTHS];
 char rain[MONTHS][string_SIZE];
 };
int main()
{
    char inrain[MONTHS][string_SIZE] =  { "January", "February", "March", "April",
   "May", "June", "July", "August", "September",
   "October", "November", "December" };
 
 rainfall yearly;
  double total;
  double average;
  double highest;
  double lowest;
  
 int i,j;
 for (i = 0; i < MONTHS; i++)
  for (j = 0; j < string_SIZE; j ++)
   yearly.rain[i][j] = inrain[i][j];
 
 

   // dynamically ask the user for input.
   for (int count = 0; count < MONTHS; count++)
   {
      cout << "Enter the amount of rainfall for "<<yearly.rain[count]<< " in inches."<<endl; 
   cin>>yearly.values[count];
   }
   
   cout<<"\n";
   cout<<"\n";
   // Get the total rainfall.
   total = sumArray(yearly.values, MONTHS);
   // Calculate the average.
   average = total / MONTHS;
   // Find the highest rainfall amount.
   highest = getHighest(yearly.values, MONTHS);
   // Find the lowest rainfall amount.
   lowest = getLowest(yearly.values, MONTHS);
   // Find the position of the lowest amount
 
      // Display the total
   cout<<"The total amount of rainfall for the year is: "<<setprecision(3)<<total<<" inches."<<endl;
   cout<<"\n";
   // Display the average
   cout<<"The average amount of rainfall for the year is: "<<setprecision(3)<<average<<" inches."<<endl;
   cout<<"\n";
   // Display the highest
   cout<<"The highest amount of rain was in "<<" with: "<<highest<<" inches."<<endl;
   cout<<"\n";
   // Display the lowest
   cout<<"The lowest amount of rain was in "<<" with: "<<lowest<<" inches."<<endl;
   cout<<"\n";
   // Display the values.
   cout << "The unsorted values are:\n";
   showArray(yearly.values, MONTHS);
   // Sort the values.Display sorted values.
   cout << "The sorted values are:\n";
   showArray(yearly.values, MONTHS);
   system("pause");
   return 0;
}

double sumArray(double array[], int size)
{
   double total = 0; 
   for (int count = 0; count < size; count++)
      total += array[count];
   return total;
   
}
double getHighest(double array[], int size)
{
   double highest;
   highest = array[0];
   for (int count = 1; count < size; count++)
   {
      if (array[count] > highest)
         highest = array[count];
   }
   return highest;
   
}
double getLowest(double array[], int size)
{
   double lowest;
   lowest = array[0];
   for (int count = 1; count < size; count++)
   {
      if (array[count] < lowest)
         lowest = array[count];
   }
   return lowest;  
}
 
/***********************************************************
Definition of function sortArray                        
This function performs an ascending order bubble sort on
array. size is the number of elements in the array.     
***********************************************************/
void sortArray(struct rainfall array[MONTHS], int size)
{
   bool swap;
   struct rainfall temp;
   do
   {
      swap = false;
      for (int count = 0; count < size; count++)
      {
         if (array[count].amount < array[count + 1].amount)
         {
            temp = array[count];
            array[count] = array[count + 1];
            array[count + 1] = temp;
            swap = true;
         }
      } 
   } while (swap);
  
}

/*************************************************************
Definition of function showArray.                          
This function displays the contents of array. size is the 
number of elements.                                       
*************************************************************/
void showArray(double array[], int size)
{
   for (int count = 0; count < size; count++)
      cout << array[count] << " ";
   cout << endl;
}