Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

where Xavg is the average of the N numbers of x1, x2, ... . Definea function tha

ID: 3568237 • Letter: W

Question

where Xavg is the average of the N numbers of x1, x2, ... . Definea function that takes a partially filled array of numbers as itsarguments and returns the standard deviation of the numbers in thepartially filled array. Since a partially filled array requires twoarguments, the function will actually have two formal parameters:an array parameter and a formal parameter of type int thatgives the number of array positions used. The numbers in the arraywill be of type double. Embed your function in a suitabletest program. Use pointer variables, dynamically allocate the array in which the data is stored.. Also use pointer arithmetic and dereferencing to access each element in the array as you calculate your final results.

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;

double standard_deviation(double* arr, int size)
{
   double sum = 0;
   double* p = arr;
   int i;
   double avg;

   // calculate average
   for (i = 0; i < size; i++, p++)
       sum += *p;
   avg = sum / size;
   // calculate deviation
   sum = 0;
   p = arr;
   for (i = 0; i < size; i++, p++)
       sum += (*p - avg) * (*p - avg);
   return sqrt(sum / size);
}

int main()
{
   int size = 0;
   cout << "Enter number of data: ";
   cin >> size;
   double* data = new double[size];
   cout << "Enter " << size << " values: ";
   for (int i = 0; i < size; i++)
       cin >> data[i];
   cout << "Standard deviation is: " <<
       standard_deviation(data, size) << endl;
   delete[] data;

   system("pause");
   return 0;
}