help me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

help me

I need to determine the average age of C++ code that's the task --------------------------------------- Average age This code declares an array called ages, which stores the ages of all employees in the company. The code also includes a for loop that outputs an array. Task Modify the code to calculate and display the average age of employees. You don't need to output the array. here is the code -------------------------------------- #include <iostream> using namespace std; int main() { int ages[] = {19, 24, 36, 45, 56, 52, 21, 27, 24, 34, 29, 60, 40, 42, 45, 47, 22, 30, 34, 20, 18, 26, 51, 43, 47, 39, 22, 34, 56, 52, 21, 27, 24, 37, 19, 24, 36, 45, 44, 49, 23, 25, 19, 40, 29, 60, 40, 42, 45, 47, 61, 30, 19, 43, 47, 39, 41, 46, 29, 24, 21, 25, 28}; int size = 63; int *p = ages; for(int i=0;i<size;i++) { cout << *p << endl; p++; } }

2nd May 2024, 9:48 AM
WLAIN CORNIT
WLAIN CORNIT - avatar
4 Answers
+ 4
Declare a variable and set its value to the sum of all elements(integers) using for, while or do while loop as you wish and then divide the result by the length of array(no. of elements in the array).
2nd May 2024, 12:14 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 7
Sum of all the given age and then devide by no of ages
2nd May 2024, 10:08 AM
A͢J
A͢J - avatar
+ 4
Its your work to make the code, we can only help you by giving it formula to calculate the average Formula:- sum of all num. ÷ total num.
2nd May 2024, 10:41 AM
Alhaaz
Alhaaz - avatar
0
in c++11 int result = std::accumulate(array.begin(), array.end(), 0) / array.size() in c++23 result | std::ranges::transform(0, std::ostream_iterator<int>{ result / array.size() }); I assume you are using the standard array
2nd May 2024, 5:56 PM
White Shadow
White Shadow - avatar