C Program to Read in Random Number of Integers and Calculates Average of Numbers Using Loop

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

Trouble Clarification

We take to write a program in C such that nosotros are reading an array of Due north elements so we are going to summate the sum and average of those Northward elements and display it to the standard output or screen.

Expected Input and Output

If nosotros are entering five 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 + xx + thirty + forty + l = 150
2. Average of Elements of the array volition be: 150 / 5 = xxx

Problem Solution

Fundamentally, an array is a information structure containing a collection of values or variables. The simplest blazon of array is a linear assortment or 1-dimensional array. An assortment can be divers in C with the post-obit syntax:
int Arr[five] = {10, 20, 30, 40, 50};
/* here 10,20,30,xl,l are the elements at indices 0,1,ii,3,4 respectively */

In this case, array Arr is a drove of 5 integers. Each integer can be identified and accessed past its index.

The indices of the array start with 0. And so, the first element of the array will take alphabetize 0, the next will have index ane and then on. For example, if we have to add twenty to the 2nd element, then the syntax volition exist:
Arr[1] = Arr[i] + xx; (we can also utilise Arr[1] += twenty;)

For the solution, we will beginning construct an array with user-defined length and so, nosotros volition notice its sum and average.

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

Simply, we volition add all the elements of the array and and then carve up it with the number of the elements to find the average.

The sequence of steps for the solution will be as follows:
i. Take north, a variable that stores the number of elements of the array.
two. Create an array of size n.
3. Iterate via for loop to take assortment elements as input, and print them.
4. Iterate via for loop to access each element of array to get the sum of all the elements.
v. The average is calculated by dividing the overall sum to the number of elements in the assortment.
6. Sum and average are printed on the screen.

Program/Source Code

Here is the source code of the C program to calculate the sum & average of an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.                       /*                    
  2.                        * C plan to read N integers into an array A and                    
  3.                        * a) Find the sum of all numbers                    
  4.                        * b) Detect the average of all numbers                    
  5.                        * Display the results with suitable headings                    
  6.                        */                    
  7.                   
  8.                       #include  <stdio.h>                    
  9.                   
  10.                       int                      main(                      )                    
  11.                       {                    
  12.                       int                      i,                      num;                    
  13.                       bladder                      full                      =                      0.0                      ,                      average;                    
  14.                       printf                      (                      "Enter the value of North                        \north"                      )                      ;                    
  15.                       scanf                      (                      "%d"                      ,                      &num)                      ;                    
  16.                       int                      assortment[num]                      ;                    
  17.                   
  18.                       printf                      (                      "Enter %d numbers (-ve, +ve and zero)                        \north"                      ,                      num)                      ;                    
  19.                   
  20.                       for                      (i                      =                      0                      ;                      i                      <                      num;                      i++                      )                    
  21.                       {                    
  22.                       scanf                      (                      "%d"                      ,                      &array[i]                      )                      ;                    
  23.                       }                    
  24.                   
  25.                       printf                      (                      "Input array elements                        \north"                      )                      ;                    
  26.                   
  27.                       for                      (i                      =                      0                      ;                      i                      <                      num;                      i++                      )                    
  28.                       {                    
  29.                       printf                      (                      "%+3d\northward"                      ,                      array[i]                      )                      ;                    
  30.                       }                    
  31.                   
  32.                       /*  Summation starts */                    
  33.                   
  34.                       for                      (i                      =                      0                      ;                      i                      <                      num;                      i++                      )                    
  35.                       {                    
  36.                       total+=array[i]                      ;                      /* this means full=full+array[i]; */                    
  37.                       }                    
  38.                   
  39.                       average                      =                      total                      /                      num;                    
  40.                   
  41.                       printf                      (                      "\n                        Sum of all numbers =  %.2f\n"                      ,                      total)                      ;                    
  42.                   
  43.                       printf                      (                      "\n                        Boilerplate of all input numbers =  %.2f\north"                      ,                      average)                      ;                    
  44.                   
  45.                       }                    

Program Explanation

1. Take a number num as input, which will point the number of elements in the array.
2. Create an array of integer with user-divers size.
3. Iterating through for loops (from 0 to Due north)), take integers as input from the user and print them. These inputs are the elements of the assortment.
4. At present, beginning summation past iterating through all the elements and adding numbers to calculate the sum of the array.
5. To calculate the average, the overall sum is divided by the total number of elements in the array.
6. Print the sum and average values calculated.

Runtime Examination Cases

Hither is the runtime output of the C program where the user is reading an array of 5 integers with values ten,20,xxx,40 and 50 and the program is computing and displaying the sum and average of the elements of the assortment.

Enter the value of Northward five Enter five numbers (-ve, +ve and zero) 10 xx thirty 40 50   Input array elements x 20 30 40 50   Sum of all numbers =  150   Average of all input numbers =  thirty

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here's the list of All-time Books in C Programming, Data-Structures and Algorithms

Manish Bhojasia - Founder & CTO at Sanfoundry

Manish Bhojasia, a applied science veteran with twenty+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on evolution of Linux Kernel, SAN Technologies, Advanced C, Information Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.

fraziersupostan63.blogspot.com

Source: https://www.sanfoundry.com/c-program-sum-average-array/

0 Response to "C Program to Read in Random Number of Integers and Calculates Average of Numbers Using Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel