Need help with standard deviation C++ program

Click For Summary
SUMMARY

The forum discussion focuses on creating a C++ program that calculates the average and standard deviation of ten floating-point values input by the user. The user successfully computes the initial average and standard deviation but struggles with filtering out values that are more than four standard deviations from the average. A suggested solution involves creating an additional array to store valid values, but the user encounters issues with implementation. Key functions discussed include calcAverage, stdDev, and devDrop.

PREREQUISITES
  • Understanding of C++ syntax and data types
  • Knowledge of functions and arrays in C++
  • Familiarity with mathematical concepts of average and standard deviation
  • Experience with conditional statements and loops in C++
NEXT STEPS
  • Implement a filtering mechanism to count valid entries in the devDrop function
  • Research dynamic arrays in C++ to handle variable-sized input
  • Learn about the std::vector class for more efficient data handling
  • Explore error handling in C++ to manage user input effectively
USEFUL FOR

C++ developers, students learning programming concepts, and anyone interested in statistical calculations within software applications.

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++)
{
count << "Enter Number #" << i+1 << ":";
cin >> number;
}

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

count << endl;

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

count << endl;

dropAvg = devDrop(number, adj, size, avg, dev);
count << 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 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K