Reducing my list of if loops

To put it bluntly I need help. The large number of variables is the only way that I can get the function to work. I don't know how to reduce them to the point where it will be decent size and work the same way. I have tried everything I know and it doesn't work except for all the if loops.




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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

const int MONTHS = 12;//Constant for number of months.
void getMonth(string months[], int& month);// Asks user for current month.
void getAvg(double avgRain[],string months[], int& i);
void getAct(double actRain[],string months[], int& j, int& month);
void printTable(double avgRain[], double actRain[],string months[], double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26);
const int ZERO = 0;
const int WIDTH1 = 20;
const int WIDTH2 = 20;
const int WIDTH3 = 8;

int main()
{

     double avgRain[MONTHS];
     double actRain[MONTHS];
     double totalAvg,totalAct,sum3,sum4,sum5,sum6,sum7,sum8,sum9,sum10,sum11,sum12,sum13,sum14,sum15,sum16,sum17,sum18,sum19,sum20,sum21,sum22,sum23,sum24,sum25,sum26;
     int i,j, month;
     string months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

     getAvg(avgRain,months,i);
     getMonth(months, month);//Calls getMonth function.
     getAct(actRain,months,j,month);
     totals(avgRain,actRain,months,i,j,totalAvg,totalAct,sum3,sum4,sum5,sum6,sum7,sum8,sum9,sum10,sum11,sum12,sum13,sum14,sum15,sum16,sum17,sum18,sum19,sum20,sum21,sum22,sum23,sum24,sum25,sum26);
     

    return 0;
}
 
     
void getMonth(string months[],int& month)// Asks user for current month.
{

     cout << "\nPlease enter the current month. "
          << "Use 1 for January, 2 for February, "
          << "and so on." << endl;
     cin  >> month;
     
    if (month > ZERO && month <= MONTHS)
	{
		cout << "\nThe current month is " << months[month - 1] << endl;
	}
	else
	{
		cout << "You must enter a number between 1 and 12." << endl;
		getMonth(months, month);
	}
    return;
}

void getAvg(double avgRain[],string months[], int& i)
{
     int month = 1;

     cout << "\nPlease enter the average rainfall for each month" << endl;
     for (int i = ZERO; i < MONTHS; i++)
     {
	cout << months[i]<<" : ";
	cin  >> avgRain[i];
     }
     return;
}
void getAct(double actRain[],string months[], int& j, int& month)
{
     
 
     cout << "\nPlease enter the actual monthly rainfall for the previous 12 months. Begin with the current month: "<< months[month - 1] << endl;
     
     for (j = (12 - 1); j >= 0; --j)

     {
         cout << months[j] << " : ";
         cin  >> actRain[j];
     }
     return;
}

void totals(double avgRain[], double actRain[],string months[], int& i, int& j, double& totalAvg, double& totalAct, double& sum3, double& sum4, double& sum5, double& sum6, double& sum7, double& sum8, double& sum9, double& sum10, double& sum11, double& sum12, double& sum13, double& sum14, double& sum15, double& sum16, double& sum17, double& sum18, double& sum19, double& sum20, double& sum21, double& sum22, double& sum23, double& sum24, double& sum25, double& sum26)
{
     double sum = 0, sum2 = 0;
     

     if ( *avgRain > *actRain )
     {
          sum3 = *avgRain - *actRain;
          sum4 = ZERO;
          if ( sum3 == ZERO && sum4 == ZERO )
          {
               sum3 = ZERO;
               sum4 = ZERO;
          }
     }     
     if ( *(avgRain + 1) > *(actRain + 1) )
     {     
          sum5 = *(avgRain + 1) - *(actRain + 1);
          sum6 = ZERO;
     }
     if ( *(avgRain + 2) > *(actRain + 2) )
     {
          sum7 = *(avgRain + 2) - *(actRain + 2);
          sum8 = ZERO;
     }
     if ( *(avgRain + 3) > *(actRain + 3) )
     {
          sum9 = *(avgRain + 3) - *(actRain + 3);
          sum10 = ZERO;
     }
     if ( *(avgRain + 4) > *(actRain + 4) )
     {
          sum11 = *(avgRain + 4) - *(actRain + 4);
          sum12 = ZERO;
     }
     if ( *(avgRain + 5) > *(actRain + 5) )
     {
          sum13 = *(avgRain + 5) - *(actRain + 5);
          sum14 = ZERO;
     }
     if ( *(avgRain + 6) > *(actRain + 6 ))
     {
          sum15 = *(avgRain + 6) - *(actRain + 6);
          sum16 = ZERO;
     }
     if ( *(avgRain + 7) > *(actRain + 7) )
     {
          sum17 = *(avgRain + 7) - *(actRain + 7);
          sum18 = ZERO;
     }
     if ( *(avgRain + 8) > *(actRain + 8) )
     {
          sum19 = *(avgRain + 8) - *(actRain + 8);
          sum20 = ZERO;
     }
     if ( *(avgRain + 9) > *(actRain + 9) )
     {
          sum21 = *(avgRain + 9) - *(actRain + 9);
          sum22 = ZERO;
     }
     if ( *(avgRain + 10) > *(actRain + 10) )
     {     
          sum23 = *(avgRain + 10) - *(actRain + 10);
          sum24 = ZERO;
     }
     if ( *(avgRain + 11) > *(actRain + 11) )
     {         
          sum25 = *(avgRain + 11) - *(actRain + 11);
          sum26 = ZERO;
     }
     if ( *actRain > *avgRain )
     {
          sum4 = *actRain - *avgRain;
          sum3 = ZERO;
     }
     if ( *(actRain + 1) > *(avgRain + 1) )
     {          
          sum6 = *(actRain + 1) - *(avgRain + 1);
          sum5 = ZERO;
     }
     if ( *(actRain + 2) > *(avgRain + 2) )
     {          
          sum8 = *(actRain + 2) - *(avgRain + 2);
          sum7 = ZERO;
      }
     if ( *(actRain + 3) > *(avgRain + 3) )
     {         
          sum10 = *(actRain + 3) - *(avgRain + 3);
          sum9 = ZERO;
     }
     if ( *(actRain + 4) > *(avgRain + 4) )
     {          
          sum12 = *(actRain + 4) - *(avgRain + 4);
          sum11 = ZERO;
     }
     if ( *(actRain + 5) > *(avgRain + 5) )
     {         
          sum14 = *(actRain + 5) - *(avgRain + 5);
          sum13 = ZERO;
     }
     if ( *(actRain + 6) > *(avgRain + 6) )
     {         
          sum16 = *(actRain + 6) - *(avgRain + 6);
          sum15 = ZERO;
     }
     if ( *(actRain + 7) > *(avgRain + 7) )
     {          
          sum18 = *(actRain + 7) - *(avgRain + 7);
          sum17 = ZERO;
     }
     if ( *(actRain + 8) > *(avgRain + 8) )
     {          
          sum20 = *(actRain + 8) - *(avgRain + 8);
          sum19 = ZERO;
     }
     if ( *(actRain + 9) > *(avgRain + 9) )
     {          
          sum22 = *(actRain + 9) - *(avgRain + 9);
          sum21 = ZERO;
     }
     if ( *(actRain + 10) > *(avgRain + 10) )
     {         
          sum24 = *(actRain + 10) - *(avgRain + 10);
          sum23 = ZERO;
     }
     if ( *(actRain + 11) > *(avgRain + 11) )
     {         
          sum26 = *(actRain + 11) - *(avgRain + 11);
          sum25 = ZERO;
     }
     

         
     sum = sum + *avgRain + *(avgRain + 1) + *(avgRain + 2) + *(avgRain + 3) + *(avgRain + 4) + *(avgRain + 5) + *(avgRain + 6) + *(avgRain + 7) + *(avgRain + 8) + *(avgRain + 9) + *(avgRain + 10) + *(avgRain + 11);
     //cout << sum << " is the test" << endl;
     totalAvg = sum;
     //cout << totalAvg << " is the test" << endl;
     sum2 = sum2 + *actRain + *(actRain + 1) + *(actRain + 2) + *(actRain + 3) + *(actRain + 4) + *(actRain + 5) + *(actRain + 6) + *(actRain + 7) + *(actRain + 8) + *(actRain + 9) + *(actRain + 10) + *(actRain + 11);
     //cout << sum2 << " is the test" << endl;
     totalAct = sum2;
     //cout << totalAct << " is the test" << endl;

     return;     
}





If loops? I never heard that term before ;o)

Your ifs arn't looking very loopy to me, perhaps you need a for loop?
1
2
3
4
5
6
7
8
9
10
11
double total(double avgRain[])
{
    double total = 0.0; // accumulator

    for(int i = 0; i < size, ++i) // loop where i takes on values from 0 to size - 1
    {
        total = total + avgRain[i]; // use [i] rather than *(avgRain + i) because its neater
    }

    return total;
}

Last edited on
You need to learn about arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// very bad

void func(int& v1,int& v2,int& v3,int& v4)
{
  v1 = 1;
  v2 = 2;
  v3 = 3;
  v4 = 4;
}

int main()
{
  int v1, v2, v3, v4;
  func(v1,v2,v3,v4);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
//better

void func(int* v)
{
  for(int i = 0; i < 4; ++i)
    v[i] = i+1;
}

int main()
{
  int v[4];
  func(v);
}



As a general rule of thumb... if you find yourself naming things "sum1, sum2, sum3" etc, you're doing things wrong.

Also if you're copy/pasting blocks of code and only changing variables names, you're doing something wrong.


EDIT:

I also thought this was comical:

const int ZERO = 0;

is 'ZERO' really any more clear than '0'?
Last edited on
Topic archived. No new replies allowed.