Rainfall array

So for my assignment, I have to:


Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should use functions that:
- calculate and display the total rainfall for the year,
- calculates the average monthly rainfall,
- displays the month with the highest amount of rainfall,
- displays the month with the lowest amount of rainfall.


I started to know what I was doing and followed the directions in the Algorithm that my teacher gave as best as I could but the most I could come up with is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

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

// Function prototypes
float rainfall(double[], int);
double getTotal(const double[], int);
double getAverage(const double[], int);
double getLargest(const double[], int);
double getSmallest(const double[], int);



int main()
{
    const int SIZE = 12;
    double rainfall[SIZE], total, SmallestAmount, LargestAmount, average;

    cout << fixed << showpoint << setprecision(1);

    total = getTotal(rainfall, SIZE);

    LargestAmount = getLargest(rainfall, SIZE);

    SmallestAmount = getSmallest(rainfall, SIZE);

    total -= SmallestAmount;

    average = total / (SIZE - 1);

    cout << "The average amount of rainfall is " << average << " ";


    return 0;
}

float rainfall(double scores[], int size)
{
    int index;

    for (index = 0; index <= size - 1; index++)
    {
        cout << "Enter the amount of rainfall: " << (index + 1) << ": ";
        cin >> scores[index];
    }
    return 0;
}

double getTotal(const double numbers[], int size)
{
    double total = 0;
    for (int count = 0; count < size; count++)
        total += numbers[count];

    return total;
}

double getSmallest(const double numbers[], int size)
{
    double smallest;

    smallest = numbers[0];

    for (int count = 1; count < size; count++)
    {
        if (numbers[count] < smallest)
            smallest = numbers[count];
    }

    return smallest;
}

double getLargest(const double numbers[], int size)
{
    double largest;

    largest = numbers[0];

    for (int count = 1; count < size; count++)
    {
        if (numbers[count] > largest)
            largest = numbers[count];
    }

    return largest;
}
Last edited on
Hi,

Good work, your code is reasonably Ok, although you forgot to call the rainfall function in main() .

The rainfall function should return a double.

The normal idiom for a for loop is :

42
43
44
45
46
for (index = 0; index < size ; index++) {

// your code here

}


Make sure you do this for all of your for loops.

Check to see whether the input is negative, if so that is an error. It will affect the TotalRainFall

On line 17 make it const unsigned short, and change all the size parameters in the functions to be const unsigned short as well.

Your return values can be const as well

Always initialise your variables to something, and do it when you declare them. And only declare 1 variable or function per line.

I am pedantic, so I like to declare doubles like this:

double TotalRainFall = 0.0;

not or .0

Try to come up with meaningful names like I just did.

Hope all goes well :+)
Last edited on
Topic archived. No new replies allowed.