i'm trying to display the values (min, max, mean, standard deviation, etc. ) and it's giving me most things back with 0 in the decimal place. the sum of the array is coming out to be 210.1, when in reality it is 215.6. so all of the decimals are being dropped off. except for .1, it is keeping that, but it is dropping .7 down to 0. so it's throwing off all my equations. is it that in the for loop to find the sum, i set "i" as int? i've tried using double and float, but it gives me error messages saying i can't do that. i'm not sure what to do. and yeah i know my standard deviation equation is wrong, i'll fix that.
Code:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <ctype.h>
#include <math.h>
using namespace std;
int i; //declaring i as int.
void sortarrays (float ar[]); //declaring function prototypes
void calcByAddr( float ar[], int, double*, double*, double*, double*, double* );
int main()
{
float ar[15] = {2.4, 6.5, 1.2, 0.7, 15.3, 3.9, 78.1, 12.0, 5.4, 10.1, 24.0, 7.8, 46.8, 1.3, 0.1}; //initializing array
int num;//declaring int and double values to be used
double min;
double max;
double average;
double standarddev;
double median;
sortarrays(ar);//calling the two functions to be used. first sort the array, then calculate the values by address
calcByAddr(ar, num, &min, &max, &average, &standarddev, &median);
return 0;
}
/******************************************************************
Function: sortarrays
Use: Sorts the array in ascending order
Arguments: Takes a double array as its argument
Returns: None
***************************************************************/
void sortarrays (float ar[])
{
int i, j;
for (i=0; i<15; i++)
{
for(j=i+1; j<15; j++)
{
if(ar[i]>ar[j]) //CONDITION :
{
int temp;
temp = ar[i]; //save a copy of value in i
ar[i] = ar[j]; //copy value from j to i
ar[j] = temp; //copy saved value from i to j
}
}
}
}
/******************************************************************
Function: calcByAddr
Use: Calculates various values such as minimum, maximum, median, etc.
Arguments: 1 array, 1 integer that holds the number of values in the array,
and five pointers to doubles. .
Returns: None
***************************************************************/
void calcByAddr( float ar[], int num, double* min, double* max, double* average, double* standarddev, double* median )
{
int i; //declaring variables to be used
double sum=0;
double var=0;
double a=0;
double b=0;
double size=15;
double x=0;
*min=ar[0]; //setting value for min and max
*max=ar[14];
for (i=0; i<15; i++) //setting for loop to calculate the sum of the array
{
sum+=ar[i];
};
*median = ar[7]; //setting value for median
*average=sum/size; //setting value for the average
for (i=0; i<15; i++)
{
x=ar[i]*ar[i];
};
*standarddev=sqrt(((x)-(sum*sum)/14)/13);
cout<<"Results Using Pointers";
cout<<"\nMinimum: "<<right<<setw(20)<<fixed<<setprecision(1)<<*min;
cout<<"\nMaximum: "<<right<<setw(20)<<*max;
cout<<"\nMean: "<<right<<setw(23)<<*average;
cout<<"\nMedian: "<<right<<setw(21)<<*median;
cout<<"\nStandard Deviation: "<<right<<setw(9)<<*standarddev; //displaying the final results
}