SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:3 | ISBN:9780132846813 | Edition: 5

Question

The standard deviation of a list of numbers is a measure of how much the num-

bers deviate from the average. If the standard deviation is small, the numbers are

clustered close to the average. If the standard deviation is large, the numbers are

scattered far from the average. The standard deviation, S , of a list of N numbers x i

is defined as follows,

where is the average of the N numbers x 1 , x 2 , .... Define a function that takes a partially filled array of numbers as its argument and returns the standard deviation of the numbers in the partially filled array. Since a partially filled array requires two arguments, the function will actually have two formal parameters: an array parameter and a formal parameter of type int that gives the number of array positions used. The numbers in the array will be of type double . Embed your function in a suitable test program.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

//Header section
#include <iostream>
#include <cmath>
using namespace std;
// Function to calculate the average of the numbers in the array
double calculateAverage(double numbers[], int size) 
{
    double sum = 0.0;

    for (int i = 0; i < size; i++)
    {
        sum += numbers[i];
    }

    return sum / size;
}

// Function to calculate the standard deviation of the numbers in the array
double calculateStandardDeviation(double numbers[], int size) 
{
    double average = calculateAverage(numbers, size);
    double sumOfSquares = 0.0;

    for (int i = 0; i < size; i++) 
    {
        sumOfSquares += pow(numbers[i] - average, 2);
    }

    double standardDeviation = sqrt(sumOfSquares / size);

    return standardDeviation;
}
//Program begins with a main method
int main()
{
    const int MAX_SIZE = 100;
    double numbers[MAX_SIZE];
    int size;

    // Get the number of array positions used from the user
    cout << "Enter the number of array positions used: ";
    cin >> size;

    // Validate the number of array positions used
    if (size <= 0 || size > MAX_SIZE)
    {
        cout << "Invalid number of array positions used. Please enter a value between 1 and " << MAX_SIZE << endl;
        return 0;
    }

    // Get the numbers from the user
    cout << "Enter " << size << " numbers: " << endl;
    for (int i = 0; i < size; i++) {
        cout << "Number " << i + 1 << ": ";
        cin >> numbers[i];
    }

    // Calculate the standard deviation of the numbers
    double standardDeviation = calculateStandardDeviation(numbers, size);

    // Display the standard deviation
    cout << "The standard deviation of the numbers is: " << standardDeviation << endl;

    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Enter the number of array positions used: 5
Enter 5 numbers: 
Number 1: 5.5
Number 2: 3.4
Number 3: 6
Number 4: 8.4
Number 5: 9
The standard deviation of the numbers is: 2.03529

 

0 0

Discussions

Post the discussion to improve the above solution.