在C中对齐数据集列

时间:2016-09-08 18:09:21

标签: c arrays excel alignment

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

原始档案(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

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

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个值是一种可行的方法吗?

感谢。

1 个答案:

答案 0 :(得分:1)

  

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

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

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

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

(您也可以使用链表。链表的优势在于它可以根据需要增长;您不需要提前了解空间要求。)

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

行时
6   0.156   5   0.146

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

当从文件中读取所有值后,您只需扫描两个数组,并打印两个数组都具有非负值的值。

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