/ / Tablica wskaźników do struktury okręgu - c, tablice, wskaźniki, random, struct

Tablica wskaźników do kółek struct - c, tablice, wskaźniki, losowe, struct

Mam tablicę 50 wskaźników, które wskazują na strukturę okręgu, która zawiera współrzędne środka xiy oraz promień okręgu. Przydzieliłem całą pamięć i wykorzystałem rand_float aby utworzyć losowe x, y i z dla okręgów.Celem mojego programu jest znalezienie koła o największej powierzchni i wydrukowanie go. Mam problemy z moim programem, rozumiem, że z randami wyniki nie będą za każdym razem takie same, ale moje wartości nie są w pobliżu zamierzonego wyniku. Nie widzę też pliku printf wyjście z largestcircle na wyjściu. Na koniec pojawia się błąd, gdy próbuję uruchomić program z 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 %fn", 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;
}

wynik powinien wyglądać mniej więcej tak:

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

Moje aktualne wartości x y i z wyglądają następująco:

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.

Myśli?

Odpowiedzi:

1 dla odpowiedzi № 1

Problem polegający na tym, że widzisz jakieś śmieciowe losowe wartości dla circle-> x, yiz jest związany z twoim specyfikatorem formatu, podczas drukowania circle->x, circle->y i circle->z. Posługiwać się %f zamiast %d odkąd używasz double i nie int.

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

Zmień powyżej na

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

0 dla odpowiedzi nr 2

Oto twój problem, którego używasz %d, posługiwać się %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 %llfn", max, x, y, radius);

też masz trochę missclick tutaj I zamiast i gdy wolna pamięć


0 dla odpowiedzi № 3

Wystąpił problem z tą funkcją:

 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 %fn", max,x,y,radius);
}

Instrukcja if () porównana dla obszaru koła, które jest plackiem * r * r. Ale wewnątrz if (), max jest ustawiane na obwód koła, który jest 2 * pie * r.

To da ci złą odpowiedź.