0

Firstly, I don't post here that often. So apologies if this is out of place. I moderate some forums so I understand why you would find it frustrating if this is in the wrong place or answered 1000 times (have looked 0 success. But might not be looking at the right thing).

I am working on an assignment for one of my degree modules (C) and I am stuck on one item.

I am trying to create a Linear Regression formula that takes an input (test.txt). Reads the text file, places the text file in 2 seperate arrays (xArray, yArray).

The program then takes both of those arrays and uses them to work through the rest of the program (performing various calculations to produce an end result).

Now, the program works as intended. I do have some indentation issues (sorry if it makes your eyes bleed). I have used a test file (with 6 lines), which gives the correct result so I am happy if it works.

However, it ONLY works if I use a array size which matches the lines in the file (for example if I have 6 lines in the file and I change the array size to 7). It will produce 6 lines of accurate values and the 7th line will be 0,0. These values are treated as 1,1.

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

int xArray[6];
int yArray[6];
int nValue = sizeof(xArray)/sizeof(int);
int totalX = 0;
int totalY = 0;
int totalXY = 0;
int totalXSquared = 0;
char ch;
char name[10];

void main() {

    // PROGRAM RUN
    arraySetting();
    sumofX();
    sumofY();
    sumofXY();
    sumofXSquared();
    formula();

}

int arraySetting() {

    int x, y, n;

    FILE *fptr;
    fptr = fopen("test.txt", "r");

    while (!feof(fptr)) {
        for (int a = 0; a <= 6; a++) {
            n = fscanf(fptr, "%d,%d", &xArray[a], &yArray[a]);
        }

    }

    fclose(fptr);

    printf("Read Data Printout\n");

    // ARRAY PRINT
    for (int a = 0; a < (sizeof(yArray)/sizeof(int)); a++) {
        printf("x = %d  y = %d\n", xArray[a], yArray[a]);
    }

    printf("\n");
    printf("Printout read complete\n");
    printf("Press ENTER key to Continue\n");    
    //here also if you press any other key will wait till pressing ENTER
    scanf("%c",&ch); //works as getchar() but here extra variable is required.    
}

int sumofX(){
    for (int a = 0; a < (sizeof(xArray)/sizeof(int)); a++) {
    totalX = totalX + xArray[a];

    }
    printf("The Sum of X is:%d\n",totalX);
    printf("Press ENTER key to Continue\n");    
    //here also if you press any other key will wait till pressing ENTER
    scanf("%c",&ch); //works as getchar() but here extra variable is required.    
}
int sumofY(){
        for (int a = 0; a < (sizeof(yArray)/sizeof(int)); a++) {
        totalY = totalY + yArray[a];
    }
    printf("The Sum of Y is:%d\n",totalY);
    printf("Press ENTER key to Continue\n");    
    //here also if you press any other key will wait till pressing ENTER
    scanf("%c",&ch); //works as getchar() but here extra variable is required.    
}
int sumofXY(){
        for (int a = 0; a < nValue; a++) {
        totalXY = totalXY + (xArray[a] * yArray[a]);
    }
    printf("The Sum of XY is:%d\n",totalXY);
    printf("Press ENTER key to Continue\n");    
    //here also if you press any other key will wait till pressing ENTER
    scanf("%c",&ch); //works as getchar() but here extra variable is required.    
}
int sumofXSquared(){
    for (int a = 0; a < (sizeof(xArray)/sizeof(int)); a++) {
        totalXSquared = totalXSquared + (xArray[a]*xArray[a]);
    }
    printf("The Sum of Xsquared is:%d\n",totalXSquared);
    printf("Press ENTER key to Continue\n");    
    //here also if you press any other key will wait till pressing ENTER
    scanf("%c",&ch); //works as getchar() but here extra variable is required.    
}

int formula() {
    // INPUT THEM INTO FORMULA TO FIND A & B
    double a;
    double b;
    double x;
    double y;
    double value;

    // A CALCULATION
    a = ((totalY * totalXSquared) - (totalX * totalXY));
    a = a / ((nValue * totalXSquared) - (totalX * totalX));

    // B CALCULATION
    b = ((nValue * totalXY) - (totalX * totalY));
    b = b / ((nValue * totalXSquared) - (totalX * totalX));

    // USER INPUT
    printf("Linear Regression formula\n\n");
    printf("Y = %.2lf + %.2lfx\n\n",a, b);
    printf("\nEnter a Value for X: ");
    scanf("%lf", &x);

    // USER INPUTTED VALUE INTO FORMULA TO CALCULATE 'Y'
    y = a + (b * x);
    printf("\nY = %.2lf + %.2lf x %.2lf\n", a, b, x);
    printf("Y = %.2lf\n", y);

}

If the above is used the output of the file read is as follows:

x = 43  y = 99
x = 21  y = 65
x = 25  y = 79
x = 42  y = 75
x = 57  y = 87
x = 59  y = 81

Printout read complete
Press ENTER key to Continue

However, if xArray or yArray is changed to a different value (say 7). The following is seen:

Read Data Printout
x = 43  y = 99
x = 21  y = 65
x = 25  y = 79
x = 42  y = 75
x = 57  y = 87
x = 59  y = 81
x = 0   y = 0

Printout read complete
Press ENTER key to Continue

I understand the concept of what I'm trying to do. The array needs to be flexible to cope with the lines in a file. However, I am unsure how to do this.

Any help would be appreciated.

1
  • 1
    Please show the verbatime content of test.txt. Commented Nov 18, 2019 at 17:41

1 Answer 1

1

Pointers int *xArray may be used to allocate memory as needed.
realloc is used to change the memory allocation. If the pointer is set to NULL, the first call to realloc will behave the same as a call to malloc. For large files it would be better to allocate memory in blocks and keep track of memory usage instead of allocating memory for every pair of numbers.

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

int *xArray = NULL;
int *yArray = NULL;
int nValue = 0;
int totalX = 0;

void arraySetting ( );
void sumofX ( );

int main ( void) {

    arraySetting ( );
    sumofX ( );

    free ( xArray);
    free ( yArray);

}

void arraySetting ( ) {
    int x, y;
    int *temp = NULL;
    FILE *fptr = NULL;

    if ( NULL == ( fptr = fopen("test.txt", "r"))) {
        perror ( "test.txt");
        exit ( EXIT_FAILURE);
    }

    while ( 2 == fscanf(fptr, "%d,%d", &x, &y)) {
        if ( NULL == ( temp = realloc ( xArray, sizeof ( *xArray) * ( nValue + 1)))) {
            fprintf ( stderr, "realloc problem xArray\n");
            exit ( EXIT_FAILURE);
        }
        xArray = temp;
        if ( NULL == ( temp = realloc ( yArray, sizeof ( *yArray) * ( nValue + 1)))) {
            fprintf ( stderr, "realloc problem yArray\n");
            exit ( EXIT_FAILURE);
        }
        yArray = temp;

        xArray[nValue] = x;
        yArray[nValue] = y;

        nValue++;
    }

    fclose(fptr);

    printf("Read Data Printout\n");

    // ARRAY PRINT
    for (int a = 0; a < nValue; a++) {
        printf("x = %d  y = %d\n", xArray[a], yArray[a]);
    }

    printf("\n");
    printf("Printout read complete\n");
    printf("Press ENTER key to Continue\n");
    //here also if you press any other key will wait till pressing ENTER
    char ch = 0;
    scanf("%c",&ch); //works as getchar() but here extra variable is required.
}

void sumofX ( ) {
    totalX = 0;
    for (int a = 0; a < nValue; a++) {
        totalX = totalX + xArray[a];
    }
    printf("The Sum of X is:%d\n",totalX);
    printf("Press ENTER key to Continue\n");
    //here also if you press any other key will wait till pressing ENTER
    char ch = 0;
    scanf("%c",&ch); //works as getchar() but here extra variable is required.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. I do however get an issue that it misses the first line of the text file (X = 43, Y= 99). Why is that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.