- #1
cromwest
- 1
- 0
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
Homework Equations
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];
int adj[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))
total += adj;
}
dropAvg = total / size;
return dropAvg;
}