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.
-
/*
-
* C plan to read N integers into an array A and
-
* a) Find the sum of all numbers
-
* b) Detect the average of all numbers
-
* Display the results with suitable headings
-
*/
-
#include <stdio.h>
-
int main( )
-
{
-
int i, num;
-
bladder full = 0.0 , average;
-
printf ( "Enter the value of North \north" ) ;
-
scanf ( "%d" , &num) ;
-
int assortment[num] ;
-
printf ( "Enter %d numbers (-ve, +ve and zero) \north" , num) ;
-
for (i = 0 ; i < num; i++ )
-
{
-
scanf ( "%d" , &array[i] ) ;
-
}
-
printf ( "Input array elements \north" ) ;
-
for (i = 0 ; i < num; i++ )
-
{
-
printf ( "%+3d\northward" , array[i] ) ;
-
}
-
/* Summation starts */
-
for (i = 0 ; i < num; i++ )
-
{
-
total+=array[i] ; /* this means full=full+array[i]; */
-
}
-
average = total / num;
-
printf ( "\n Sum of all numbers = %.2f\n" , total) ;
-
printf ( "\n Boilerplate of all input numbers = %.2f\north" , average) ;
-
}
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
Side by side Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Competition
- Become a Meridian Ranker in C Programming
- Take C Programming Tests
- Chapterwise Do Tests: Chapter 1, 2, 3, iv, five, vi, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, iii, iv, five, 6, seven, 8, 9, 10
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