1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
#include <cmath>
using namespace std;
void compute(int x[], int y[], double totalx[], double totaly[], double meanx[], double meany[],double totalvaluex[], double totalvaluey[], double variancex[], double variancey[], double stddevx[], double stddevy[], int count[], const int size);
void correlation(int x[], int y[], double meanx[], double meany[], double stddevx[], double stddevy[], double nume[], double denom[], double coefnume[], double corrcoef[], int count[], const int size);
void printdata (int x[], int y[], double meanx[], double meany[], double variancex[], double variancey[], double stddevx[], double stddevy[],double corrcoef[], const int size);
int main()
{
const int size= 10;
int x[size] = {56,65,47,57,62,48,68,75,79,49};
int y[size] = {45,49,35,44,45,40,52,57,62,39};
double meanx[size]= {0}, meany[size] = {0}, variancex[size] = {0}, variancey[size] = {0};
double stddevx[size] = {0}, stddevy[size] = {0}, corrcoef[size] = {0};
int count[size] = {0};
double totalx[size] = {0}, totaly[size] = {0}, totalvaluex[size] = {0}, totalvaluey[size] = {0}, nume[size] = {0}, denom[size] = {0}, coefnume[size] = {0};
compute(x, y, totalx, totaly, meanx, meany, totalvaluex, totalvaluey, variancex, variancey, stddevx, stddevy, count, size);
correlation(x, y, meanx, meany, stddevx, stddevy, nume, denom, coefnume, corrcoef, count, size);
printdata(x, y, meanx, meany, variancex, variancey, stddevx, stddevy, corrcoef, size);
cin>>corrcoef[size];
return 0;
}
void compute(int x[], int y[], double totalx[], double totaly[], double meanx[], double meany[],double totalvaluex[], double totalvaluey[], double variancex[], double variancey[], double stddevx[], double stddevy[], int count[], const int size)
{
for (int a=0; a<size; a++)
{
totalx[a] = totalx[a] + x[a];
totaly[a] = totaly[a] + y[a];
count[a] = count[a] + 1;
meanx[a] = totalx[a]/count[a];
meany[a] = totaly[a]/count[a];
}
for (int b=0; b<size; b++)
{
totalvaluex[b] = totalvaluex[b] + ((x[b] - meanx[b]) * (x[b] - meanx[b]));
totalvaluey[b] = totalvaluey[b] + ((y[b] - meany[b]) * (y[b] - meany[b]));
count[b] = count[b] +1;
variancex[b] = totalvaluex[b]/count[b];
variancey[b] = totalvaluey[b]/count[b];
stddevx[b] = sqrt(variancex[b]);
stddevy[b] = sqrt(variancey[b]);
}
}
void correlation(int x[], int y[], double meanx[], double meany[], double stddevx[], double stddevy[], double nume[], double denom[], double coefnume[], double corrcoef[], int count[], const int size)
{
for (int i=0; i<size; i++)
{
nume[i] = nume[i] + ((x[i] - meanx[i]) * (y[i] - meany[i]));
count[i] = count[i] +1;
coefnume[i] = nume[i]/count[i];
denom[i] = (stddevx[i]) * (stddevy[i]);
corrcoef[i] = coefnume[i]/denom[i];
}
}
void printdata (int x[], int y[], double meanx[], double meany[], double variancex[], double variancey[], double stddevx[], double stddevy[],double corrcoef[], const int size)
{
for(int c = 0 ; c<size; c++)
{
cout<<setw(13)<<"List the values of x "<<x[c]<<endl<<setw(13)<<"List the values of y "<<y[c]<<endl<<endl;
cout<<setw(13)<<"The mean x is "<<meanx[c]<<endl<<setw(13)<<"The mean y is "<<meany[c]<<endl<<endl<<setw(13)<<"The variance x is "<<variancex[c]<<endl<<setw(13)<<"The variance y is "<<variancey[c]<<endl<<endl;
cout<<setw(13)<<"The Standard deviation of x is "<<stddevx[c]<<endl<<setw(13)<<"The Standard deviation of y is "<<stddevy[c]<<endl<<endl<<setw(13)<<"The correlation coefficient is "<<corrcoef[c]<<endl<<endl;
}
}
|