1. Engineering
  2. Computer Science
  3. c program you wrote to use the following functions i...

Question: c program you wrote to use the following functions i...

Question details

C program you wrote to use the following functions. I did this problem before what know i have to use functions in that as describes below: please help me.

  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.

  1. Create and call a function to return the month name to main()
  1. Create and call ONE function to print one line of symbols.
    • Call it the first time to print the line of *
    • Call it a second time to print the line of!
    • No if statement is needed or allowed in this function
    • Think very carefully about the parameter list for this function

  1. Call a function to print the scale and the legend as shown below.
  1. Call a function to find and print the total rainfall for each data set, and also the comparison of the two totals. The function will state whether 2018 was wetter, drier, or equal to a normal 6 month rainfall and by how much.
  1. Call a function to determine which month in 2018 had the highest rainfall, and print the month name (using the function described in #2), the rainfall amount and how that amount compares to the normal amount for that month.
  2. #include<stdio.h>

    void Fun(int size, char c)
    //Function prototypes
    void drawGraph(double[], double[],int a);
    double totalNormalRainfall(double[],int a);
    double total2018Rainfall(double[], int a);
    void wetterDrierStatus(double, double);
    void highestRainFall(double[], double[], int a);

    //Main function
    int main()
    {
      
    //Variables for total rainfalls
    double normal, rain2018;

    //Rainfalls stored arrays
    double monthRainFall[]= { 3.1,4.7,4.2,5.0,4.0,6.3 };
    double rainAmt2018[]= { 5.4,4.4,4.1,6.0,5.6,4.5 };
    //printf("Enter the normal rainfall: ");
    //scanf("%lf %lf %lf %lf %lf %lf",&monthRainFall[0],&monthRainFall[1],&monthRainFall[2],&monthRainFall[3],&monthRainFall[4],&monthRainFall[5]);
    //printf("Enter the 2018 rainfall: ");

    //scanf("%lf %lf %lf %lf %lf %lf", &rainAmt2018[0],&rainAmt2018[1],&rainAmt2018[2],&rainAmt2018[3],&rainAmt2018[4],&rainAmt2018[5]);

    //Call draw function for graph display
    drawGraph(monthRainFall, rainAmt2018,6);

    //Find total normal rain falla
    normal = totalNormalRainfall(monthRainFall, 6);

    //Find total 2018 rain fall
    rain2018 = total2018Rainfall(rainAmt2018, 6);

    //Display details
    printf("Total normal rainfall was %.2lf mm. \n",normal);
    printf("Total rainfall for 2018 was %.2lf mm. \n ",rain2018);

    //Find wetter/drier states
    wetterDrierStatus(normal, rain2018);

    //Find highest rainfall in 2018
    highestRainFall(monthRainFall, rainAmt2018, 6);
    printf(" ");
    return 0;
    }

    //Function to display graph
    void drawGraph(double rainFall[], double rainAmt[],int a)
    {
       
    //Create a month array
    char month[6][10] = {"January","February","March","April","May","June"};

    //printf("%c",month[0]);

    //Header
    printf("Rainfall comparison for January to June 2018 \n");

    //Loop to print graph
    for (int i = 0; i < a; i++)
    {
      
       printf("%3s%c ", month[i]);
       printf("%10c ",'|');
       for (double j = 0; j <=rainFall[i]; j+=.2)
       {
           printf("%c", '*');
       }
       printf("\n");
       printf("%15c", '|');
       for (double j = 0; j <= rainAmt[i]; j += .2)
       {
       printf("%c", '!');
       }
       printf(" ");
       printf("\n");
    }
    printf("%10c", '|');
    int j = 0,k=1;
    for (int i = 1; i <= 40; i++)
    {
    if (i!=0&&i%5==0) {
    printf("%d", k);
    k++;
    }
    else {
    printf("%c", '-');
    }
    }

    //Display legend section
    printf("\n");
    printf("LEGEND: (numbers below are for illustration purpose only \n");
    printf("* - normal rainfall for a given month \n");
    printf("! - 2018 rainfall for a given month \n");
    }

    //Method to find total normal rain fall
    double totalNormalRainfall(double rainfall[],int a) {
    double total = 0;
    for (int i = 0; i < a; i++) {
    total += rainfall[i];
    }
    return total;
    }

    //Method to find total 2018 rain fall
    double total2018Rainfall(double rainfall[], int a) {
    double total = 0;
    for (int i = 0; i < a; i++) {
    total += rainfall[i];
    }
    return total;
    }


    //Method to find wetter or drier year
    //If difference negative, then drier
    //Otherwise wetter
    void wetterDrierStatus(double p, double q) {
    double diff = q - p;
    if (diff < 0) {
    printf("2018 was a drier year than normal by %.2lf mm.\n",(-1*diff));
    }
    else {
    printf("2018 was a wetter year than normal by %.2lf mm.\n", diff);
    }
    }

    //Method to find highest rain fall in 2018
    void highestRainFall(double rainfall[], double rain2018[], int a) {
    double highest = rain2018[0],diff;
    int index=0;
    char month[6][10] = {"\n January","February","March","April","May","June"};


    //Loop to get highest rainfall month index and amount
    for (int i = 1; i < a; i++) {
    if (highest < rain2018[i]) {
    highest = rain2018[i];
    index = i;
    }
    }
    //Print details
    printf("The month and rainfall amount, with the highest rainfall was %s and %.2lf mm ", month + index, rain2018[index]);

    //Compare with normal rainfall and display details
    diff = rain2018[index] - rainfall[index];
    if (diff < 0) {
    printf(",which is lower than the normal rainfall by %.2lf mm", (-1 * diff));
    }
    else {
    printf(",which is higher than the normal rainfall by %.2lf mm ",diff);
    }

    }

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution