-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAverage.cpp
More file actions
31 lines (24 loc) · 789 Bytes
/
Copy pathFindAverage.cpp
File metadata and controls
31 lines (24 loc) · 789 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
// Find the Average of Array Elements-In this problem, you are tasked with writing a program that
//calculates the average of an array of integers.
// The program should return the average as a floating-point number that reflects the arithmetic mean of all elements in the array.
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
double findAverage(const vector<int>& arr) {
int totalSum = 0;
int n = arr.size();
for (int i = 0; i < n; i++) {
totalSum += arr[i];
}
return (double)totalSum / n;
}
};
int main() {
Solution sol;
vector<int> numbers = {10, 20, 30, 40, 50};
double average = sol.findAverage(numbers);
cout << "Average: " << average << endl;
return 0;
}