【问题标题】:Array of pointers to circle struct指向圆形结构的指针数组
【发布时间】:2017-09-06 07:02:13
【问题描述】:

我有一个包含 50 个指针的数组,它们指向一个包含 x 和 y 中心坐标以及圆半径的圆结构。我分配了所有内存并使用rand_float 为圆圈创建随机x、y 和z。我的程序的要点是找到面积最大的圆圈并将其打印出来。我的程序遇到问题,我知道每次使用 rand 的结果都不相同,但我的值与预期输出相差甚远。我也没有在输出中看到来自largestcircleprintf 输出。最后,当我尝试使用free(circleptr 运行程序时收到错误消息。

#include <stdio.h>
#include <stdlib.h>
#define PI 3.14
double rand_float(double a,double b){        //function provided my teacher.
    return ((double)rand()/RAND_MAX)*(b-a)+a;  
}
struct circle{
    double x;
    double y;
    double z;
};
 void largestcircle(struct circle **circleptr){
    float max = 0, radius =0, x= 0, y=0;
    int i;
    for(i = 0; i<50; i++){
        if(circleptr[i]->z *circleptr[i] ->z *PI > max){
            max = circleptr[i]->z*2*PI;
            radius = circleptr[i]->z;
            x = circleptr[i] ->x;
            y = circleptr[i] ->y;
        }
    }
    printf("Circle with largest area (%f) has center (%f, %f) and radius %f\n", max,x,y,radius);
}
int main(void) {
    struct circle *circleptr[50];
                             //dynamically allocate memory to store a circle
    int i;
    for(i=0; i<50; i++){
        circleptr[i] = (struct circle*)malloc(sizeof(struct circle));
    }
                             //randomly generate circles
    for(i=0; i<50; i++){//x
        circleptr[i]->x = rand_float(100, 900);
        circleptr[i]->y = rand_float(100, 900);
        circleptr[i]->z = rand_float(0, 100);
        //printf("%11f %11f %11f \n", circleptr[i] ->x, circleptr[i]->y, circleptr[i]->z);
    }
    largestcircle(circleptr);
    for(i=0; i<50; i++){
        free(circleptr[i]);
    }
    return 0;
}

输出应该类似于:

Circle with largest area (31380.837301) has center (774.922941,897.436445) and radius 99.969481

我当前的 x y 和 z 值如下所示:

1885193628 -622124880 -622124884 
1885193628 -622124868 -622124872 
1885193628 -622124856 -622124860 
1885193628 -622124844 -622124848 
1885193628 -622124832 -622124836 
1885193628 -622124820 -622124824 
1885193628 -622124808 -622124812 
1885193628 -622124796 -622124800 
1885193628 -622124784 -622124788 
1885193628 -622124772 -622124776......etc.

想法?

【问题讨论】:

  • 你不要打电话给largestcircle。那么您希望如何看到 printf 呢?
  • max = circleptr[i]-&gt;z*2*PI; 这不是圆的面积公式。应该是max = circleptr[i]-&gt;z*circleptr[i]-&gt;z*PI;。并建议您使用比 z 更好的变量名称 - radius 会更有意义。
  • free(circleptr) 当然你不能那样做。你在哪里有circleptr = malloc()?无处。所以如果你没有分配那个指针,你就不能释放它。尝试使用 free(circleptr[i]) 循环。
  • 我什至看不到需要计算圆的面积,半径最大的也有最大面积
  • 请注意,圆的面积与半径的平方成正比,但您只需要比较半径即可,因为半径越大,面积越大,与圆的位置无关飞机。很好奇您使用z 而不是r 作为半径;后者让您看起来像是在玩 3D 坐标。

标签: c arrays pointers random struct


【解决方案1】:

当您打印circle-&gt;xcircle-&gt;ycircle-&gt;z 时,您看到一些用于circle->x、y 和z 的垃圾随机值的问题在于您的格式说明符。使用%f 而不是%d,因为您使用的是double 而不是int

printf("%d %d %d \n", circleptr[i]->x, circleptr[i]->y, circleptr[i]->z);

上面改为

printf("%f %f %f \n", circleptr[i]->x, circleptr[i]->y, circleptr[i]->z);

【讨论】:

  • 为了语法正确,应该是"%lf %lf %lf \n"(对于double而不是float)。
  • @DavidC.Rankin-- %lfscanf() 格式字符串中,但%fprintf() 格式字符串中用于double 参数。这是合法的,但l 修饰符对%f 没有影响。
【解决方案2】:

这是你的问题,你使用%d,使用%llf

printf("%llf %llf %llf \n", circleptr[i] ->x, circleptr[i]->y, circleptr[i]->z);
printf("Circle with largest area (%llf) has center (%llf, %llf) and radius %llf\n", max, x, y, radius);

在释放内存时,您也有一点错误点击此处I 而不是i

【讨论】:

  • %llf 不是printf() 格式字符串的有效转换说明符。 ll 修饰符适用于 long long 整数类型。
【解决方案3】:

这个函数有问题:

 void largestcircle(struct circle **circleptr){
     float max = 0, radius =0, x= 0, y=0;
     int i;

     for(i = 0; i<50; i++){
         if(circleptr[i]->z *circleptr[i] ->z *PI > max){
             max = circleptr[i]->z*2*PI;
             radius = circleptr[i]->z;
             x = circleptr[i] ->x;
             y = circleptr[i] ->y;
         }
      }
      printf("Circle with largest area (%f) has center (%f, %f) and radius %f\n", max,x,y,radius);
  }

if() 语句比较圆的面积,即 pie * r * r。 但是在 if() 中,max 设置为 2 * pie * r 的圆的周长。

这会给你错误的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-03
    • 2012-12-07
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多