0
I need to print the values read on the keyboard of a struct vector that form a coordinate. The problem is that when I pass the struct vector of the function that takes and stores the data to function that will print them on the screen, what comes out are coordinates (0.0) regardless of what is typed. I’ve tried going by reference and value and it didn’t work both ways.
It’s probably some kind of mistake in passing, but I can’t find the right way to do it anywhere...
struct armazenar
{
float x;
float y;
};
int func_lerCoordenada(int n)
{
struct armazenar p[MAX];
int i;
for(i=0;i<n;i++)
{
puts("Insira a coordenada x:");
p[i].x=lerfloat();//lerfloat só valida enrada pra float
puts("Insira a coordenada y:");
p[i].y=lerfloat();
printf("%f,%f\n",p[i].x,p[i].y);//só verificando que os valores lidos estão sendo armazenados corretamente, portanto o problema não é aqui.
}
return 0;
}
float imprimePonto(struct armazenar p[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("(%9.2f, %9.2f)\n",p[i].x,p[i].y);
}
return 0;
}
float comparacao(struct armazenar p[],int n)
{
int i, pac=0,pab=0,pes=0,pdi=0;
float dir=0,esq,acima=0,abaixo=0;
for(i=0;i<n;i++)
{
if(p[i].y>acima)
{
acima=p[i].y;
pac=i;//para registrar a posicao em que a condicao era verdadeira
}
else
if(p[i].y<abaixo)
{
abaixo=p[i].y;
pab=i;
}
if(p[i].x>dir)
{
dir=p[i].x;
pdi=i;
}
else
if(p[i].x<esq)
{
esq=p[i].x;
pes=i;
}
}
printf("O ponto mais acima eh: (%9.2f,%9.2f).\n\n",p[pac].x,p[pac].y);
printf("O ponto mais abaixo eh: (%9.2f,%9.2f).\n\n",p[pab].x,p[pab].y);
printf("O ponto mais a esquerda eh: (%9.2f,%9.2f).\n\n",p[pes].x,p[pes].y);
printf("O ponto mais a direita eh: (%9.2f,%9.2f).\n\n",p[pdi].x,p[pdi].y);
}
int main()
{
struct armazenar p[MAX];
int n=0;
n=func_lerN();//recebendo o numero de vetores que o usuario quer digitar.
func_lerCoordenada(n);
imprimePonto(&p[MAX],n);
comparacao(&p[MAX],n);
return 0;
}
Unfortunately this answer will be useless for me because I haven’t learned it yet and I don’t understand what has to be done, but thank you.
– soAna
You have to pass the struct in the reading function.
– leonArdo
or makes the function point to the one of the parameter.
– leonArdo