简体   繁体   中英

getting variables to call for a function c++

So basically im trying to do a program that formulates distance between two points in a 3-d world. I'm trying to call on a function to do the formula called distance. But my problem was trying to get the input for all 6 variables. Could of done it one by one but that makes the code large and a bit to much. So instead tried to use an array to take in values but its coming up with errors like 'no matching function for call to 'distance float[6]'

Simply put im trying to take in 6 variables using an array then send them to the function to find the distance between them

#include <iostream>
#include <cctype>
#include <stdio.h>
#include <math.h>

using namespace std;

float distance(float ux, float uy, float  uz,
                   float vx, float vy, float vz);
int main()
{
    float number = 0.0f;
    float loopArray[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
    cout << "Enter x,y,z of first coordinates to find distance between x,y,z second cordinates" << endl;

    for (int i = 0; i < 6; i++)
    {
        cin >> number;
        loopArray[i] = number;
    }

    distance(loopArray);

}


float distance(float ux, float uy, float uz,
                   float vx, float vy, float vz);
{
    sqrt (((ux - vx) * 2) + ((uy - vy) * 2) + ((uz - vz) * 2)) = float dist;

    return dist;
}

Your function is declared to take 6 float variables as arguments. If you need to call it with an array add an overload that takes an array as argument. Something of the sort of:

float distance(float a[]) {
  return distance(a[0], a[1], a[2], a[3], a[4], a[5]);
}

This function simply calls the other version that has six arguments. A better options is to modify the original function to take an array as argument.

Ivalyo has already explained the overload, however I'd like to note that, if you want to compute the Euclidean distance between two points, your distance function is incorrect. The * operator is multiplication, not exponentiation; the correct expression might look something like

return sqrt(((ux - vx) * (ux - vx)) + ((uy - vy) * (uy - vy)) + ((uz - vz) * (uz - vz)));

I've also skipped the temporary variable, since it's not necessary and I'm not even sure the "post-assignment" is valid C++.

There are a couple of issues with your code:

  • You said distance would take six double s, but gave it a whole array of them. Either change its signature or pass them one by one.

  • The syntax

    sqrt (((ux - vx) * 2) + ((uy - vy) * 2) + ((uz - vz) * 2)) = float dist;

    makes no sense. You meant

     float dist = sqrt (((ux - vx) * 2) + ((uy - vy) * 2) + ((uz - vz) * 2)); 

    although that's not technically correct: you need to take the square of these terms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM