struct nokta
{
double x,y;
} nokt[5] = {{5,-2},{-3,-2},{-2,5},{1,6},{0,2}};
struct daire
{
int c;
double R;
};
int main()
{
int i,j;
double distance[30];
for(i=0; i<4; i++)
{
distance[i]=sqrt((pow(nokt[i].x+nokt[i+1].x,2)+pow(nokt[i].y nokt[i+1].y,2)))
}
for(i=0;i<4;i++)
{
printf("_%lf",distance[i]);
}
return 0;
}
-
1Welcome to SO. Please take some time to propperly format your code when you post it. If code looks like a complete mess, no one will read it. Also please post code that actually compiles. There are syntax errors in it. If you are able to compile and run your code, then it is not the code you show us here. – Gerhardh Oct 31 '20 at 12:39
1 Answers
As far as I understand your problem, you try to find distance between all points. Right? Your mistake is that you only calculate the distance between a point and next point in your nokt
array. For example, you do not find the distance of nokt[0]
and nokt[2]
. So you need one more for loop
inside the for loop
you've already used. Further, you may want to build functions for your specific aim such as finding distance etc. Finally, if exponent is a small natural number, it would be better to use your own power function rather than pow(double, double)
function in math.h
. The reason is that there is no pow(double,int) function in C, so in your case you make more complex calculation by using pow(double,double)
. See this discussion.
I hope this code will solve your problem. Otherwise, let me know.
#include <stdio.h>
#include <math.h>
struct nokta
{
double x,y;
} nokt[5] = {{5,-2},{-3,-2},{-2,5},{1,6},{0,2}};
double mySquareFunc(double x);
double findDistance(struct nokta p1, struct nokta p2);
int main(){
int i,j,indX = 0;
double distance[10];
for(i=0; i<5; i++){
for(j=i+1;j<5; j++){
distance[indX] = findDistance(nokt[i],nokt[j]);
indX++;
}
}
for(i=0;i<10;i++)
{
printf("_%lf",distance[i]);
}
return 0;
}
double mySquareFunc(double x){
return x*x;
}
double findDistance(struct nokta p1, struct nokta p2){
return sqrt(mySquareFunc(p1.x-p2.x)+mySquareFunc(p1.y-p2.y));
}

- 799
- 1
- 9
- 23
-
this helped me a lot didn't answer that year but stackoverflow don't let you say t4nk you :D – Burak Akı Oct 18 '21 at 12:00