gpt4 book ai didi

c - 在 C 中对齐数据集列

转载 作者:行者123 更新时间:2023-11-30 15:06:11 24 4
gpt4 key购买 nike

我有一个包含两组的数据集:红色和绿色,我想比较比率之间的差异,但它们必须首先对齐。

原始文件(前几行 200,000 个条目)

A   B       C       D
Red Ratio Green Ratio
1 0.35 1 0.21
2 0.45 2 0.235
3 0.45 3 0.154
4 0.235 4 0.156
6 0.156 5 0.146
7 0.668 6 0.154
8 0.44 7 0.148
9 0.446 8 0.148
10 0.354 9 0.199
11 0.154 10 0.143
12 0.49 12 0.148

使用代码后,值会对齐,“额外”会被删除,列会向上移动。

A   B   C   D
Red Ratio Green Ratio
1 0.35 1 0.21
2 0.45 2 0.235
3 0.45 3 0.154
4 0.235 4 0.156
6 0.156 6 0.154
7 0.668 7 0.148
8 0.44 8 0.148
9 0.446 9 0.199
10 0.354 10 0.143
12 0.49 12 0.148
15 0.146 15 0.87
17 0.113 17 0.113
19 0.44 19 0.448

这是我到目前为止的代码:我正在计算 A 和 C 之间的差异来检查它们是否为 0,如果不是则调整它们。

#include <stdio.h>

int deletemove(char column, int row)
{
// This script would delete the positions mentioned in the arguments, and shift the other values up.
}


int main(void)
{

//Opening input file for read/write

FILE *input;

input=fopen("/full/path/file.xlsx", "r");

if (input == NULL) {printf("error opening input file\n");}

//Store the values from file into an array

int colA[1024];
int colC[1024];

// read contents of columns A and C and store in an array
int ai;
for(ai=1; ai<1024; ai++)
{ fseek(input,ai,SEEK_SET);
colA[ai]=fgetc(input);
}
int ci;
for(ci=1; ci<1024; ci++)
{ fseek(input,ci,SEEK_SET);
colC[ci]=fgetc(input);
}

//Take difference between value of Column A and C to check if they are identical.

int j;
char A,B;
for (j = 1; j < 1024; j++)
{
int check = colA[j] - colC[j]; // check difference between two values in a column
if (check > 0)
deletemove(A,j); //delete values from column C and D
else if (check < 0)
deletemove(B,j); // delete values from column A and B
}


fclose(input); // close files

}

我需要帮助实现删除行/列函数并读取数组中的值。

另外,在数组中存储 200,000 个值是可行的方法吗?

谢谢。

最佳答案

is storing 200,000 values in an array a feasible method?

是的,只要您不将这些数组放入堆栈即可。在函数内声明变量会将变量放入堆栈(1)。现代(2016 年)桌面通常将堆栈大小限制为几兆字节,而主内存为几千兆字节。

所以最好将大数组放入主存中。这可以通过多种方式完成:

  • 使用全局数组,即在任何函数外部声明数组
  • 使用静态数组,即使用 static 关键字声明数组
  • 使用动态分配的数组,即使用malloc来分配数组

(您也可以使用链表。链表的优点是它可以根据需要增长;您不需要提前知道空间要求。)

在您的情况下,我会将比率存储在数组中由红色/绿色值给出的索引处。假设比率始终为正数,我将使用 -1.0 初始化数组中的所有条目。然后,当您读取文件时,将比率存储在两个数组中的正确位置。例如,当您阅读该行

6   0.156   5   0.146

0.156 存储在红色数组中的索引 6 处,并将 0.146 存储在绿色数组中的索引 5 处大批。

从文件中读取所有值后,您可以简单地扫描两个数组,并打印两个数组都具有非负值的值。

(1) 忽略没有正常堆栈的奇怪系统(例如小型嵌入式系统)。

关于c - 在 C 中对齐数据集列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39397567/

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