-1

I have this code in calculating euclidean distance. But, I don't know why I'm having this error. array1 and array2 undeclared .

double dist(double x[4] array1, double y[4] array2)
{
    double Sum;
    double distance;

    for(int i=0;i<array1.length;i++)
    {
        cout<<"Enter value of first coordinate";
        cin >> array1[i];
        cout<<"Enter value of second coordinate";
        cin >> array2[i];

        Sum = Sum + pow((array1[i]-array2[i]),2.0);
        distance = sqrt(Sum);
    }
        cout << "DISTANCE: " << distance;
    return distance;
}

(The program can also be written in Java)

pingboo23
  • 137
  • 1
  • 3
  • 15
  • 3
    this setup is so weird. You are trying to find the distance between arrays? An array is a set of values. You are declaring them wrong in the function, it should be `double dist(double array1[4], double array2[4])` – BWG Jan 15 '14 at 00:26
  • They are not passed as arrays but converted to a pointer when passed to the function. So you can't ask for the length in your for loop. I also do not understand why you use arrays if you compute the sum directly? You only need the values once, so store them in a local variable (not an array). – leemes Jan 15 '14 at 00:27
  • @BWG: If the arrays represent mathematical vectors that are points in N-space, then computing the [Euclidean distance](http://en.wikipedia.org/wiki/Euclidean_distance) between them makes sense. – Keith Thompson Jan 15 '14 at 01:55
  • @KeithThompson Yeah, I see. I guess in this case it would be 4-D space? But it could have been worded "calculate distance between points". An array is a memory structure of sorts, and it doesn't seem to make sense to find the distance between two memory structures (other than their position in memory, but there is literally no reason to do that) – BWG Jan 15 '14 at 02:06
  • @BWG: Personally, I found the phrase "Euclidean distance" clear enough, but YMMV. – Keith Thompson Jan 15 '14 at 03:07

1 Answers1

1

You didn't declare your function properly. The arguments do not follow c++ syntax. Try

double dist(double array1[4], double array2[4])

if you know in advance that you will only pass arrays of size 4. If the value can change, but is known at compile time, you could use a function template

template <size_t Size>
double dist(double (array1&)[Size], double (array2&)[Size])

Or, if the value might be determined at runtime, and you are able to use the STL, just use std::vectors.

Note: untested code written on a tablet.

Edit: Almost forgot to mention that, unlike in java if I remember correctly, the C++ primitive array-type (see also Keith's comments below and the link to an excellent explanation on this subject) does not have methods (member functions) associated with it. Statements like array1.size() don't make any sense on primitive types. Of course you don't need them if the size is hardcoded anyway. Otherwise, use std::vector.

JorenHeit
  • 3,877
  • 2
  • 22
  • 28
  • You should use double, not void, the function returns something. – N Alex Jan 15 '14 at 00:38
  • No, C++ (and C) arrays are *not* pointers, though an expression of array type "decays" to a pointer in most contexts. Section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/) explains this very well. – Keith Thompson Jan 15 '14 at 00:52
  • @Keith That's why I said "kind-of" ;-) I didn't want to go into detail on that subject, but thanks for the link in case the OP wants to know more. – JorenHeit Jan 15 '14 at 01:00
  • Ok -- but arrays are not "kind-of" pointers at all; they're two very different things (that interact in confusing ways). – Keith Thompson Jan 15 '14 at 01:01
  • @KeithThompson Fair enough. I agree that everything should be accurste on here, so I rephrased. – JorenHeit Jan 15 '14 at 01:08
  • I hate to be picky (actually, that's not true), but "can in many cases be regarded as a pointer" is still inaccurate. Arrays far too often *are* regarded as pointers, but they're really *really* **really** not. – Keith Thompson Jan 15 '14 at 01:13
  • @KeithThompson Haha you must be a purist. Especially for beginners, which the OP clearly is judging by this code (all due respect), there is no point in making the distinction. Arrays and pointers can often be treated the same, can't they? Anyway, how would you put it? Or should I leave the pointer comparison out alltogether? (Anything for the perfect answer...) – JorenHeit Jan 15 '14 at 01:21
  • @JorenHeit: *Especially* for beginners, it's important to understand that arrays and pointers are two different things. If you assume they're the same thing, it's way too easy to write code that *almost* works but has subtle errors that are impossible to understand. Say you have a function declared as `void foo(int arr[]) { /* ... */ }`; why does `sizeof arr` not give you the size of the array? Conversely, why *does* `sizeof arr` give you the size of the array (not of a pointer) if `arr` is declared as a variable, not as a parameter? – Keith Thompson Jan 15 '14 at 01:26
  • Mind you, I do know the difference. But maybe you're right and I should remove the remark altogether. Not that my answer was meant to, or will serve any purpose in teaching people about the differences between arrays and pointers, but I agree that all information should be accurate. – JorenHeit Jan 15 '14 at 01:32