Arrays and Functions

Hi, I need help changing this code into 6 functions, instead of how I just compiled everything into Main.
Thanks in advance.

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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
 
#include<iostream>
#include<iomanip>
javascript:PostPreview()
using namespace std;


void Display(string month_name,int rainfall);

int main() {
    
    double average, total_rain, highest_rain, lowest_rain, data, nmax = 0, nmin = 9999, rainfall [16];
    string month, city, name;
    const char* month_name[16];

    int hm;
    int mm;
    int max_counter = 0;
    int min_counter = 0;
    
    cout << "What is your name?" << endl;
    getline(cin, name);
    
    cout << "What is your city?" << endl;
    getline(cin, city);
  
    
    month_name[1] = "January";
    month_name[2] = "February";
    month_name[3] = "March";
    month_name[4] = "April";
    month_name[5] = "May  ";
    month_name[6] = "June";
    month_name[7] = "July";
    month_name[8] = "August";
    month_name[9] = "September";
    month_name[10] = "October";
    month_name[11] = "November";
    month_name[12] = "December";
    

    for (int month = 1; month < 13; month++)
    {
        cout << "Enter amount of rainfall for " << month_name[month] << ": ";
        cin >> data;
        cout << endl;
        total_rain += data;
        rainfall[month] = data;
    }

    for(int loop2 = 1;loop2 < 13;loop2++)
    {
        if(rainfall[loop2] == nmin)
        {
            nmin = rainfall[loop2];
            min_counter++;
            mm = loop2;
        }
        
        if(rainfall[loop2] < nmin)
        {
            nmin = rainfall[loop2];
            mm = loop2;
        }
    }
    
    
    for(int loop = 1;loop < 13;loop++)
    {
        
        if(rainfall[loop] == nmax)
        {
            nmax = rainfall[loop];
            max_counter++;
            hm = loop;
        }
        
        if(rainfall[loop] > nmax)
        {
            nmax = rainfall[loop];
            hm = loop;
        }
    }

    highest_rain = nmax;
    lowest_rain = nmin;
    average = total_rain / 12;
    
    cout <<"The rainfall report for " << city << " prepared by " << name << ":"<<endl;
    cout << endl;


    for(int i = 1;i < 13;i++)
    {
    
    Display (month_name[i],rainfall[i]);

    }
    cout << " -------------1--2--3--4--5--6--7--8--9--10-11-12-13-14-15-16-17-18-19-20"<< endl;

    
    cout <<setw(20)<<left<< "Average Rainfall" << "                    " <<fixed<<setprecision(2)<< average << " Inches" << endl;
    cout << endl;

    cout <<setw(30)<<left<< "Month with Lowest Rainfall" <<"           "<<setw(10)<<left<<month_name[mm]<<"       "<< fixed<<setprecision(2)<<nmin<< endl;

    cout<<setw(30)<<left<< "Month with Highest Rainfall" <<"           "<<setw(10)<<left<<month_name[hm]<<"       "<< fixed<<setprecision(2)<< nmax<< endl;

 
}



void Display(string month_name,int rainfall)
{
    cout << month_name;
    
    if(month_name != "February" && month_name != "September" && month_name != "November" && month_name != "December")
    {
        cout << "\t";
    }
    cout << "\t| ";
    
    for(int i = 0;i < rainfall;i++)
    {
        cout << "*  ";
    }
    cout << endl;
}

Last edited on
What should the 6 functions do? You should clarify how you want the program to be refactored.

You have basically asked us to cut a cake into 6 pieces but have no told us whether the cake should be cut horizontally, vertically, a combination, etc.
Well, I just want these a new code with 6 functions that outputs exactly like the code above.

Im just lost on how to reorganize everything into functions with arrays.
Where is this "6 functions" coming from? Without context it seems like a very strange request - can you post the relevant part of your assignment? Perhaps it describes what each of the six functions should do.
Quote: Must use at least 6 functions (pass by value, passing arrays, pass by reference) using different methods
to exchange data with the calling program.

I have no idea why my professor is asking for 6...its just an assignment. Personally I would do less than 6 but I guess I have to follow the assignment right?
Let say you have:
1
2
3
4
5
6
7
int main() {
  expr1;
  expr2;
  expr1;
  expr2;
  return 0;
}

and then you decide to exploit the repeats:
1
2
3
4
5
6
7
8
9
10
11
12
void foo();

int main() {
  foo();
  foo();
  return 0;
}

void foo() {
  expr1;
  expr2;
}

The tutorials on this site tell more of the neat details.
A good function is one that performs a specific, well-defined task. So think about what your program is doing. Look at what tasks it is doing, and break some of those tasks out into individual functions.

Some examples might be:

- reading data from a file
- performing a certain calculation
- outputting information to the user

I would really appreciate if the replies were actual help, instead of telling me to look at tutorials... I've already looked at them and when I tried it on my own, it kept giving me errors and made the code insanely complex..

Can someone help me figure this out?
Using functions generally makes code simpler, not more complex. Can you post one of your attempts at using functions and post the errors with it too?
Well, I kinda deleted them cuz i was frustrated at it.. it had to restore it to this code which took me awhile as well. Can I get at least like an example for 1 or 2 functions?
You do already have one function in your example:
void Display(string month_name,int rainfall);
And you do call it on line 96.
Topic archived. No new replies allowed.