In: Computer Science
I'm trying to create a function determines the range of outliers in an array using pass by reference.
I need to write a function that outputs the +3 and -3 standard deviation. I'm having a hard time figuring out what I should identify as my pointers and how to use the pass by reference. This is what I have so far:
#include <stdio.h>
#define ARRAY_SIZE 20
double homeworkArray[ARRAY_SIZE] = { 1.3, 5.7, 2.1, -1.2, 0.5, 4.3, 2.1, 50.2, 3.4, 1.1,
-2.1, 4.1, 6.7, 3.9, 4.1, -0.5, -3.3, 2.1, 5.1, -3.1 };
double CalculateMean(double data[], int size)
{
int i;
double sum = 0.0;
for (i = 0; i < size; i++)
{
sum += data[i];
}
return sum / size;
}
double CalculateStandardDeviation(double data[], int size)
{
double mean = CalculateMean(data, size);
double variance = 0.0;
double diff;
int i;
for (i = 0; i < size; i++)
{
diff = data[i] - mean;
variance += diff * diff;
}
return sqrt(variance / (size));}
}
int main(int argc, const char * argv[]) {
double lowerRange=0.0, upperRange=0.0;
double mean = CalculateMean(homeworkArray, ARRAY_SIZE);
double stddev = CalculateStandardDeviation(homeworkArray, ARRAY_SIZE);
printf("Anything outside of (%.2lf - %.2lf) is an outlier.\n",lowerRange,upperRange);
return 0;
}
Note: Could you plz go this code and let me know if
u need any changes in this.Thank You
_________________
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define ARRAY_SIZE 20
double homeworkArray[ARRAY_SIZE] = { 1.3, 5.7, 2.1, -1.2, 0.5, 4.3, 2.1, 50.2, 3.4, 1.1,
-2.1, 4.1, 6.7, 3.9, 4.1, -0.5, -3.3, 2.1, 5.1, -3.1 };
double CalculateMean(double data[], int size)
{
int i;
double sum = 0.0;
for (i = 0; i < size; i++)
{
sum += data[i];
}
return sum / size;
}
double CalculateStandardDeviation(double data[], int size)
{
double mean = CalculateMean(data, size);
double variance = 0.0;
double diff;
int i;
for (i = 0; i < size; i++)
{
diff = data[i] - mean;
variance += diff * diff;
}
return sqrt(variance / (size));
}
void findOutliers(double* outliers,int *cnt,double
lowerRange,double upperRange);
int main(int argc, const char * argv[]) {
double lowerRange=-3, upperRange=3;
double mean = CalculateMean(homeworkArray, ARRAY_SIZE);
double stddev = CalculateStandardDeviation(homeworkArray, ARRAY_SIZE);
printf("Anything outside of (%.2lf - %.2lf) is an outlier.\n",lowerRange,upperRange);
/* Creating an float array dynamically
* based on the size entered by the user
*/
double* outliers = (double*)malloc(ARRAY_SIZE *
sizeof(double));
int cnt=0;
int i;
findOutliers(outliers,&cnt,lowerRange,upperRange);
for(i=0;i<cnt;i++)
{
printf("%.1lf ",outliers[i]);
}
printf("\n");
return 0;
}
void findOutliers(double* outliers,int *cnt,double
lowerRange,double upperRange)
{
int i,n=0;
for( i=0;i<ARRAY_SIZE;i++)
{
if(homeworkArray[i]<lowerRange
|| homeworkArray[i]>upperRange)
{
outliers[n]=homeworkArray[i];
n++;
}
}
*cnt=n;
}
______________________
Output:
____________Thank You