简体   繁体   English

我需要 10 个带循环的结果,但我做不到我只能得到 4 个结果我如何修复我的循环

[英]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.你的错误是你只计算了nokt数组中一个点和下一个点之间的距离。 For example, you do not find the distance of nokt[0] and nokt[2] .例如,您找不到nokt[0]nokt[2]的距离。 So you need one more for loop inside the for loop you've already used.因此,您需要在已经使用过的for loop再添加一个for loop 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 .此外,您可能希望为您的特定目标构建函数,例如查找距离等。最后,如果指数是一个小的自然数,那么最好使用您自己的幂函数而不是math.h pow(double, double)函数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) .原因是 C 中没有 pow(double,int) 函数,因此在您的情况下,您可以使用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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM