Calculate distance between two points in C++ using struct

Asked

Viewed 4,125 times

-2

I’m having a hard time printing the result, but before that it’s not realized because it’s a new type created by struct He doesn’t know because he asks double

follows the code

#include <iostream>



using namespace std;

 struct Ponto{
 float x;
 float y;
 float d;

 }; 
void LerRetangulo(Ponto ret[], int tam)
{
    for(int i = 0; i<tam;i++)
    {
        cout <<"digite coordenadas x e y  "<<i+1 <<endl;
        cin >> ret[i].x>>ret[i].y;
    }
}

void CalcDistancia(Ponto ret[], int tam ){ 
   float h
        for(int i = 0; i<tam;i++)
    {
    h = sqrt(pow ( ret[i].x,2) + pow ( ret[i].y,2)); // problema aqui !!!!  

    }

}
 /*
aqui eu estava fazendo alguns teste 
void imprimir(?,int tam){
    for (int i=0;i<tam;i++){
        cout<< "A distancia = "<<? <<endl;
    }

    */
}

int main(int argc, char *argv[]) {
 Ponto retangulo[4];
LerRetangulo(retangulo,4);
 CalcDistancia(retangulo,4);
 //imprimir(?,4); duvida aqui !!!!!

    return 0;
}
  • 1

    What is this d in the structure Ponto? Is it distance? Why would a point have a distance? Do you know that your code doesn’t even compile? It is full of error even syntax, besides being without a pattern. It may sound silly but organizing the code helps you better understand what you’re doing and find silly mistakes. I’ll see if I can answer but I need to know if dhas any real function in the structure. And what distance should be calculated? Distance between what? Has 4 points. Normally the distance between 2 points is calculated.

  • There are syntax errors, several. You have never tried to compile this program. Answer the rest I asked, there’s no way to solve the problem the way you’re doing. Your code is not solving a plausible problem. So describe the problem, what is the result you want to get. Do you want to calculate the perimeter of a polygon? Why do you need the distance within the point? And if it is the perimeter, what distance do you have to do with it?

  • 1

    @bigown is right, it makes no sense to calculate the distance to a point. The distance is between two points. Anyway, by its description the error is that the function sqrt wait for a guy double and you’re using float. Change the guy to double in the definition in the structure and test again, please.

  • 1

    It’s no use if you keep making a mistake and leaving others, you need to explain the problem you’re trying to solve. Do you want to calculate the perimeter or do you want to calculate the size of the hypotenuse? In this case you would need to ensure that it is a rectangle.

1 answer

2


I did not understand which problem is being solved and after several attempts I did not get better information. Then I will solve the problems that are clearly demonstrated in the question. Certainly this code does not present the desired result but at least it is easier to read and understand what it is doing, it compiles and is with the right types. It certainly needs to be modified to solve a real problem, but what you can understand is working. I can still improve if the problem is explained.

#include <iostream>
#include <math.h>
using namespace std;

 struct Ponto{
    float x;
    float y;
 }; 
void LerRetangulo(Ponto ret[], int tam) {
    for(int i = 0; i < tam; i++) {
        cout << "digite coordenadas x e y  do ponto " << i + 1 << endl;
        cin >> ret[i].x >> ret[i].y;
    }
}

double CalcDistancia(Ponto ret[], int tam) {
    double distancia = 0;
    for(int i = 0; i < tam; i++) {
        distancia += sqrt(pow(ret[i].x, 2) + pow(ret[i].y, 2));
    }
    return distancia;
}

void imprimir(double distancia, Ponto ret[], int tam) {
    for (int i = 0; i < tam; i++) {
        cout << "Ponto " << i << " = " << ret[i].x << ", " << ret[i].y <<endl;
    }
    cout << "Distância = " << distancia << endl;
}

int main(int argc, char *argv[]) {
    Ponto retangulo[4];
    LerRetangulo(retangulo, 4);
    imprimir(CalcDistancia(retangulo, 4), retangulo, 4);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.