简体   繁体   中英

I need 10 result with loop but I can't make it I can only get 4 result how I can fix my loop

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;
}

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));
}

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