Home  >  Article  >  Backend Development  >  In C programming, arithmetic mean

In C programming, arithmetic mean

王林
王林forward
2023-09-17 11:41:061355browse

In C programming, arithmetic mean

The arithmetic mean is the sum of a set of numbers divided by the number of numbers in the set.

Basic properties of the arithmetic mean

  • n The average of the numbers x1, x2,. . .,xn is x. If each observation is increased by p, then the mean of the new observations is (x p).

  • #n Numbers x1, x2, . . .,xn is x. If each observation is reduced by p, then the mean of the new observations is (x - p).

  • n Numbers x1, x2, . . .,xn is x. If each observation is multiplied by a nonzero number p, the mean of the new observations is px.

  • #n average of em> numbers x1, x2, . .,xn is x. If each observation is divided by a nonzero number p, the mean of the new observations is (x/p).

Arithmetic mean formula

Type 1: Direct average

Given array and number of elements

Input- 1,2,3,4,5,6,7,8,9

Output- 5

Explanation- To calculate the arithmetic mean of all numbers, first add all the numbers, then create a variable responsible for the arithmetic mean and put the addition/size into the variable like armean .

Example

#include<iostream>
using namespace std;
int main(){
   int n, i, sum=0;
   int arr[]={1,2,3,4,5,6,7,8,9};
   n=9;
   for(i=0; i<n; i++) {
      sum=sum+arr[i];
   }
   int armean=sum/n;
   cout<<"Arithmetic Mean = "<<armean;
}

Type 2: Given range and number of elements present.

Given three integers X, Y and N. Find the arithmetic mean of N between logical X and Y.

N terms in an arithmetic series (the number of terms between #- Explanation

Suppose X1

, X

2, X3

, X

4

……X

n is the arithmetic mean between N two given numbers X and Y.

Then X, X

1, X2, X3, Y will be in an arithmetic series. Now Y = the (N 2)th term of the arithmetic series. Find the (N 2)th term of the arithmetic series where d is the tolerance

X= first and
Y= last terms.

Therefore the tolerance d is given by.

22 24 26 28 30 32 34
We have the value of A and the value of the tolerance (d), now we can find all N arithmetic means between X and Y. Example
Y = X + (N + 2 - 1)d
Y - X = (N + 1)d
Output
d = (Y - X) / (N + 1)

The above is the detailed content of In C programming, arithmetic mean. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete