-
Notifications
You must be signed in to change notification settings - Fork 0
/
averages.cpp
231 lines (223 loc) · 5.68 KB
/
averages.cpp
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
//Norbert Acedański
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
unsigned short int choice, i, numberOfElements, keepAveraging=1;
float product, power, weightProduct, currentProduct, result, currentWeight, currentNumber, kFactor;
void arithmeticAverage()
{
cout<<"1 - ARYTHMETIC AVERAGE\n";
cout<<"Arythmetic average form:\n";
cout<<"(a1+a2+a3+...+an)\n";
cout<<"-----------------\n";
cout<<" n\n";
cout<<"It is a sum of the elements divided by their amount.\n";
cout<<"Enter the number of elements: ";
cin>>numberOfElements;
product=0;
for(i=0; i<numberOfElements; i++)
{
cout<<"Enter " <<i+1<< ". element: ";
cin>>currentNumber;
product+=currentNumber;
}
result=product/numberOfElements;
cout<<"Arythmetic average of " <<numberOfElements<< " given elements equals: " <<result<<endl;
}
void geometricAverage()
{
cout<<"2 - GEOMETRIC AVERAGE\n";
cout<<"Geometric average form:\n";
cout<<" ________________\n";
cout<<"n /\n";
cout<<" \\/ a1*a2*a3*...*an\n";
cout<<"It is an n-th square root of the product of positive numbers\n";
cout<<"Enter the number of elements: ";
cin>>numberOfElements;
product=1;
for(i=0; i<numberOfElements; i++)
{
cout<<"Enter " <<i+1<< ". element: ";
cin>>currentNumber;
if(currentNumber<=0)
{
cout<<"Cannot compute geometric average with non-positive numbers\n";
i--;
}
product*=currentNumber;
}
power=1.00/numberOfElements;
result=pow(product, power);
cout<<"Geometric mean of " <<numberOfElements<< " given elements equals: " <<result<<endl;
}
void harmonicAverage()
{
cout<<"3 - HARMONIC AVERAGE\n";
cout<<"Harmonic average form:\n";
cout<<" n\n";
cout<<"---------------\n";
cout<<" 1 1 1 1\n";
cout<<"--+--+--+...+--\n";
cout<<"a1 a2 a3 an\n";
cout<<"It is a number of elements divided by the sum of inverses of the positive numbers\n";
cout<<"Enter the number of elements: ";
cin>>numberOfElements;
product=0;
for(i=0; i<numberOfElements; i++)
{
cout<<"Enter " <<i+1<< ". element: ";
cin>>currentNumber;
if(currentNumber<=0)
{
cout<<"Cannot compute harmonic average with non-positive numbers\n";
i--;
}
product+=1.00/currentNumber;
}
result=numberOfElements/product;
cout<<"Harmonic average of " <<numberOfElements<< " given elements equals: " <<result<<endl;
}
void squareAverage()
{
cout<<"4 - SQUARE AVERAGE\n";
cout<<"Square average form:\n";
cout<<" ____________________\n";
cout<<" / 2 2 2\n";
cout<<" / a1 + a2 + ... + an\n";
cout<<"\\ / -------------------\n";
cout<<" \\/ n\n";
cout<<"It is a square root of the arithmetic average of squared numbers\n";
cout<<"Enter the number of elements: ";
cin>>numberOfElements;
product=0;
for(i=0; i<numberOfElements; i++)
{
cout<<"Enter " <<i+1<< ". element: ";
cin>>currentNumber;
product+=currentNumber*currentNumber;
}
product=product/numberOfElements;
result=sqrt(product);
cout<<"Square average of " <<numberOfElements<< " given elements equals: " <<result<<endl;
}
void powerAverage()
{
cout<<"5 - POWER AVERAGE\n";
cout<<"Power average form:\n";
cout<<" ____________________\n";
cout<<" / k k k\n";
cout<<"k / a1 + a2 + ... + an\n";
cout<<" \\ / -------------------\n";
cout<<" \\/ n\n";
cout<<"It is a k-th root of the quotient of the sums of the k-th powers of successive numbers and the number of elements\n";
cout<<"Enter the number of elements: ";
cin>>numberOfElements;
cout<<"Enter \"k\" factor: ";
cin>>kFactor;
currentProduct=0;
for(i=0; i<numberOfElements; i++)
{
cout<<"Enter " <<i+1<< ". element: ";
cin>>currentNumber;
currentProduct=pow(currentNumber, kFactor);
product+=currentProduct;
}
product=product/numberOfElements;
power=1.00/kFactor;
result=pow(product, power);
cout<<"Power average of " <<numberOfElements<< " given elements equals: " <<result<<endl;
}
void weightedAverage()
{
cout<<"6 - WEIGHTED AVERAGE\n";
cout<<"Weighted average form:\n";
cout<<"a1*w1+a2*w2+a3*w3+...+an*wn\n";
cout<<"---------------------------\n";
cout<<" w1+w2+w3+...+wn\n";
cout<<"It is a quotient of the sum of the products of numbers and their weights and the sum of the weights\n";
cout<<"Enter the number of elements: ";
cin>>numberOfElements;
product=0;
weightProduct=0;
for(i=0; i<numberOfElements; i++)
{
cout<<"Enter " <<i+1<< ". element: ";
cin>>currentNumber;
cout<<"Enter element weight: ";
cin>>currentWeight;
product+=currentNumber*currentWeight;
weightProduct+=currentWeight;
}
result=product/weightProduct;
cout<<"Weighted average of " <<numberOfElements<< " given elements equals " <<result<<endl;
}
void printAveragesMenu()
{
system("cls");
cout<<"MENU\n";
cout<<"Choose average to compute:\n";
cout<<"1 - Arythmetic average\n";
cout<<"2 - Geometric average\n";
cout<<"3 - Harmonic average\n";
cout<<"4 - Square average\n";
cout<<"5 - Power average\n";
cout<<"6 - Weighted average\n";
cout<<"0 - To stop the program\n";
cout<<"Enter your choice: ";
cin>>choice;
system("cls");
}
int main()
{
cout<<setprecision(100000);
cout<<"A program that calculates averages\n";
while(keepAveraging!=0)
{
printAveragesMenu();
switch(choice)
{
case 1:
{
arithmeticAverage();
break;
}
case 2:
{
geometricAverage();
break;
}
case 3:
{
harmonicAverage();
break;
}
case 4:
{
squareAverage();
break;
}
case 5:
{
powerAverage();
break;
}
case 6:
{
weightedAverage();
break;
}
case 0:
{
system("pause");
return 0;
break;
}
}
cout<<"To select menu enter 1, to stop the program, enter 0: ";
cin>>keepAveraging;
}
system("pause");
return 0;
}