Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 574929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:53:01+00:00 2026-05-13T13:53:01+00:00

I have an assignment in my C programming class to write a program to

  • 0

I have an assignment in my C programming class to write a program to get the correlation coefficient of 2 sets of real numbers. I’ve been given the equations, and it referenced wikipedia so I double checked the equations there. Here is a link to the equation, which seems to be pretty standard from my research:

alt text

I’ve written the program, but when I ran it I was getting numbers greater than 1 for my results, which I knew wasn’t correct. I looked over my code several times but couldn’t find anything out of place, so I tried dividing by n at the end instead of n-1, this gave me values with the -1 to 1 range that I expected, so i tested it against data values that I found online as well as a correlation coefficient calculator ( http://easycalculation.com/statistics/correlation.php ) and I’m now getting correct results for all of the numbers I input. I can’t figure out why this is, so thought I might be able to get a little help with it here. Here is my code for the program, If there is anything else that stands out that I have done wrong here I would love to hear some advice, but mostly I’m trying to figure out why I’m getting the right results with what appears to be the wrong equation.

It will then read in the values for both arrays(x and y), and then computes
the correlation coefficient between the 2 sets of numbers.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
   int n;  /* value to determine array length */
   /* declare variables to hold results for each equation for x and y
   initialize all to zero to prepare for summation */
   float r = 0.0, xbar = 0.0, ybar = 0.0, sx = 0.0, sy = 0.0;

   /*get number n input from user */
   printf("Please enter a number n: ");
   scanf("%d", &n);

   if( n < 1) {
      printf("n must be a positive number.\nPlease enter a new value: ");
      scanf("%d", &n);
      if( n < 1) {
         printf("Invalid input, exiting...\n");
         return 0;
      }
   }

   /*initialize arrays x and y with length of n */
   float x[n], y[n];
   /*use for loop to read in values of x*/
   int i;
   for(i = 0; i < n; ++i) {
      printf("Please enter a number for x: ");
      scanf("%f", &x[i]);
   }
   /*use for loop to read in values of y*/
   for(i = 0; i < n; ++i) {
      printf("Please enter a number for y: ");
      scanf("%f", &y[i]);
   }

   /*compute xbar */
   for(i = 0; i < n; ++i) {
      xbar += x[i];
   }
   xbar /= n;
   /*compute ybar*/
   for(i = 0; i < n; ++i) {
      ybar += y[i];
   }
   ybar /= n;

   /* compute standard deviation of x*/
   for(i = 0; i < n; ++i) {
      sx += (x[i] - xbar) * (x[i] - xbar);
   }
   sx = sqrt((sx / n));
   /* compute standard deviation of y */
   for(i = 0; i < n; ++i) {
      sy += (y[i] - ybar) * (y[i] - ybar);
   }
   sy = sqrt((sy / n));

   /*compute r, the correlation coefficient between the two arrays */
   for( i = 0; i < n; ++i ) {
      r += (((x[i] - xbar)/sx) * ((y[i] - ybar)/sy));
   }
   r /= (n); /* originally divided by n-1, but gave incorrect results
   dividing by n instead produces the desired output */
   /* print results */ 
   printf("The correlation coefficient of the entered lists is: %6.4f\n", r);
   return 0;

}

(it looks like my code formatting isn’t working, very sorry about this. Tried using tags and the button but can’t figure it out. It looks like I got it working somewhat, better than before.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:53:02+00:00Added an answer on May 13, 2026 at 1:53 pm

    You are calculating your standard deviation as:

    sx = sqrt((sx / n));
    

    and similarly for sy.

    The equation you have used uses n-1 in the denominator for calculating this (reason: there are n-1 degrees of freedom, so you should divide by n-1). So, your sx and sy are actually sx' and sy', where sx' = sx*sqrt(n-1)/sqrt(n), and sy' = sy*sqrt(n-1)/sqrt(n). So, sx' * sy' = sx * sy * (n-1)/n. Since sx*sy is in the denominator, your calculation is off by a factor of n/(n-1). Dividing this by n gives you the factor you need outside of the summation.

    So if you changed your code to calculate the sample standard deviation (divide by n-1), you can finally divide by n-1 and your code will get the result you expect. For efficiency, since the division is going to cancel out anyway, you can save some computation and increase your accuracy by simply not dividing by n-1 in calculations of sx and sy, and then omit the final division as well:

    sx = sqrt((sx / n));
    sy = sqrt((sy / n));
    

    become

    sx = sqrt(sx);
    sy = sqrt(sy);
    

    and:

    r /= (n);
    

    goes away altogether.

    Edit: Since you asked…

    1. There is no reason to use float unless you have to. double gives you much better precision.
    2. By default, stdout is line buffered on most systems, so your prompt may not appear before your call to scanf(). To make sure your prompt shows, do fflush(stdout); after your printf() call.
    3. It is very hard to use scanf() safely. For reading numbers, scanf() has undefined behavior when someone enters a number that’s not in the range of the data type. Also, it is bad for cases like when someone enters a non-integer in response to your prompt. For your case, you can make n passable as a command-line parameter, and then use strtol(argv[1]) to parse the number. If you want to read from stdin anyway, use fgets() + sscanf() combination, or fgets() + strtol().
    4. You can reduce the number of loops in your program. For one, you can calculate xbar and ybar in the same loop. Even better, you can write a function double avg(double *data, int n), that calculates average of n values, and then do: xbar=avg(x, n);, ybar=avg(y, n);.
    5. Similarly, you can define a function double std(double *data, int n), and then use that to calculate sx and sy.
    6. Finally, although it’s okay, you have way too many parentheses: sqrt((sx / n)); is better written as sqrt(sx / n);. r /= (n); doesn’t need the parentheses either.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently for a programming class, we were given the assignment to write a program
We were given an assignment in our C programming class to modify a program
I have completed the assignment (yes it is for a programming class), but I
For my second programming assignment in my Java class, we have to create a
I have an assignment from my programming class, which is very poorly worded... The
For a programming assignment, we are given a template class with two members declared
As part of the last assignment in a beginner python programing class, I have
For a programming assignment, we have the following requirements: It needs to be a
I'm rushing against the clock for a programming assignment in which I have to
I have an assignment about fftw and I was trying to write a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.