Find average, standard deviation of array elements

Question

The following formula computes the standard deviation of a set of numbers:

Find maximum, minimum, average and standard deviation of the array elements question

This formula can be implemented in a program that does a “single-pass” over its input.

Design a program that reads integers (lines), and outputs (1) the number of integers (lines) read, (2) the maximum integer read, (3) the minimum integer read, (4) the average of the integers read, and (5) the standard deviation of the integers read. Document your program in flowchart form.

Code one (1) python and one (1) C++ implementation of your program. Read integers from stdin and output to stdout.

You cannot assume that there will be any input lines, or that there will be enough input for the formula to return a result, and must output only an error message if either of these situations takes place.

You can assume that all input lines contain correctly formatted integers. The output must allow the cut program to process it using only the –fields argument.

Recalling Instruction 2, the Teaching Team approves of you using the math.sqrt() function from the math module std::sqrt() function from the math module in your code. You are also approved to use the std::stoi () function from the string module.

Summary

In this question, we have been providing an array of size n, and we have to find the maximum element in the array, minimum element in the array, average element in the array, and also the standard variation of the array elements. We have to a program in C++ and Python for it. 

Explanation

We will use a loop in order to find the maximum and minimum elements of the array. We will find the average of the array elements too. For finding the average, we will be adding the elements of the array and dividing it by the number of elements in the array. Finally, we will find the standard deviation using the given formula.

Below we have attached the codes for finding the minimum, maximum, average, and standard deviation of array elements in both languages.

Code in C++

#include <iostream>
using namespace std;
#include <cmath>
int main()
{
    /* size of the array */
    int n;
    cin>>n;
    int arr[n];
    /* for getting the set of integers */
    for(int i=0;i<n;i++)
    cin>>arr[i];
    int max=-999999999, min=999999999;
    /* to get the maximum and minimumm integers */
    for(int i=0;i<n;i++){
        if(max<arr[i])
        max=arr[i];
        
        if(min>arr[i])
        min=arr[i];
    }
    cout<<"\nMinimum: "<<min<<endl;
    cout<<"Maximum: "<<max<<endl;
    /* to find the average */
    double avg=0.0;
    for(int i=0;i<n;i++)
    avg+=arr[i];
    avg=avg/n;
    cout<<"Average: "<<avg<<endl;
    double sd;
    /* to find the standard deviation */
    double sum=0.0;
    for(int i=0;i<n;i++)
    sum+=arr[i]*arr[i];
    sd=sqrt((sum-(n*avg*avg))/(n-1));
    cout<<"Standard Deviation: "<<sd<<endl;
}

 

Code in Python

import math
arr=[]
n=int(input())
for i in range(n):
    arr.append(int(input()))

mn=999999999
mx=-999999999
avg=0.0
# to find the minimum, maximum and average 
for i in range(n):
    if arr[i]>mx:
        mx=arr[i]
    if arr[i]<mn:
        mn=arr[i]
    avg=avg+arr[i]
avg=avg/n 
# print the output
print("Minimum:",mn)
print("Maximum:",mx)
print("Average:",avg)
sum1=0.0
# to find the standard deviation 
for i in range(n):
    sum1=sum1+(arr[i]*arr[i])

sd=math.sqrt((sum1-(n*avg*avg))/(n-1))
print("Standard Deviation:",sd)

 

Output in C++

Find maximum, minimum, average and standard deviation of the array elements output c++

Output in Python 

Find maximum, minimum, average and standard deviation of the array elements python output

 

Also read, How to delete a file or folder in python

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *