Q:

C++ Program To Find Average of An Array Elements Using Pointers

0

Explanation:-

 In this problem we are passing the value of an array using reference(passing address of the variable) so for this problem we create a function after that we took an array value by user after taking an array value we pass the address of an array to function(we pass the first index address and size of an array) and put the some condition statements in function that help us to calculate the average of an array. After calculating the average of an array we return the average to function and int main function we print the value of an array calculated after average.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include<bits/stdc++.h>
using namespace std;

// function declaration:
double Average(int *arr, int size);

int main ()
{

    int i, n;
    double avg;
  cout<<"Enter The Size Of Array\n";
  cin>>n;
  int average[n];
  cout<<"\nEnter The Array Elements\n"; 
 
 for(i=0; i<n; i++)
  {
  cin>>average[i];
 }
    
 cout << "\n\nAverage Value of An Array Is: " << Average(average , n)<< endl;
  
   return 0;
}

double Average(int *arr, int size)
{
   int i, sum = 0;     
   double avg;         

   for (i = 0; i < size; ++i)
   {
     sum += arr[i];
    }

   avg = double(sum) / size;

   return avg;
}

 

Output:

Enter The Size Of Array

6

Enter The Array Elements

23

56

89

12

45

78

Average Value of An Array Is: 50.5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program For Swapping Number Using Pointer... >>
<< C++ Program To Read Infinite Number And Sort In As...