2017-04-11 123 views
0

我有一個50個指針的數組,指向包含x和y中心座標以及圓的半徑的圓形結構。我分配了所有內存,並使用rand_float爲圓圈創建隨機x,y和z。我的計劃的關鍵是找到面積最大的圓圈並將其打印出來。我遇到了我的程序問題,我知道隨着rand結果不會每次都是一樣的,但我的價值是沒有接近預期的輸出。我也沒有在輸出中看到來自largestcircleprintf輸出。最後,當我嘗試使用free(circleptr運行程序時,我收到一個錯誤)。指向圓的指針的數組struct

#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. 

的思考?

+0

你不要叫'largestcircle'。那麼你如何看待printf? – Arash

+1

'max = circleptr [i] - > z * 2 * PI;'這不是一個圓的面積公式。應該是'max = circleptr [i] - > z * circleptr [i] - > z * PI;'。並建議你使用比'z'更好的變量名稱 - 'radius'會更有意義。 – kaylum

+1

'免費(circleptr)'當然你不能那樣做。你在哪裏有'circleptr = malloc()'?無處。所以如果你沒有分配這個指針,你就不能釋放它。用'free(circleptr [i])'嘗試循環。 – kaylum

回答

1

當您看到circle-> x,y和z的某些垃圾隨機值時,您的格式說明符正在打印circle->x,circle->ycircle->z。由於您使用的是double而不是int,因此請使用%f而不是%d

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); 
+1

爲了在語法上正確,應該是'「%lf%lf%lf \ n「'(對於double而不是'float')。 –

+1

@ DavidC.Rankin--'scanf()'格式字符串中的'%lf',而''double'參數的'printf()'格式字符串中的'%f'。這是合法的,但'l'修飾符對'%f'沒有影響。 –

0

這是你的問題,你正在使用%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而不是i而空閒內存

+1

'%llf'不是'printf()'格式字符串的有效轉換說明符。 'll'修飾符用於'long long'整數類型。 –

0

那裏是這個函數的一個問題:

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()語句比較了餅圖* r * r的圓的區域。 但是在if()中,max被設置爲2 * pie * r的圓的圓周。

這會給你錯誤的答案。