Comp Sci Need help with standard deviation C++ program

AI Thread Summary
The discussion revolves around creating a C++ program to calculate the average and standard deviation of 10 user-input floating-point values, while excluding values that are more than four standard deviations from the average. The user has successfully implemented the initial calculations but struggles with the logic for dropping outlier values and recalculating the average. A suggestion is made to count the valid entries when calculating the new average after filtering outliers. The user is advised to ensure that the new average is computed by dividing the total by the count of included values, rather than the original size. This guidance aims to help refine the program's functionality and accuracy.
cromwest
Messages
1
Reaction score
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;
}
 
Physics news on Phys.org
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];
 int adj[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))
        total += adj[i];
    }
    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.
 

Similar threads

Replies
4
Views
1K
Replies
2
Views
3K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
8
Views
1K
Replies
14
Views
4K
Replies
2
Views
2K
Replies
7
Views
2K
Replies
3
Views
2K
Back
Top