繁体   English   中英

指向圆形结构的指针数组

[英]Array of pointers to circle struct

我有一个由50个指针组成的数组,这些数组指向一个圆形结构,该结构包含x和y中心坐标以及一个圆的半径。 我分配了所有内存,并使用rand_floatrand_float创建了随机的x,y和z。 我程序的重点是找到面积最大的圆并将其打印出来。 我的程序遇到问题,我知道每次使用rand的结果都不会相同,但是我的值与预期输出不符。 我也没有在输出中看到largestcircleprintf输出。 最后,当我尝试使用free(circleptr )运行程序时收到错误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

我当前的xy和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.

有什么想法吗?

当您打印circle->xcircle->ycircle->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);

这是您的问题,您正在使用%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);

你也有一些missclick在这里I而不是Ii空闲内存

此功能有问题:

 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的圆的周长。

这将给您错误的答案。

暂无
暂无

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

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