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.
test.txt.