gpt4 book ai didi

根据文件中的行创建数组

转载 作者:行者123 更新时间:2023-11-30 14:34:34 26 4
gpt4 key购买 nike

首先,我不经常在这里发帖。如果这是不恰当的,我们深表歉意。我管理一些论坛,所以我理解为什么如果这是在错误的地方或回答了 1000 次(看上去成功率为 0。但可能没有看到正确的东西),您会感到沮丧。

我正在为我的一个学位模块 (C) 做一项作业,但我被困在一个项目上。

我正在尝试创建一个接受输入(test.txt)的线性回归公式。读取文本文件,将文本文件放入 2 个单独的数组(xArray、yArray)中。

然后,程序获取这两个数组并使用它们来完成程序的其余部分(执行各种计算以产生最终结果)。

现在,程序按预期运行。我确实有一些缩进问题(很抱歉,如果它让你的眼睛流血)。我使用了一个测试文件(有 6 行),它给出了正确的结果,所以如果它有效,我很高兴。

但是,只有当我使用与文件中的行匹配的数组大小时,它才有效(例如,如果文件中有 6 行,并且我将数组大小更改为 7)。它将产生 6 行精确值,第 7 行将为 0,0。这些值被视为 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);

}

如果使用上述,读取文件的输出如下:

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

但是,如果 xArray 或 yArray 更改为不同的值(例如 7)。可以看到如下:

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

我理解我正在尝试做的事情的概念。该数组需要灵活地处理文件中的行。但是,我不确定如何执行此操作。

如有任何帮助,我们将不胜感激。

最佳答案

指针int *xArray可用于根据需要分配内存。
realloc 用于更改内存分配。如果指针设置为 NULL,则第一次调用 realloc 的行为与调用 malloc 的行为相同。对于大文件,最好按 block 分配内存并跟踪内存使用情况,而不是为每对数字分配内存。

#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.
}

关于根据文件中的行创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com