how to find average code example
Example 1: average calculator
1. make an array nums[number of neumbers you will calculate the average of]
wich will store the numbers and a variable sum=0 (make it double)
2. input that array with a loop
3. in the loop, after you have entered nums[i] add it to sum
4. after loop output sum divided by the number of numbers
code in c++:
using namespace std;
int main()
{
long long nums[ 5 ]; // I used 5 cus I like the number 5.
//Could have been 3 or 10 or whatever.
double sum = 0;
for( long long i = 1 ; i <= 5 ; i++ )
{
cin >> nums[ i ];
sum += nums[ i ];
}
cout << sum / 5 << endl ;
return 0;
}
Example 2: mean
def mean(arr):
return sum(arr)/len(arr)
Example 3: mean
def mean(arr):
arr = list(filter(lambda n:n!=0,arr))
return sum(arr)/len(arr)
Example 4: Calculation of Average
This program is to calculate the average of two numbers.
The numbers are 3 and 5.
The average is: 4.0