Back to all tasks

Calculate Average of Positive Numbers

Your task is to implement the calculateAverage function. This function takes an array of integers (arr) and its size (size) as input.

The function should:

  1. Iterate through the array to find all positive numbers.
  2. Calculate the sum of these positive numbers.
  3. Count how many positive numbers were found.
  4. Return the average of the positive numbers.

Important Considerations:

  • If the array contains no positive numbers or if the size is 0 (representing an empty array), the function should return 0.0 to prevent division by zero and correctly handle these edge cases.
  • Ensure floating-point division is used for the average calculation.
calculate_average_of_positive_numbers.cpp
Loading...

Use the "Verify Solution" button to get AI feedback on your c++ code.