PLEASE HELP WITH PART C (GETMAXIMUM AND WHICH MONTH IT OCCURED) AND N0 2(POINTER
ID: 3782340 • Letter: P
Question
PLEASE HELP WITH PART C (GETMAXIMUM AND WHICH MONTH IT OCCURED) AND N0 2(POINTERS).....It is a C++ Question,please i am familiar with Visual Studio 2015,it will be nice if you can also add comments to the program....THANK YOU.
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)Explanation / Answer
1.
#include <iostream>
using namespace std;
double getAverage(double[]);
double getMaximum(double[]);
double getMinimum(double[]);
int main()
{
double rainfall[12]= {3.5,7.9,5.3,4.8,6.7,10.45,28.78,30.4,17.2,12,5.8,5.5};
cout<<"Average rainfall in 12 months of 2016 for Kamloops : "<<getAverage(rainfall) <<" mm/h";
cout<<" Mimimum rainfall in 12 months of 2016 for Kamloops : "<<getMinimum(rainfall)<<" mm/h";
cout<<" Maximum rainfall in 12 months of 2016 for Kamloops : "<<getMaximum(rainfall)<<" mm/h";
return 0;
}
double getAverage(double rainfall[12])
{
double avg =0;
for(int i=0;i<12;i++)
{
avg = avg +rainfall[i]; // compute sum
}
return avg/12; // sum/12 = average
}
double getMinimum(double rainfall[12])
{
double max =0;
for(int i=0;i<12;i++)
{
if(rainfall[i] >max) //compare max with all 12 values if > assign that value to max
max = rainfall[i];
}
return max;
}
double getMaximum(double rainfall[12])
{
double min =99;
for(int i=0;i<12;i++)
{
if(rainfall[i] <min) //compare min with all 12 values if < assign that value to min
min = rainfall[i];
}
return min;
}
output:
2.
#include <iostream>
using namespace std;
double getAverage(double*); //function prototypes
double getMaximum(double*);
double getMinimum(double*);
int main()
{
double rainfall[12]= {3.5,7.9,5.3,4.8,6.7,10.45,28.78,30.4,17.2,12,5.8,5.5};
cout<<"Average rainfall in 12 months of 2016 for Kamloops : "<<getAverage(rainfall) <<" mm/h";
cout<<" Mimimum rainfall in 12 months of 2016 for Kamloops : "<<getMinimum(rainfall)<<" mm/h";
cout<<" Maximum rainfall in 12 months of 2016 for Kamloops : "<<getMaximum(rainfall)<<" mm/h";
return 0;
}
double getAverage(double *rainfall) //pointer to base address of the array
{
double avg =0;
for(int i=0;i<12;i++)
{
avg = avg + *(rainfall+i); // value at pointer index i is added to avg
}
return avg/12; // sum/12 = average
}
double getMinimum(double *rainfall)
{
double max =0;
for(int i=0;i<12;i++)
{
if(*(rainfall+i) >max) //compare max with all 12 values if > assign that value to max
max = *(rainfall+i);
}
return max;
}
double getMaximum(double *rainfall)
{
double min =99;
for(int i=0;i<12;i++)
{
if(*(rainfall+i) <min) //compare min with all 12 values if < assign that value to min
min = *(rainfall+i);
}
return min;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.