correlation prob

Hi guys, i'm stuck with this program here. Any help is appreciated. So the question that was given was this:
Find the correlation coefficient for the 10 observations of the fetal head diameter x and the femur length y.

DATA:
X Y
56 45
65 49
47 35
57 44
62 45
48 40
68 52
75 57
79 62
49 39


For these data:

(a) calculate mean , variance and standard deviation for x and y
(b) compute the correlation coefficient
(c) based on the result of the correlation coefficient determine if there is a correlation between x and y

Note: create two functions to compute the variance and correlation coefficient

Here's the code that i wrote out but i feel as if it's a crazy mess and is outputting wrong results. It's not listing all the data and is calculating it and giving the answer as 0.

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;

	}


}



Please Help. Thank you!
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

const int SIZE = 10;

double mean( int[] );

int main()
{
    int x[] = {56,65,47,57,62,48,68,75,79,49};
    int y[] = {45,49,35,44,45,40,52,57,62,39};

    double meanX, meanY;

    meanX = mean ( x );
    cout << meanX << endl;

    meanY = mean ( y );
    cout << meanY << endl;

    return 0;
}

double mean( int zzz[] )
{
    double m = 0;

    for (int i = 0; i < SIZE; i++ )
        m += zzz[i];

    m /= SIZE;

    return m;
}


Try a fresh start and use this as a pattern. You are using arrays everywhere which is totally unnecesary. :-)
Last edited on
Topic archived. No new replies allowed.