C Program to Calculate Sum & Average of an Array


                         

This is a C Program to calculate the sum & average of an array.

Problem Description

We have to write a program in C such that we are reading an array of N elements and then we are going to calculate the sum and average of those N elements and display it to the standard output or screen.

Expected Input and Output

If we are entering 5 elements (N = 5), with array element values as 10, 20, 30, 40 and 50 then,
1. Sum of Elements of the array will be: 10 + 20 + 30 + 40 + 50 = 150
2. Average of Elements of the array will be: 150 / 5 = 30 

Problem Solution

Fundamentally, an array is a data structure containing a collection of values or variables. The simplest type of array is a linear array or one-dimensional array. An array can be defined in C with the following syntax:
int Arr[5] = {10, 20, 30, 40, 50};
/* here 10,20,30,40,50 are the elements at indices 0,1,2,3,4 respectively */

In this example, array Arr is a collection of 5 integers. Each integer can be identified and accessed by its index.

The indices of the array start with 0. So, the first element of the array will have index 0, the next will have index 1 and so on. For example, if we have to add 20 to the 2nd element, then the syntax will be:
Arr[1] = Arr[1] + 20; (we can also use Arr[1] += 20;)

advertisement

For the solution, we will first construct an array with user-defined length and then, we will find its sum and average.

The formula for the average of the array will be:
average=Σ(elements of the array)/number of elements in the array

Simply, we will add all the elements of the array and then divide it with the number of the elements to find the averag

Comments

Popular posts from this blog

What is ARTIFICIAL INTELLIGENCE?

What is the cloud?

What is Pinterest How to make money from it - Earn money from phone everyday