1
+ /* *
2
+ * Joel Brigida
3
+ * COP 3014-003
4
+ * Dr. Sareh Taebi
5
+ * September 18, 2020
6
+ * Assignment 2: Sum and Average calculator
7
+ */
8
+
9
+ #include < iostream>
10
+
11
+ using namespace std ;
12
+
13
+ void Q1 (); // Void function for calculator
14
+ void Q2 (); // Alternate way of doing it with input while loop
15
+
16
+
17
+ int main ()
18
+ {
19
+
20
+ // Q1();
21
+ Q2 ();
22
+
23
+ return 0 ;
24
+
25
+ }
26
+
27
+ void Q1 ()
28
+ {
29
+
30
+ int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10; // user input variables.
31
+ char exit_status; // Variable for repeating calculator do-while loop
32
+
33
+ do {
34
+
35
+ int pos_counter = 0 ; // counters to keep track of the # of positive and non-positive integers.
36
+ int non_pos_counter = 0 ; // We assume Zero is not a Positive Integer as in Discrete Math set theory.
37
+ int sum_positive = 0 ; // sum variables for sum calculations.
38
+ int sum_non_pos = 0 ;
39
+ int sum_all_nums = 0 ;
40
+ double avg_positive = 0 ; // average variables for average calculations.
41
+ double avg_non_pos = 0 ;
42
+ double avg_all_nums = 0 ;
43
+
44
+ cout << " \t => Sum and Average Calculator <=" << endl << endl;
45
+
46
+ cout << " Please enter any 10 integers each separated by a space: " << endl;
47
+ cin >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8 >> num9 >> num10; // User Entry of variables all at once.
48
+
49
+ cout << endl;
50
+
51
+ // 10 if-else conditions, one for each user entered variable,
52
+ // which classifies each user input into the proper category.
53
+ if (num1 > 0 )
54
+ {
55
+ sum_positive = sum_positive + num1;
56
+ pos_counter++;
57
+ }
58
+ else
59
+ {
60
+ sum_non_pos = sum_non_pos + num1;
61
+ non_pos_counter++;
62
+ }
63
+
64
+ if (num2 > 0 )
65
+ {
66
+ sum_positive = sum_positive + num2;
67
+ pos_counter++;
68
+ }
69
+ else
70
+ {
71
+ sum_non_pos = sum_non_pos + num2;
72
+ non_pos_counter++;
73
+ }
74
+
75
+ if (num3 > 0 )
76
+ {
77
+ sum_positive = sum_positive + num3;
78
+ pos_counter++;
79
+ }
80
+ else
81
+ {
82
+ sum_non_pos = sum_non_pos + num3;
83
+ non_pos_counter++;
84
+ }
85
+
86
+ if (num4 > 0 )
87
+ {
88
+ sum_positive = sum_positive + num4;
89
+ pos_counter++;
90
+ }
91
+ else
92
+ {
93
+ sum_non_pos = sum_non_pos + num4;
94
+ non_pos_counter++;
95
+ }
96
+
97
+ if (num5 > 0 )
98
+ {
99
+ sum_positive = sum_positive + num5;
100
+ pos_counter++;
101
+ }
102
+ else
103
+ {
104
+ sum_non_pos = sum_non_pos + num5;
105
+ non_pos_counter++;
106
+ }
107
+
108
+ if (num6 > 0 )
109
+ {
110
+ sum_positive = sum_positive + num6;
111
+ pos_counter++;
112
+ }
113
+ else
114
+ {
115
+ sum_non_pos = sum_non_pos + num6;
116
+ non_pos_counter++;
117
+ }
118
+
119
+ if (num7 > 0 )
120
+ {
121
+ sum_positive = sum_positive + num7;
122
+ pos_counter++;
123
+ }
124
+ else
125
+ {
126
+ sum_non_pos = sum_non_pos + num7;
127
+ non_pos_counter++;
128
+ }
129
+
130
+ if (num8 > 0 )
131
+ {
132
+ sum_positive = sum_positive + num8;
133
+ pos_counter++;
134
+ }
135
+ else
136
+ {
137
+ sum_non_pos = sum_non_pos + num8;
138
+ non_pos_counter++;
139
+ }
140
+
141
+ if (num9 > 0 )
142
+ {
143
+ sum_positive = sum_positive + num9;
144
+ pos_counter++;
145
+ }
146
+ else
147
+ {
148
+ sum_non_pos = sum_non_pos + num9;
149
+ non_pos_counter++;
150
+ }
151
+
152
+ if (num10 > 0 )
153
+ {
154
+ sum_positive = sum_positive + num10;
155
+ pos_counter++;
156
+ }
157
+ else
158
+ {
159
+ sum_non_pos = sum_non_pos + num10;
160
+ non_pos_counter++;
161
+ }
162
+
163
+ sum_all_nums = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10; // sum of all numbers calculation
164
+ avg_all_nums = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10) / 10.0 ; // average of all numbers calculation
165
+
166
+ if (pos_counter > 0 ) // run-time error prevention
167
+ {
168
+ avg_positive = static_cast <double > (sum_positive) / pos_counter; // positive average calculation
169
+ }
170
+ else avg_positive = 0.0 ;
171
+
172
+ if (non_pos_counter > 0 ) // run-time error prevention
173
+ {
174
+ avg_non_pos = static_cast <double > (sum_non_pos) / non_pos_counter; // negative average calculation
175
+ }
176
+ else avg_non_pos = 0.0 ;
177
+
178
+ cout << " The number of positive integers entered is: " << pos_counter << endl;
179
+ cout << " The number of non-positive integers entered is: " << non_pos_counter << endl << endl;
180
+
181
+ cout << " The sum of all positive integers is: " << sum_positive << endl; // Part A
182
+ cout << " The average of all positive integers is: " << avg_positive << endl << endl; // Part B
183
+
184
+ cout << " The sum of all non-positive integers is: " << sum_non_pos << endl; // Part C
185
+ cout << " The average of all non-positive numbers is: " << avg_non_pos << endl << endl; // Part D
186
+
187
+ cout << " The sum of all entered integers is: " << sum_all_nums << endl; // Part E
188
+ cout << " The average of all entered integers is: " << avg_all_nums << endl << endl; // Part F
189
+
190
+ cout << " Would you like to perform another calculation?\n "
191
+ << " Enter Y to continue, or any other key to quit: " ;
192
+ cin >> exit_status;
193
+
194
+ cout << endl;
195
+
196
+ } while (exit_status == ' Y' || exit_status == ' y' ); // Loop condition to perform add'l calculations
197
+
198
+ cout << " See you next time!!" << endl << endl;
199
+
200
+ }
201
+
202
+ void Q2 ()
203
+ {
204
+
205
+ char exit_status; // variable to continue or terminate loop
206
+
207
+ do {
208
+
209
+ int entry_counter = 0 ; // Counts how many inputs user has entered.
210
+ int pos_counter = 0 ; // counters to keep track of the # of positive and non-positive integers.
211
+ int non_pos_counter = 0 ; // We assume Zero is not a Positive Integer as in Discrete Math set theory.
212
+ int sum_positive = 0 ; // sum variables for sum calculations.
213
+ int sum_non_pos = 0 ;
214
+ int sum_all_nums = 0 ;
215
+ double avg_positive = 0 ; // average variables for average calculations.
216
+ double avg_non_pos = 0 ;
217
+ double avg_all_nums = 0 ;
218
+ int input_number;
219
+
220
+ cout << " \t => Sum and Average Calculator <=" << endl << endl;
221
+
222
+ cout << " Please enter 10 integers, each followed by Enter: " << endl;
223
+
224
+ while (entry_counter < 10 )
225
+ {
226
+ cin >> input_number;
227
+ entry_counter++;
228
+
229
+ if (input_number > 0 )
230
+ {
231
+ sum_positive = sum_positive + input_number;
232
+ pos_counter++;
233
+ }
234
+ else
235
+ {
236
+ sum_non_pos = sum_non_pos + input_number;
237
+ non_pos_counter++;
238
+ }
239
+ }
240
+
241
+ sum_all_nums = sum_positive + sum_non_pos; // sum of all numbers calculation
242
+ avg_all_nums = (sum_positive + sum_non_pos) / 10.0 ; // average of all numbers calculation
243
+
244
+ if (pos_counter > 0 ) // run-time error prevention
245
+ {
246
+ avg_positive = static_cast <double > (sum_positive) / pos_counter; // positive average calculation
247
+ }
248
+ else avg_positive = 0.0 ;
249
+
250
+ if (non_pos_counter > 0 ) // run-time error prevention
251
+ {
252
+ avg_non_pos = static_cast <double > (sum_non_pos) / non_pos_counter; // negative average calculation
253
+ }
254
+ else avg_non_pos = 0.0 ;
255
+
256
+ cout << endl << endl;
257
+
258
+ cout << " The number of positive integers entered is: " << pos_counter << endl;
259
+ cout << " The number of non-positive integers entered is: " << non_pos_counter << endl << endl;
260
+
261
+ cout << " The sum of all positive integers is: " << sum_positive << endl; // Part A
262
+ cout << " The average of all positive integers is: " << avg_positive << endl << endl; // Part B
263
+
264
+ cout << " The sum of all non-positive integers is: " << sum_non_pos << endl; // Part C
265
+ cout << " The average of all non-positive numbers is: " << avg_non_pos << endl << endl; // Part D
266
+
267
+ cout << " The sum of all entered integers is: " << sum_all_nums << endl; // Part E
268
+ cout << " The average of all entered integers is: " << avg_all_nums << endl << endl; // Part F
269
+
270
+ cout << " Would you like to perform another calculation?\n "
271
+ << " Enter Y to continue, or any other key to quit: " ;
272
+
273
+ cin >> exit_status;
274
+
275
+ } while (exit_status == ' Y' || exit_status == ' y' );
276
+
277
+ cout << endl << endl;
278
+ cout << " See you next time!!" << endl << endl;
279
+ }
0 commit comments