Calculate distance between two points in C ++ using struct

-2

I am having trouble printing the result, but before that it is not done because it is a new type created by struct it does not calculate since it asks for type double

follow 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;
}
    
asked by anonymous 18.03.2015 / 14:15

1 answer

2

I do not understand what the problem is being solved and after several attempts I did not get better information. So I will solve the problems that are clearly demonstrated in the question. Surely this code does not show the desired result but at least it is easier to read and understand what it is doing, it compiles and it has the right types. Surely it needs to be modified to solve a real problem, but what it came to 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);
    return 0;
}

See running on ideone .

    
18.03.2015 / 15:07