-1

I wanted to create a program to calculate the regression line of some given data, along with the errors, so I can use in my university assignments. This is the following program:

#include <stdio.h>
#include <math.h>
int main(void)
{
    int i,n,N;
    double x[n],y[n],a=0.0,b=0.0,c=0.0,d=0.0,D,P=0.0,p,Sx,A,B,dA,dB;
    printf("Give the number of data you want to input for x : ");
    scanf("\n%d", &N);
    printf("\nGive the values of x : ");
    for (i=1; i<=N; i++);
    {
        printf("\n Enter x[%d] = ", i);
        scanf("%lf", &x[i]);
        a+=x[i];
        b+=pow(x[i],2);
    }
    printf("\nGive the values of y : ");
    for (i=1; i<=N; i++);
    {
        printf("\n Enter y[%d] = ", i);
        scanf("%lf", &y[i]);
        c+=y[i];
    } 
    D=N*b-pow(a,2);
    A=(b*c-a*d)/D;
    B=(N*d-a*c)/D;
    for (i=1; i<=N; i++);
    {
        d+=x[i]*y[i];
        p=y[i]-A-B*x[i];
        P+=pow(p,2);
    }
    Sx=sqrt(P/N-2);
    dA=Sx*sqrt(b/D);
    dB=Sx*sqrt(N/D);
    printf("\n x \t \t \t y");
    for (i=1; i<=N; i++);
        printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
    printf("\nA = %lf\t B = %lf", A,B);
    printf("\nThe errors of A & B are dA = %lf and dB = %lf", dA,dB);
    printf("\nThe equation of the regression line is y=%lfx+(%lf)", B,A);
    return 0;
} 

I have two problems.

  1. Despite giving a value to N, the program runs so that I can only give one value for x and one value for y. Why and where is the mistake?
  2. When printing the "Enter x[%d]", it displays x[11] and at the end when printing "x[%d] = %lf\t%lf = y[%d]", it displays x[0]. Again why and where is the mistake?

Thank you for your help!

  • 1
    Arrays start with 0 in C so I fnd all of your loops very suspect. Also, your initial `n` is undefined so you cannot create your arrays with that value. Do you have *any* warning settings on your compiler set up? – Jongware Nov 24 '18 at 11:55
  • You have: `int i,n,N; double x[n],y[n], …` — the value of `n` is undefined, so you've no idea what size the `x` and `y` arrays are. I doubt if things go well after that. – Jonathan Leffler Nov 24 '18 at 12:08
  • I'm surprised you don't need ∑y². I think you would be better off entering (x,y) pairs rather than "all the x's" and then "all the y's". – Jonathan Leffler Nov 24 '18 at 12:13
  • The compiler they hade us download is a bit bugged and for me it crashes when running program with 30 or so lines. I want to ask them for a solution. That is why I run this program on a website called [https://repl.it/languages]. Why is **n** undefined? I wrote **int n** or should I have written **x[N],y[N]** and dismiss **n**? Also, my loops should have been **for (i=1; i<=N; i++);** and then **printf("\n Enter x[%d] = ", i+1);**? – Negafilms Origins Nov 24 '18 at 12:21
  • Also, I don't know too much about coding in C, since I have been learning it for almost two months and I mainly study Physics, so I make do with simple stuff that I know. – Negafilms Origins Nov 24 '18 at 12:25
  • No, your loops should be `for (i=0; i – Jongware Nov 24 '18 at 13:26

2 Answers2

0
  1. You are trying to create a dynamic array in C.

To do that, you need to use dynamic memory allocation with malloc and free. So, your code should look something like this:

int N;
double *x;

printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);

x = malloc(sizeof(double) * N);

Then, at the end of your program you need to free the memory:

free(x);

If you don't want to deal with manual memory management (or can't for some reason), you can use a static maximum array size like this:

#define MAX_N_X  100

int main(void) {
    int N;
    double x[MAX_N_X];

    printf("Give the number of data you want to input for x :\n");
    scanf("%d", &N);

    if (N > MAX_N_X) {
        printf("Can't handle that many inputs! Maximum %d\n", MAX_N_X);
        return 0;
    }
}
  1. You simply missed two parameters to printf.

Yo wrote:

printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);

But it should be:

printf("\nx[%d] = %lf\t%lf = y[%d]", i, x[i], y[i], i);
Lev M.
  • 6,088
  • 1
  • 10
  • 23
0

I found the main problem with this program a few days after posting this and the fix would be removing the ; from the for commands, along with some other minor changes. I thought I might add this comment to let you know and now it is working like a charm. The simplest of mistakes fools even the trained eyes. After I found this, I was shocked that no one picked up on this mistake.