Computer Science, asked by kreezyk14, 4 months ago

In the following program, an array of type integer (arr[]) is declared. The array can store any integer value. The following program is to print the average value of all the even numbers including zero stored in the array (arr[] ).

Note: Integer type array 'arr[]' is already defined and initialized. Use double type variables wherever applicable.

Answers

Answered by shubhamzone9
5

Answer:

NPTEL SET1:Q3  -

int n=arr.length;

 int  sum = 0;

       for (int i = 0; i < n; i++)

       {

          if(arr[i]%2==0)

            sum += arr[i];

       }

double average = sum/n;

System.out.print(average);

Explanation:

Answered by Anonymous
0

Answer:

#include <iostream>

using namespace std;

// Function that return average of an array.

double average(int a[], int n)

{

   // Find sum of array element

   int sum = 0;

   for (int i=0; i<n; i++)

      sum += a[i];

   return (double)sum/n;

}

// Driver code

int main()

{

   int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};

   int n = sizeof(arr)/sizeof(arr[0]);

   cout << average(arr, n) << endl;

   return 0;

}

Output: 6

Explanation:

Similar questions