- #1
mathmari
Gold Member
MHB
- 5,049
- 7
Hey!
I have to write a function for the destruction of the planetary system "solid".
With the destruction of the planetary system, the asteroids where the gap between this one and the object, is at least "gap" will also be destructed. (so, they have to be deleted) The asteroids for which the gap is greater are converted to free-floating planets consisting a new collection of free-floating planets (ffplan_t). This new collection should be added to the array of the free-floating planets of the star system to which the planetary system, that is destructed, belonged. The identifier "fp" of the new collection is the identifier of the planetary system that is destructed. The list of the free-floating planets that corresponds to the new collection should be sorted as for the field "as" of the asteroids that are contained. The destructed planetary system should be deleted from the list of planetary systems of the star system to which it belonged.
The function should print the following:
Planetary system: < solid >_1 , < solid >_2 , ... , < solid >_n
Free-floating planetsA: < fp > _1 , < fp >_2 , ... , < fp >_n
Free-floating planets: < FFP >_1 , < FFP >_2 , ... , < FFP >_n
< solid >_i is the identifier of the ith planetary system of the Solar System to which the deleted planetary system belonged.
< fp >_i is the identifier of the ith collection of the free-floating planets in the array of the free-floating planets of the Solar System to which the deleted planetary system belonged.
< FFP >_i is the identifier of the ith free-floating planet in the list of the free-floating planets in the new collection of the free-floating planets of the Solar System to which the deleted planetary system belonged. I have done the following:
When I run this function, I get a segmentation fault.
What mistake could I have done?? (Wondering)
I have to write a function for the destruction of the planetary system "solid".
With the destruction of the planetary system, the asteroids where the gap between this one and the object, is at least "gap" will also be destructed. (so, they have to be deleted) The asteroids for which the gap is greater are converted to free-floating planets consisting a new collection of free-floating planets (ffplan_t). This new collection should be added to the array of the free-floating planets of the star system to which the planetary system, that is destructed, belonged. The identifier "fp" of the new collection is the identifier of the planetary system that is destructed. The list of the free-floating planets that corresponds to the new collection should be sorted as for the field "as" of the asteroids that are contained. The destructed planetary system should be deleted from the list of planetary systems of the star system to which it belonged.
The function should print the following:
Planetary system: < solid >_1 , < solid >_2 , ... , < solid >_n
Free-floating planetsA: < fp > _1 , < fp >_2 , ... , < fp >_n
Free-floating planets: < FFP >_1 , < FFP >_2 , ... , < FFP >_n
< solid >_i is the identifier of the ith planetary system of the Solar System to which the deleted planetary system belonged.
< fp >_i is the identifier of the ith collection of the free-floating planets in the array of the free-floating planets of the Solar System to which the deleted planetary system belonged.
< FFP >_i is the identifier of the ith free-floating planet in the list of the free-floating planets in the new collection of the free-floating planets of the Solar System to which the deleted planetary system belonged. I have done the following:
Code:
int ffplanpos=0;
int destruction(int solid, int gap){
int i, j;
int sum=0;
plansys_t *p=StarS[0].plasy;
for(i=0; i<Sfreep; i++){
p=StarS[i].plasy;
while (p != NULL && p->solid != solid){
p=p->next;
j=i;
}
}
if(p == NULL){
printf("The planetary system with identifier %d couldn't be found\n", solid);
exit;
}
asteroid_t *f=p->asteroids;
while(sum<gap){
sum=sum+f->gap;
f=f->next;
DELETE(f->prev, f->prev->as);
}
StarS[j].ffplan[ffplanpos].fp=solid;
ffplanpos++;
asteroid_t *planf = calloc(1, sizeof(asteroid_t));
planf->as=f->as;
planf->gap=0;
planf->next=NULL;
planf->prev=NULL;
asteroid_t *K=StarS[j].ffplan[ffplanpos].ff;
f=f->next;
while(f != NULL){
if (K == NULL) {
K = planf;
} else {
asteroid_t *last = K;
while (last->next != NULL) {
last = last->next;
}
last->next = planf;
}
f=f->next;
}
printf("\n\nPlanet Systems = ");
while(StarS[j].plasy != NULL){
printf(" %d ", StarS[j].plasy->solid);
StarS[j].plasy=StarS[j].plasy->next;
}
printf("\n\nFree-floatingM = ");
for(i=0; i<max; i++){
printf(" %d ", StarS[j].ffplan[i].fp);
}
printf("\n\nFree-floating planets = ");
for(i=0; i<max; i++){
while(StarS[j].ffplan[i].ff != NULL){
printf(" %d ", StarS[j].ffplan[i].ff->as);
StarS[j].ffplan[i].ff=StarS[j].ffplan[i].ff->next;
}
}
return 0;
}
When I run this function, I get a segmentation fault.
What mistake could I have done?? (Wondering)