Program To Calculate Average Of N Numbers Using Arrays And By Allocating Memory Dynamically In C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.


Background and Explanation of the code


The statement,

p = new int [n] ;

on execution allocates memory for storing n integer values at run time and returns the address of the first element which is stored in pointer p. The dynamically allocated memory can be referred using pointer variable p.So *(p+i) refers to the ith element.The user first enter the array elements and then calculates the average of numbers.

OUTPUT

Enter how many elements = 4

Enter value : 1 =20

Enter value : 2=25

Enter value : 3 =15

Enter value : 4 = 35

Average of given value = 23.75