# Need help with standard deviation C++ program

• Comp Sci
• cromwest
In summary, the conversation discusses a C++ program that calculates the average and standard deviation of 10 floating-point values input by the user. The program also drops any values that are more than four standard deviations away from the average and recalculates the average and standard deviation. The program currently has issues with the second part of the problem and the code is provided for reference. The expert provides guidance on how to fix the issue by correctly calculating the new average after dropping values.
cromwest

## Homework Statement

Write a C++ program that accepts 10 floating-point values from a user and determines and displays the average and the standard deviation. All values more than four standard deviations away from the average should be dropped and then the new average and standard deviation should computed and displayed

## The Attempt at a Solution

I got the average and the standard deviation to work just fine but the second part of the problem I am completely stuck on. Some one suggested to make another array to store the values in but I am definitely not doing it right. Any guidance on where to go from here would be greatly appreciated.

This is my program so far:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

float stdDev(int arr[], int size, float avg);
float calcAverage(int arr[], int size);
float devDrop(int arr[], int adj[], int size, float avg, float dev);

int main()
{
const int size = 10;
int number[size];
float avg, dev, dropAvg;

for (int i=0; i<size; i++)
{
cout << "Enter Number #" << i+1 << ":";
cin >> number;
}

avg = calcAverage(number, size);
cout << avg;

cout << endl;

dev = stdDev(number, size, avg);
cout << dev;

cout << endl;

dropAvg = devDrop(number, adj, size, avg, dev);
cout << dropAvg;

return 0;
}

float calcAverage(int arr[], int size)
{
float avg;
int total = 0;
for (int i=0; i<size; i++)
total += arr;
avg = float(total) / size;
return avg;
}

float stdDev(int arr[], int size, float avg)
{
float dev;
float total = 0;
for (int i=0; i<size; i++)

total += pow(avg - arr, 2);
dev = sqrt(float(total) / size);

return dev;
}

float devDrop(int arr[], int adj[], int size, float avg, float dev)
{
float dropAvg;

float total = 0;

for (int i=0; i<size; i++)
{
if (arr < (avg + dev * 4) || arr > (avg - dev * 4))
}
dropAvg = total / size;
return dropAvg;
}

Please put your code between CODE tags to help format it correctly. It's darn hard to read that way with all the indentation gone. Just put [ CODE] in front of your code, and [ /CODE] after it, remembering to remove the spaces after the square brackets.

The first one is free, as they say.

Code:
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

float stdDev(int arr[], int size, float avg);
float calcAverage(int arr[], int size);
float devDrop(int arr[], int adj[], int size, float avg, float dev);

int main()
{
const int size = 10;
int number[size];
float avg, dev, dropAvg;

for (int i=0; i<size; i++)
{
cout << "Enter Number #" << i+1 << ":";
cin >> number[i];
}

avg = calcAverage(number, size);
cout << avg;

cout << endl;

dev = stdDev(number, size, avg);
cout << dev;

cout << endl;

dropAvg = devDrop(number, adj, size, avg, dev);
cout << dropAvg;

return 0;
}

float calcAverage(int arr[], int size)
{
float avg;
int total = 0;
for (int i=0; i<size; i++)
total += arr[i];
avg = float(total) / size;
return avg;
}

float stdDev(int arr[], int size, float avg)
{
float dev;
float total = 0;
for (int i=0; i<size; i++)

total += pow(avg - arr[i], 2);
dev = sqrt(float(total) / size);

return dev;
}

float devDrop(int arr[], int adj[], int size, float avg, float dev)
{
float dropAvg;

float total = 0;

for (int i=0; i<size; i++)
{
if (arr[i] < (avg + dev * 4) || arr[i] > (avg - dev * 4))
}
dropAvg = total / size;
return dropAvg;
}

I've only taken a very quick look, so there may be other mistakes. However, remember that after not counting some elements, you no longer have size elements added together. The new average is the new total divided by the number of elements you added to the sum. So count each entry you add to the sum, and divide by that number.

Let us know if you get stuck anywhere else, of course.

## What is the purpose of calculating standard deviation in a C++ program?

The purpose of calculating standard deviation in a C++ program is to measure the spread of data points from the mean. It is commonly used in statistical analysis to determine the variability of a set of data.

## How do you calculate standard deviation in a C++ program?

To calculate standard deviation in a C++ program, you can use the standard deviation formula:
sqrt(sum((x - mean)^2) / n)
where x is each data point, mean is the average of the data points, and n is the total number of data points.

## What is the difference between population and sample standard deviation?

Population standard deviation is used when the data set includes all members of a population, while sample standard deviation is used when the data set is a subset of a larger population. The formulas for calculating population and sample standard deviation are slightly different.

## Can standard deviation be negative in a C++ program?

No, standard deviation cannot be negative in a C++ program. It is a measure of variability and can never be negative. If you get a negative value when calculating standard deviation, it is likely due to an error in your code or data.

## What are some common errors when calculating standard deviation in a C++ program?

Some common errors when calculating standard deviation in a C++ program include using the wrong formula, not properly accounting for all data points, and not properly initializing variables. It is important to carefully check your code and data to identify any potential errors.

Replies
4
Views
1K
Replies
2
Views
2K
Replies
3
Views
906
Replies
3
Views
1K
Replies
8
Views
1K
Replies
14
Views
3K
Replies
2
Views
1K
Replies
7
Views
1K
Replies
3
Views
2K
Replies
24
Views
2K