两个数组用C相比,由元元 [英] Comparing two arrays in C, element by element

查看:86
本文介绍了两个数组用C相比,由元元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开裂我的头在为了使我的节目(不是我写的)我们计算物理项目更加动态的一个实现的东西用C很简单:
 如果有条件在比较由元素两个不同的数组元素。

I have been cracking my head at achieving something very simple in C in order to make my one of the programs (not written by me) in our computational physics project more dynamic: comparing two different arrays element by element in an if conditional.

#include <math.h>
#include <stdio.h>
#include "header.h"
const   int nParam = 10;
double  a[nParam], a_tmp[nParam];
double values[10000];

double FitParam(double x){
    int xindex;
    double value;

    xindex=(int) x;

    if (a_tmp[1]==a[1] && a_tmp[2]==a[2] && a_tmp[3]==a[3] && a_tmp[4]==a[4]){ 
        value=values[xindex];
        return(value);
    }

// code continues... (very long subroutine and there is recursion for
// the subroutine so this if statement above is very important).

该数组a []的每一个我们运行程序时不同数量显著的元素;例如,现在,我们使用这个子程序仅元素[1]到[4]。然而,在其他情况下,我们会希望有更少或更多的元件,比如,多达3个元素或至多5个元素,分别

The array a[ ] has a varying number of significant elements every time we run our program; for example, right now, we are using this subroutine for only elements [1] through [4]. However, in other cases, we will want to have fewer or more elements, say, up to 3 elements or up to 5 elements, respectively.

所以基本上,我希望能够为改写if语句上面,以便它是动态的......换句话说,如果有N个元素考虑,那么它会做

So essentially, I want to be able to rewrite the if statement above so that it is dynamic... in other words, if there are N elements considered, then it will do:

如果(a_tmp [1] == A [1]安培;&安培; ...&放大器;&安培; a_tmp [N] ==一个[N]){}

所以这个如果有条件应因人而异,只要我们感兴趣的元素个数N改变(N定义为在该文件中的头一个的#define,这是我刚刚入选header.h)。

So this if conditional should vary whenever our number N of elements of interest is changed (N is defined as a #define in the header of this file, which I just named header.h).

我将在这个任务大大AP preciate您的支持。谢谢你。

I would greatly appreciate your support on this task. Thank you.

推荐答案

您最好的选择就是重写其作为一个返回真或假的函数(1或0):

Your best bet is to rewrite it as a function that returns true or false (1 or 0):

int compareArrays(double a[], double b[], int n) {
  int ii;
  for(ii = 1; ii <= n; ii++) {
    if (a[ii] != b[ii]) return 0;
    // better:
    // if(fabs(a[ii]-b[ii]) < 1e-10 * (fabs(a[ii]) + fabs(b[ii]))) {
    // with the appropriate tolerance
  }
  return 1;
}

请注意,它通常是不好的做法比较双打平等 - 你最好比较它们的差异,并确保绝对值小于宽容一些。

Note that it is usually bad practice to compare doubles for equality - you are better off comparing their difference, and making sure the absolute value is less than some tolerance.

另外请注意你到n元素比较1 - C数组从0开始,虽然

Also note you are comparing elements 1 through n - C arrays start at 0 though.

您将使用以上

if (compareArrays(a, a_tmp, N)) {

其中的值 N 的#define 根据您的问题,D。

如果你想成为聪明,避免循环,你可以写出如下。它的还是的一个坏主意,双打比较平等,但我会离开,对于其他时间(见上文中的code注释的解决方案)。

If you want to be "clever" and avoid a loop, you can write the following - it will stop ("short-circuiting") as soon as you reach the right number of comparisons. It is still a Bad Idea to compare doubles for equality but I will leave that for another time (see comment in code above for a solution).

if(a[1]==a_temp[1] && (2 > N || (a[2]==a_temp[2] && (3 > N || (a[3]==a_temp[3]))))) {

这使得剩下的真正只要你有比较而言正确的号码 - 因此它会停止评估方面(如需要)。我不相信这是不是更快,更好或code - 但它的的动态......你可以明显地使这个前pression只要你想;我刚写的前三个条件,所以你的想法。我不建议这样做。

This makes the "and the rest" true as soon as you have compared the right number of terms - so it will stop evaluating terms (as you need). I am not convinced this is either faster, or better code - but it is "dynamic"... You can obviously make this expression as long as you would like; I just wrote the first three terms so you get the idea. I DO NOT RECOMMEND IT.

至于双打的比较,你可能会考虑替换

As for the comparison of doubles, you might consider replacing

if(a == b)

if(closeEnough(a, b))

在这里你定义宏

#define closeEnough(a, b) (fabs((a)-(b)) < 1e-10 * (fabs(a) + fabs(b)))? 1 : 0

这将确保您的双打不必是正好等于 - 这取决于你如何到达他们,他们几乎不可能出现,并且在10 ^ 1部分的相对误差通常是足够了最实用的比较。

This will make sure that your doubles don't have to be "exactly equal" - depending on how you arrived at them, they will almost never be, and the relative tolerance of 1 part in 10^10 is usually plenty for most practical comparisons.

这篇关于两个数组用C相比,由元元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆