-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion6.cpp
More file actions
30 lines (23 loc) · 832 Bytes
/
question6.cpp
File metadata and controls
30 lines (23 loc) · 832 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
/*
* Write a C++ program that does the following:
* i. Uses a loop for a user to continually input 5 values to populate an array.
* ii. Calculates and displays the average of the values input into the array.
*/
#include <iostream>
int main() {
// Declaration of variables
double myArray[5];
double sum = 0, average;
// Allow the user to input 5 values to populate the array
for (int i = 0; i < std::size(myArray); i ++) {
std::cout << "Enter value " << i + 1 << ": ";
std::cin >> myArray[i];
// Keep track of the sum of values in the array
sum += myArray[i];
}
// Calculate the average of values in the array
average = sum/std::size(myArray);
// Display the sum
std::cout << "The average of the values is: " << average << std::endl;
return 0;
}