You are on page 1of 4

Program 6: Correlation

For this assignment, compute the correlation coefficient from a sample of (x, y) pairs. In the
above picture, each point is a pair (xi, yi).
First compute the sample average value for x and for y:

Use a similar formula for the average y. Next compute the sample standard deviation for x and
standard deviation for y:

Use a similar formula for y. To compute the correlation coefficient r use the following formula:

Of course, these are double precision values.

The correlation coefficient will always in the range -1.0 to +1.0. There are many different formulas
for correlation. Use only the formulas given above.

To start, dont do input of the (x, y) pairs.

Instead, have the following at the top of the

program:
int n = 21;
double x[] = {79,70,79,74,70,93,88,94,64,60,62,89,92,73,
72,85,94,84,86,82,87 };
double y[] =
{94,115,96,104,106,136,99,128,78,48,87,127,140,136,134,115,
135,115,82,65,132 };
Each value x[i] is paired with the corresponding y[i]. You will have to edit the program to
use different sets of data. This is a kludge, but good enough for debugging. Make sure your
program works on all sets of data, not just the above. It would be sensible to write separate
functions that compute the average and standard deviation of an array of values.
The above (x, y) pairs are midterm exam grades and final exam grades for a section of CS 153.
Are they correlated?
The program will print out something like:
n = 21; correlation coefficient = 0.5661

Next, edit the program so that it inputs the values for the arrays from standard input.

The data
will be in a separate text file that starts with the number of data pairs, n, and then has n pairs of
data one per line. You will need to change the declaration of the arrays so that each has a fixed
size of 100 (but can easily be changed). Dont attempt to change the size of the array as the
program is running. C arrays are not objects.
int n = 0;
const int maxSize = 100;
double x[ maxSize ] ;
double y[ maxSize] ;
The data file will look something like this:
5
1.1 2.3
2.4 3.96
7.8 10.23
3.03 5.12
-0.5 1.8
Usually the file will have many more pairs.

Because the program does input from standard input, the data can be input directly from the
keyboard or by input file redirection. Normally you will use input file redirection. The data file
and the executable file should be in the same subdirectory. DO NOT write out prompts to the
user.
C:\Projects\Correlation\Debug\prog.exe < dataFile.txt
n = 21; correlation coefficient = 0.5661
Turn In: Your two source files (only) using the Blackboard assignment feature. The first source
file is the version that has the data declared in the program; the second is the version that does
input from standard input. Make sure your programs compile and run under VC++ . Dont zip the
files. At the top of each file have a block of documentation:
/*
File Name:
Author:
Date:
Brief Description:
*/

You might also like