In: Computer Science
Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays
Please use the "C" program toProduce the EXACT output shown at the end of the document.
1.Generate a graph that compares, on a month-by-month basis, the monthly rainfall for Kamloops for the first half of 2018 (i.e. Jan – June) versus the normal (30 year average) rainfall for Kamloops for the same months.
Input will consist of 6 pairs of numbers representing the normal rainfall for the month and the 2018 rainfall amount for the same month. Use the exact data shown below when you run your program, (Note: the data and output below are for illustration purposes only. Your program must be able to work with any data that has this format)
Rainfall comparison for January to June 2018
January |***********
|!!!!!!!!!!!!!!!!!!!!!!!!!
|
February |***************
|!!!!!!!!!!!!!!!!!!!!!
|
March | etc for the rest of the months
|
June |*****************
|!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|----1----2----3----4----5----6----7----8
LEGEND:
* - normal rainfall for a given month
! - 2018 rainfall for a given month
Total normal rainfall was xx.x mm.
Total rainfall for 2018 was yy.y mm.
2018 was a drier year than normal by z.z mm. (or print wetter or equal if that is appropriate)
The month in 2018 with the highest rainfall was …
IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU
ANSWER:
CODE:
#include<stdio.h>
// one function to print the lines of symbols
void oneFunction(double *rainAmt, char c) {
int size=(*rainAmt)/0.2;
for (int i = 0; i < size; i++) {
printf("%c", c);
}
}
// Function to call scale and legend
void scaleAndLegend() {
//Generates the ruler at the bottom of the
scale.
printf("%-19s|----1----2----3---4----5----6----7----8 ",
"");
//Generates legend at the end of scale.
printf(" Legend ");
printf(" ");
printf(" * - Normal rainfall for a given month. ");
printf(" ");
printf(" ! - 2018 rainfall for a given month. ");
}
double total(double *a, int *size) {
double sum = 0;
for (int i = 0; i < (*size); i++) {
sum += *(a + i);
}
return sum;
}
//printing the comparison of the two totals
void printComparison(double *monthlyRainF, double *rainAmount2018,
int *size) {
//calculating the total normal rainfall
double previousYear = total(monthlyRainF, size);
//Calculating the total 2018 rainfall
double currentYear = total(rainAmount2018, size);
printf(" Total normal rainfall was %.2lfmm. ", previousYear);
printf(" ");
printf(" Total rainfall for 2018 was %.2lfmm. ",
currentYear);
printf(" ");
//Finding the wetter or drier year with if else statement
double wetterOrDrier = previousYear - currentYear;
if (wetterOrDrier < 0) {
printf(" 2018 was the drier year than normal by %.2lfmm. ", (-1 *
wetterOrDrier));
} else {
printf(" 2018 was a wetter year than normal by %.2lfmm. ",
wetterOrDrier);
}
}
//calculation of the highest rainfall
int highestRainfall(double *a, int *size) {
int hRF = 0;
for (int i = 1; i < (*size); i++) {
if (a[i] > a[hRF]) {
hRF = i;
}
}
return hRF;
}
//========================
int main() {
// Variable for total rainfall
double rainAmt2018;
//initialization lists for the normal rainfall,
total rainfall and months.
double monthlyRainF[] = {3.1, 4.7, 4.2, 5.0, 4.0, 6.3};
double rainAmount2018[] = {5.4, 4.4, 4.1, 6.0, 5.6, 4.5};
char month[6][10] = {"January", "February", "March", "April",
"May", "June"};
double previousYear = 0;
double currentYear = 0;
//size
int s = 6;
//Title for the beginning of the graph
printf(" Rainfall comparison for January to June 2018 ");
printf(" ");
//graph generating the rainfall comparison
for (int i = 0; i < s; i++) {
printf("%10s", month[i]);
printf("%10c", '|');
//representing the indentation for graph format and
given rainfall for months
oneFunction(monthlyRainF + i, '*');
printf(" ");
printf("%20c", '|');
oneFunction(rainAmount2018 + i, '!');
printf(" ");
printf("%20c", '|');
printf(" ");
}
//Displaying the legend by calling the functions
made at the beginning
scaleAndLegend();
printf(" ");
printComparison(monthlyRainF, rainAmount2018, &s);
printf(" ");
//Calculating the month with the highest rain
fall
int highestRF;
float highest;
highestRF = highestRainfall(rainAmount2018, &s);
highest = rainAmount2018[highestRF];
//Displaying the normal rainfall and 2018
rainfall
printf(" The month with the highest rainfall was %s at %.1fmm",
month[highestRF], highest);
printf(" ");
printf(" ");
}
RATE THUMBSUP PLEASE