-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayatruntime.cpp
More file actions
33 lines (28 loc) · 803 Bytes
/
arrayatruntime.cpp
File metadata and controls
33 lines (28 loc) · 803 Bytes
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
#include <iostream>
#include <iomanip>
int main() {
size_t count{};
std::cout << "How many heights will you enter?";
std::cin >> count;
// create array at runtime
int height[count];
// read the heights
size_t entered{};
while (entered < count) {
std::cout << "Enter a height: ";
std::cin >> height[entered];
if (height[entered] > 0) {
++entered;
} else {
std::cout << "A height must be positive - try again.\n";
}
}
//calculate the sum of the heights
unsigned int total {};
for (auto h : height) {
total += h;
}
std::cout << std::fixed << std::setprecision(1);
std::cout << "The average height is " << static_cast<float>(total) / count << std::endl;
return 0;
}