create an array that contains the rainfall amounts for 2016

create an array that contains the rainfall amounts for 2016 for Kamloops (make up some data). Write the following functions: a) getAverage returns the average rainfall for 2016 for 2016 b) getMinimum returns the smallest rainfall amount for one month for 2016, AND which c) getMaximum returns the largest rainfall amount for one month month it occurred Write 2 different programs: 1) finds the 3 values described above using a normal for loop, and array subscripts 2) finds the 3 values described above using pointers to work through the array and to return the values (get as much practice as possible using pointers)

Solution

1. Program using for loop

PROGRAM CODE:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

double getAverage(double array[][3])
{
   double sum = 0;
   for(int i=0; i<12; i++)
       for(int j=0; j<3; j++)
           sum += array[i][j];
      
   return sum/36;
}

double getMinimum(double array[][3], int month)
{
   double min = array[0][0];
   for(int i=0; i<3; i++)
   {
       if(min>array[month-1][i])
           min = array[month-1][i];
   }
   return min;
}

string getMaximum(double array[][3])
{
   int month = 0;
   double max = array[0][0];
   for(int i=0; i<12; i++)
   {
       for(int j=0; j<3; j++)
       {
           if(max<array[i][j])
           {
               max = array[i][j];
               month = i+1;
           }
       }
      
   }
   return to_string(max) + \" month: \" + to_string(month);
}

int main() {
double rainfallData[][3] = {{23.1, 34.23, 23.43}, {33.43, 32.54, 32.4}, {22.44, 23.45, 23.10},
                       {12.23, 16.43, 11.12}, {11.11, 11.12, 12.34}, {18.34, 17.99, 19.56},
                       {22.33, 22.54, 23.45}, {27.64, 25.34, 26.31}, {12.32, 13.87, 12.95},
                       {32.23, 33.32, 34.12}, {12.23, 13.23, 13.43}, {21.22, 22.34, 22.13}};
   cout<<\"Average Rainfall \"<<fixed<<setprecision(2)<< getAverage(rainfallData)<<endl;
   cout<<\"Minimum Rainfall \"<<setprecision(2)<< getMinimum(rainfallData, 3)<<endl;
   cout<<\"Maximum Rainfall \"<< getMaximum(rainfallData)<<endl;
   return 0;
}

OUTPUT:

Average Rainfall 21.37
Minimum Rainfall 22.44
Maximum Rainfall 34.230000 month: 1

2. Program using pointers

PROGRAM CODE:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

double getAverage(double array[][3])
{
   double sum = 0;
   int counter = 0;
   double *value = array[0];
   while(counter < 36)
   {
       sum += *(value+counter);
      
       counter++;
   }
      
   return sum/counter;
}

double getMinimum(double array[][3], int month)
{
  
   int startIndex = (month-1)*3;
   int counter = (month-1)*3;
   int endIndex = (month-1)*3+3;
   double *value = array[0];
   double min = *(value+counter);
   while(counter < endIndex)
   {
       if(min>*(value+counter))
           min = *(value+counter);
       counter++;
   }
   return min;
}

string getMaximum(double array[][3])
{
   int counter = 0;
   int month = 0;
   double *value = array[0];
   double max = *(value+counter);
   while(counter < 36)
   {
       if(max<*(value+counter))
       {
           max = *(value+counter);
           month = counter;
       }
       counter++;
   }
   return to_string(max) + \" month: \" + to_string(month);
}

int main() {
   double rainfallData[][3] = {{23.1, 34.23, 23.43}, {33.43, 32.54, 32.4}, {22.44, 23.45, 23.10},
                       {12.23, 16.43, 11.12}, {11.11, 11.12, 12.34}, {18.34, 17.99, 19.56},
                       {22.33, 22.54, 23.45}, {27.64, 25.34, 26.31}, {12.32, 13.87, 12.95},
                       {32.23, 33.32, 34.12}, {12.23, 13.23, 13.43}, {21.22, 22.34, 22.13}};
   cout<<\"Average Rainfall \"<<fixed<<setprecision(2)<< getAverage(rainfallData)<<endl;
   cout<<\"Minimum Rainfall \"<<setprecision(2)<< getMinimum(rainfallData, 3)<<endl;
   cout<<\"Maximum Rainfall \"<< getMaximum(rainfallData)<<endl;
   return 0;
}

OUTPUT:

Average Rainfall 21.37
Minimum Rainfall 22.44
Maximum Rainfall 34.230000 month: 1

 create an array that contains the rainfall amounts for 2016 for Kamloops (make up some data). Write the following functions: a) getAverage returns the average
 create an array that contains the rainfall amounts for 2016 for Kamloops (make up some data). Write the following functions: a) getAverage returns the average
 create an array that contains the rainfall amounts for 2016 for Kamloops (make up some data). Write the following functions: a) getAverage returns the average

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site