I've been trying to write a code for standard deviation and I thought I nearly had it, but it's still slightly off. Could I please get some help? It's getting a bit frustrating.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
double arrStdDev(int array[], int length){
double mean = 0;
double sum = 0;
double var = 0;
double avg = 0;
for(int i = 0; i < length; i++){
if(length > 0){
mean += array[i];
avg = mean /= length;
sum += (array[i] - avg)*(array[i] - avg);
var = sum / length;
return sqrt(var);
}else{
return 0;
}
}
return 0;
}
// my population standard deviation function
double stdDev(constint arr[], constint& size)
{
double mean{};
for(int a{}; a < size; a++) mean += (double)arr[a];
mean /= size;
double var{}; // variance
for(int b{}; b < size; b++) {
// calculate the difference from the mean
double diff{ arr[b] - mean };
// square the difference
var += diff * diff;
}
// variance is the average of the above
var /= size;
return sqrt(var);
}
stdDev = 1.41421
arrStdDev = 0.357771
I don't think it's slightly off.
The problem with your function is that you calculate the mean once per loop, length times, when you're meant to loop through the whole array, get the sum and divide that by the size of the array.
Okay, sorry, it was more messed up than I thought.
I took the code you gave and changed around some syntax to suit my assignment/machine (my version won't allow me to do whatever you did with the braces, i have to declare them normally), and I'm still failing the tests I'm running..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
double arrStdDev(constint arr[], constint& size){
if(size > 0){
double mean = 0;
for(int a = 0; a < size; a++){
mean += (double)arr[a];
mean /= size;
}
double var = 0;
for(int t = 0; t < size; t++){
double diff = arr[t] - mean;
var += diff * diff;
}
var /= size;
return sqrt(var);
}else{
return 0;
}
}
Alright. I think that improved it a bit, but the tests are still failing. What else may be a problem?
Running cxxtest tests (28 tests).................
In MyTests::testStdDev2:
stats_test.h:100: Error: Expected (arrStdDev(a, 3) == 0.816), found (0.8164 != 0.8160)
In MyTests::testStdDev3:
stats_test.h:105: Error: Expected (arrStdDev(a, 5) == 1.414), found (1.4142 != 1.4139)
In MyTests::testStdDev4:
stats_test.h:110: Error: Expected (arrStdDev(a, 4) == 1.118), found (1.1180 != 1.1180)
I'm still confused..the instructions for my assignment said to return the final result as a double. Should I be swapping all my = signs with the isEqual command?
I don't know what you're trying to ask.
You've fixed the standard deviation calculations but because they're a floating point value, there is no true way to test the equality.
As far as I'm concerned, you've fixed your problem which was the standard deviation function.
It's okay, I'm just frustrated because this lab is due in an hour and I've now finished every part besides this one, which is preventing me from submitting it for grading properly.
I need to know how to modify the code so that it passes unit tests, because as it is I'm getting a 0% in that category. Can I change the code away from floating point values so that it outputs what the unit test wants?