Sorting the months along with the rainfall

Can you please tell me how to sort the month along with the amount of rainfall.(from highest to lowest) Thank you very much.


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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 #include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void selectionSort(double array[],int size)
{
    int startScan, minIndex, maxValue;
    for(startScan = 0; startScan < (size - 1); startScan++)
    {
        minIndex = startScan;
        maxValue = array[startScan];
        for(int index = startScan + 1; index < size; index++)
        {
            if (array[index] > maxValue)
            {
                maxValue = array[index];
                minIndex = index;
            }
        }
        array[minIndex] = array[startScan];
        array[startScan] = maxValue;
    }
}
void highestfunction(double highestrainfall[], string monthtemp[])
{
   double highest;
    highest = highestrainfall[0];
    string highestmonth = "January";
    for (int count = 1; count < 12; count++)
    {
        if (highestrainfall[count] > highest)
            {
            highest = highestrainfall[count];
            highestmonth = monthtemp[count];
            }
    }
    cout << highestmonth << " has highest amount of rainfall: " << highest << " ml" << endl;
}
void lowestfunction(double lowestrainfall[], string monthtemp[])
{
    double lowest;
    lowest = lowestrainfall[0];
    string lowestmonth = "January";
    for (int count = 1; count < 12; count++)
    {
        if(lowestrainfall[count] < lowest)
        {
            lowest = lowestrainfall[count];
            lowestmonth = monthtemp[count];
        }
    }
    cout << lowestmonth << " has lowest amount of rainfall: " << lowest << " ml" << endl;
}
int main()
{
    string months[] = { "January",
                        "February",
                        "March",
                        "April",
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December"};
    double rainfall[12];
    double total = 0;
    double average;
    int count;
    for (count = 0; count < 12; count++)
    {
        cout << "Enter the total rainfall (in milliliter) of " << months[count] << endl;
        cin >> rainfall[count];
        while(rainfall[count] < 06)
        {
            cout << "Invalid input!!! Please try again. " << endl;
            cout << "Enter the total rainfall (in milliliter) of " << months[count] << endl;
            cin >> rainfall[count];
        }
    }
    for (count = 0; count <12; count++)
    {
        total += rainfall[count];
    }
    cout << "----------------------------------------" << endl;
    cout << setprecision(2) << showpoint << fixed;
    cout << "The total rainfall for a year is: " << total << " ml" << endl;
    average = (total / 12);
    cout << "The average rainfall monthly rainfall is " << average << " ml" << endl;
    highestfunction(rainfall, months);
    lowestfunction(rainfall, months);
    selectionSort(rainfall, 12);
    cout << "List of the months, sorted in order of rainfall from highest to lowest" << endl;
    for(count = 0; count < 12; count++)
    {
        cout << rainfall[count] <<" ml" << endl;
    }
    return 0;
}
There are many ways to sort, just google.

As for how to do it, just sort the array that holds the data that were input by the user and apply the sorting algorithm to them.

You can do that using std::vectors.
Last edited on
Topic archived. No new replies allowed.