简体   繁体   English

指向圆形结构的指针数组

[英]Array of pointers to circle struct

I have a array of 50 pointers that point to a circle struct that contains the x and y center coordinates, and the radius for a circle. 我有一个由50个指针组成的数组,这些数组指向一个圆形结构,该结构包含x和y中心坐标以及一个圆的半径。 I allocated all of the memory and used rand_float to create random x, y, and z for the circles. 我分配了所有内存,并使用rand_floatrand_float创建了随机的x,y和z。 The point to my program is the find the circle with the largest area and print it out. 我程序的重点是找到面积最大的圆并将其打印出来。 I am experiencing problems with my program, I understand that with rand results will not be the same every time but my values are no where near the intended output. 我的程序遇到问题,我知道每次使用rand的结果都不会相同,但是我的值与预期输出不符。 I am also not seeing the printf output from largestcircle in the output. 我也没有在输出中看到largestcircleprintf输出。 Lastly I receive a error when I try running the program with free(circleptr ). 最后,当我尝试使用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;
}

the output should look something like: 输出应类似于:

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

My current xy and z values look like: 我当前的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.

Thoughts? 有什么想法吗?

The problem where you are seeing some garbage random values for circle->x, y and z is is with your format specifier, while you are printing the circle->x , circle->y and circle->z . 当您打印circle->xcircle->ycircle->z ,格式说明符是您看到circle-> x,y和z的一些垃圾随机值的问题。 Use %f instead of %d since you are using double and not an int . 使用%f而不是%d因为您使用的是double而不是int

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

Change above to 更改为

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

Here is your problem, you re using %d , use %llf 这是您的问题,您正在使用%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);

also you have a bit missclick here I instead of i while free memory 你也有一些missclick在这里I而不是Ii空闲内存

There is a problem with this function: 此功能有问题:

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

The if() statement compared for the area of a circle which is pie * r * r. if()语句比较了一个圆形区域,即pie * r * r。 But within if(), max is set to circumference of a circle which is 2 * pie * r. 但是在if()中,max设置为2 * pie * r的圆的周长。

This will give you the wrong answer. 这将给您错误的答案。

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

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