0

I am writing a program that goes through an array of ints and calculates stdev to identify outliers in the data. From here, I would like to create a new array with the identified outliers removed in order to recalculate the avg and stdev. Is there a way that I can do this?

1
  • 3
    Two arrays, two indexes. Use the input index to go through each element of the input array. Copy an element to the output array if it passes the test, and then increment the output index. Commented Nov 7, 2016 at 22:24

1 Answer 1

1

There is a pretty simple solution to the problem that involves switching your mindset in the if statement (which isn't actually in a for loop it seems... might want to fix that).

float dataMinusOutliers[n];
int indexTracker = 0;
for (i=0; i<n; i++) {
    if (data[i] >= (-2*stdevfinal) && data[i] <= (2*stdevfinal)) {

        dataMinusOutliers[indexTracker] = data[i];
        indexTracker += 1;
    }
}

Note that this isn't particularly scalable and that the dataMinusOutliers array is going to potentially have quite a few unused indices. You can always use indexTracker - 1 to note how large the array actually is though, and create yet another array into which you copy the important values in dataMinusOutliers. Is there likely a more elegant solution? Yes. Does this work given your requirements though? Yup.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.