试图找到两个数组之间的最大差异

时间:2017-01-21 15:49:11

标签: c

编辑:显然变量dmax不会随每个循环更新。

我有2个文件被扫描并输入到2个独立的数组中但是当我运行代码以找到2个数组中相同元素之间的每日最大差异时,输出达到107374208.000000。以下是我的代码。

void diff()
{
float ftemp[size], mtemp[size], diff[size], count = 1.0;

feb = fopen("feb.txt", "r");
mar = fopen("mar.txt", "r");

for(i = 1; i < size; i++)
{
    fscanf(feb, "%f", &ftemp[i]);
    fscanf(mar, "%f", &mtemp[i]);

    dmax = (i * 3) - 3;

    if((mtemp[dmax] - ftemp[dmax]) > count && (mtemp[dmax] - ftemp[dmax]) > 0)
    {
        count = mtemp[dmax] - ftemp[dmax];
    }
}

printf("The highest temperature difference between March and February is %f.\n", count);
}

这是二月的每日气温

maximum     minimum    average
31.6        22.4        25.9
30.2        22.7        25.5
31.2        22.9        26.1
31.3        23.4        26.4
30.7        23.2        26.2
31.3        23.1        26.4
31.6        23.9        26.4
31.6        24.0        26.9
32.7        24.7        27.5
33.8        24.8        27.7
32.4        25.0        27.6
32.1        24.9        27.6
32.7        25.4        27.9
31.9        25.5        27.6
31.9        25.4        27.8
32.1        25.3        27.8
31.7        25.6        27.8
32.6        25.2        27.7
32.2        24.9        27.5
32.2        24.9        27.7
31.7        25.8        27.7
32.3        25.5        27.9
32.1        24.4        27.3
31.5        24.6        27.2
31.8        24.0        27.0 
32.0        24.4        27.4 
32.4        24.9        27.8
32.1        25.0        27.6

这是3月份的每日气温

maximum     minimum    average
32.7        25.1        27.7
33.8        24.8        28.0 
32.9        24.7        27.6
32.9        25.0        27.8
32.9        25.0        27.8
33.0        23.8        27.5
32.6        24.2        27.6
32.8        24.8        27.9
32.0        24.2        27.6
32.3        24.9        27.8
33.6        25.0        28.1
33.4        25.6        28.3
33.8        24.7        28.3
34.1        25.2        28.6
32.7        25.9        28.6
28.2        23.6        25.9
30.7        24.3        26.4
32.7        24.9        27.5
32.5        25.4        27.5
33.6        25.9        27.6
33.1        25.3        27.7
31.0        25.0        27.5
32.8        24.2        27.9
33.0        24.7        28.1
33.2        25.2        28.4
34.0        25.7        28.8
34.4        25.8        29.1
32.7        26.2        28.6
33.3        26.5        28.5
32.3        25.8        28.5
33.0        26.6        28.8    

1 个答案:

答案 0 :(得分:0)

代码无法编译,因为有一堆未声明的变量,例如dmaxftemp

修好后...

  1. 取消选中fopen
  2. 假设文件大小合适。
  3. 它远远超过阵列的初始化部分。
  4. fscanf未经过检查。
  5. fscanf是一个错误生成器。
  6. 让我们来看看你的循环。

    for(i = 1; i < size; i++)
    {
        fscanf(feb, "%f", &ftemp[i]);
        fscanf(mar, "%f", &mtemp[i]);
    
        dmax = (i * 3) - 3;
    
        if((mtemp[dmax] - ftemp[dmax]) > count && (mtemp[dmax] - ftemp[dmax]) > 0)
        {
            count = mtemp[dmax] - ftemp[dmax];
        }
    }
    

    在第一次迭代之后,dmax将始终大于i(i = 0,dmax = 0; i = 1,dmax = 3; i = 2,dmax = 6)。但是循环只能填充到i。因此,当您尝试使用mtemp[dmax]ftemp[dmax]时,永远不会填充fscanf(feb, "%f", &ftemp[i])for( int i = 0; i < size; i += 3 ) { fscanf(feb, "%f %f %f", &ftemp[i], &ftemp[i+1], &ftemp[i+2]); fscanf(mar, "%f %f %f", &mtemp[i], &mtemp[i+1], &mtemp[i+2]); ... } 。你会得到垃圾。

    我怀疑你假设fscanf正在扫描整排数字。它只扫描一个,就像你要求的那样。要做一整行,你必须要求它。

    size

    请注意,循环从0开始,因为数组从0开始。它前进3,因为我们一次只读3。

    这仍然无法奏效。文件的第一行是所有字符串,而不是浮点数。 ftemp[i]如果匹配不匹配,则不会跳过以找到匹配项。如果你继续尝试相同的比赛,它将继续尝试一遍又一遍地从同一位置读取。您的所有代码都在尝试将每个文件的第一个字节读取为浮点数,失败,然后再次执行mtemp[i]次。 <{1}}和scanf仍未初始化,因此您会变得垃圾。

    这就是应该避免使用fscanfsscanf的原因。相反,请阅读整行并使用#include <stdio.h> #include <string.h> #include <errno.h> #include <math.h> #include <stdlib.h> /* A little function to open files while checking it was successful and giving a good error message if not */ FILE *open_file( const char *file, const char *mode ) { FILE *fp = fopen(file, mode); if( fp == NULL ) { fprintf( stderr, "Could not open '%s' for '%s': %s\n", file, mode, strerror(errno) ); exit(1); } return fp; } /* Instead of using hard coded file names, they're passed in. */ /* Instead of hard coding the use of the diff, it's returned. */ /* I switched to doubles because that's what fabs() returns and the extra accuracy can't hurt. */ double largest_max_temp_diff(const char *file1, const char* file2) { FILE *feb = open_file(file1, "r"); FILE *mar = open_file(file2, "r"); char line1[1024]; char line2[1024]; /* Skip the header lines */ fgets(line1, 1024, feb); fgets(line2, 1024, mar); double max_diff = 0; /* Loop infinitely, loop exit is handled using `break` */ while(1) { /* Read in a line from each file. Stop when we reach the end of either file. */ if( fgets(line1, 1024, feb) == NULL ) { break; } if( fgets(line2, 1024, mar) == NULL ) { break; } /* Read in just the first column, ignore the rest. */ /* Make sure the parsing was successful. */ double max1, max2; if( sscanf(line1, "%lf %*lf %*lf", &max1) < 1 ) { fprintf( stderr, "Could not understand '%s'", line1); continue; } if( sscanf(line2, "%lf %*lf %*lf", &max2) < 1 ) { fprintf( stderr, "Could not understand '%s'", line2); continue; } /* Compare the diffs as absolute values */ double diff = max2 - max1; if(fabs(diff) > fabs(max_diff)) { max_diff = diff; } } /* Return the max diff so it can be used as the caller likes */ return max_diff; } int main() { /* Get the diff */ double diff = largest_max_temp_diff("feb.txt", "mar.txt"); /* Use it */ printf("The highest temperature difference between March and February is %f.\n", diff); } 处理它们。

    阵列也是不必要的,你没有用以前的值做任何事情,你只需要当前的最小值和最大值。

    考虑到这一切以及其他一些事情,我已经重写了这样的话:

    sscanf

    此新代码现在跳过标题行。它读取一行并单独解析它,保证它不会卡住。它使用仅存储最大值的Function SumIfColor(lcol1, lrow1, lcol2, lrow2) sum = 0 oCellRange = ThisComponent.CurrentController.ActiveSheet.getCellRangeByPosition(_ lcol1-1,lrow1-1,lcol2-1,lrow2-1) For lCol = 0 To oCellRange.Columns.Count -1 For lRow = 0 To oCellRange.Rows.Count -1 oCell = oCellRange.getCellByPosition(lCol, lRow) If oCell.CellBackColor > -1 Then sum = sum + oCell.Value End If Next Next SumIfColor = sum End Function 一次解析一行。它检查解析是否成功。

    它使用绝对值来检查最大差异,因为-4的差异大于3.

    最后,它返回调用者的值,以便用它做它想做的事情。在同一函数中计算和打印(或任何格式化)是一个红旗;它使函数变得不灵活,你最终编写了大量重复的代码。

    这不是最好的代码,但仍有很多重复,但它会更好,并会自行检查。使用文件时,您必须检查您的假设。