What is the purpose of using a helper variable when inserting an element in an array?

  • MHB
  • Thread starter mathmari
  • Start date
  • Tags
    Array
In Summary, We had defined StarS[N] to be an array of lists.)In summary, the programmer wants to write a function for the constitution of a star system. They are wondering how they can insert an element in an array in time complexity O(1). They also want to know what variables they need to use to achieve this. They have done some research and found that setting an element in an "normal" array at a certain index to a value is an O(1) operation, and inserting an element in a linked list (if you already know the location) is also an O(1) operation. They want to write a program for the constitution of a star system "ss". They have also defined
  • #36
mathmari said:
So, we don't have to add the new object "solid" at the field plansystem->solid , right?? Where do we have to add it then?? (Wondering) I got stuck right now...

I don't understand your question. :confused:

You can assign a value to plansystem->solid, and it is initially set to 0, meaning false.
 
Technology news on Phys.org
  • #37
I like Serena said:
You can assign value to plansystem->solid, and it is initially set to 0, meaning false.

But why can we not assign the value solid to plansystem->solid
(plansystem->solid=solid; ) ?? (Wondering)

After that I have to write a function for the constitution of a new object and therefore the constitution of a new planetary system (plansys_t) with identifier of the object "solid" at the star system with identifier "ss". The new planetary system contains an empty list of asteroids. The new element, that corresponds to the new planetary system, should be added to the list of the planetary systems of the star system with identifier "ss".

With the following function:

Code:
int Beginning(int solid, int ss){
	        int i, k;
                plansys_t *plansystem = calloc(1, sizeof(plansys_t));
                plansystem->asteroids=NULL;
		for(i=0; i<nrStarS; i++){
			if(StarS[i].ss=ss){
				k=i;
			}
		}
		StarS[k].plasy=plansystem;
		return k;
}

the new planetary system contains an empty list of asteroids and the new plansystem is added to the array StarS at the position where the star system has the identifier "ss", right?? (Wondering) But I haven't used the identifier of the new object "solid"...
 
  • #38
mathmari said:
But why can we not assign the value solid to plansystem->solid
(plansystem->solid=solid; ) ?? (Wondering)

I don't understand.
We can do that, can't we?:confused:

With the following function:

Code:
int Beginning(int solid, int ss){
	        int i, k;
                plansys_t *plansystem = calloc(1, sizeof(plansys_t));
                plansystem->asteroids=NULL;
		for(i=0; i<nrStarS; i++){
			if(StarS[i].ss=ss){
				k=i;
			}
		}
		StarS[k].plasy=plansystem;
		return k;
}

the new planetary system contains an empty list of asteroids and the new plansystem is added to the array StarS at the position where the star system has the identifier "ss", right?? (Wondering) But I haven't used the identifier of the new object "solid"...

So you should add [m]plansystem->solid = solid;[/m], shouldn't you? (Wondering)

Btw, what happens if $ss$ cannot be found? (Wondering)
 
  • #39
I like Serena said:
So you should add [m]plansystem->solid = solid;[/m], shouldn't you? (Wondering)

Btw, what happens if $ss$ cannot be found? (Wondering)

So, should it be as followed?? (Wondering)

Code:
int k=-1;

int Beginning(int solid, int ss){
		int i;
                plansys_t *plansystem = calloc(1, sizeof(plansys_t));
                plansystem->solid=solid;
                plansystem->asteroids=NULL;
		      for(i=0; i<nrStarS; i++){
			   if(StarS[i].ss=ss){
				k=i;
                                StarS[k].plasy=plansystem;
			   }
		      }
                      if(k==-1){
                         printf("The star system with identifier %d couldn't be found\n", ss);
                      }
		return k;
}
 
Last edited by a moderator:
  • #40
mathmari said:
So, should it be as followed?? (Wondering)

Code:
int k=-1;

int Beginning(int solid, int ss){
		int i;
                plansys_t *plansystem = calloc(1, sizeof(plansys_t));
                plansystem->solid=solid;
                plansystem->asteroids=NULL;
		      for(i=0; i<nrStarS; i++){
			   if(StarS[i].ss=ss){
				k=i;
                                StarS[k].plasy=plansystem;
			   }
		      }
                      if(k==-1){
                         printf("The star system with identifier %d couldn't be found\n", ss);
                      }
		return k;
}

Well, you wrote: "The new element, that corresponds to the new planetary system, should be added to the list of the planetary systems of the star system".

I interpret that to mean that the current list is not necessarily empty.
That means that you have to add it to the linked list. (Wasntme)Btw, we could treat $ss$ as an index in the StarS array, in which case we could do: [m]starsy_t *starsystem = &StarS[ss];[/m].
Adding a new star system would then simply mean initializing the [m]StarS[ss][/m] structure, which is also an O(1) operation. (Nerd)
 
  • #41
The function should do the following:
Constitution of a new object and a new planetary system with identifier "solid" at the star system with identifier "ss".

>empty list of asteroids

>add a new element to the list of the planetary system of the star system with idenifier "ss".

I like Serena said:
Well, you wrote: "The new element, that corresponds to the new planetary system, should be added to the list of the planetary systems of the star system".

I interpret that to mean that the current list is not necessarily empty.
That means that you have to add it to the linked list. (Wasntme)

Which list do you mean?? (Wondering)
I like Serena said:
Btw, we could treat $ss$ as an index in the StarS array, in which case we could do: [m]starsy_t *starsystem = &StarS[ss];[/m].
Adding a new star system would then simply mean initializing the [m]StarS[ss][/m] structure, which is also an O(1) operation. (Nerd)

Could you explain me what you mean?? (Wondering)
 
  • #42
mathmari said:
The function should do the following:
Constitution of a new object and a new planetary system with identifier "solid" at the star system with identifier "ss".

>empty list of asteroids

>add a new element to the list of the planetary system of the star system with idenifier "ss".

Which list do you mean?? (Wondering)

The [m]starsy_t[/m] structure has a field [m]plasy[/m] that identifies a linked list:

[m]plansys_t *plasy; /*Pointer to the first element in the list of the planetary system */[/m]

In turn, the structure [m]plansys_t[/m] has a [m]next[/m] field, which links the structure to the next plansys_t. (Thinking)
mathmari said:
Could you explain me what you mean?? (Wondering)

I mean that your Constitution() function could be like this.

Code:
int Constitution(int ss) {
    if ((ss >= 1) && (ss < N)) {
        // Initialize the star system
        starsy_t *starsystem = &StarS[ss];
        starsystem->ss = ss;
        return 0; // Success
    } else {
        printf("The star system could not be constituted: %d \n", ss);
        return 1; // Failure
    }
}

int main(){
    Constitution(12);
    for(ss = 1; ss < N; i++){
        if (StarS[ss].ss == ss) {
            printf("The elements are: %d \n", ss);
        }
    }
    return 0;
}
(Thinking)
 
  • #43
I like Serena said:
The [m]starsy_t[/m] structure has a field [m]plasy[/m] that identifies a linked list:

[m]plansys_t *plasy; /*Pointer to the first element in the list of the planetary system */[/m]

In turn, the structure [m]plansys_t[/m] has a [m]next[/m] field, which links the structure to the next plansys_t. (Thinking)

Ahaa...

- - - Updated - - -

It has to look as followed:

View attachment 3469

So could we write it as you did in the post #42?? (Wondering)
 

Attachments

  • star_system.png
    star_system.png
    5.5 KB · Views: 65
  • #44
mathmari said:
It has to look as followed:

View attachment 3469

Aha! (Smile)

So could we write it as you did in the post #42?? (Wondering)

Yes.
It fits either way. (Thinking)
 
  • #45
I like Serena said:
Aha! (Smile)
Yes.
It fits either way. (Thinking)

At the code of the post #42, we suppose that 1 $\leq $ ss $\leq $ N, right?? (Wondering)

Now I read again the exercise and I saw the following:

The function Constitution should print the identifiers of the star systems:
$<ss_1>, <ss_2>, \dots , <ss_n>$
where $n$ is the number of the existing star systems and for each $i$, $1 \leq i \leq n$, $<ss_i>$ is the identifier of the $i-$th star system.So, can it not be that the identifier of a star system is greater than $N$ ?? (Wondering)
 
  • #46
mathmari said:
At the code of the post #42, we suppose that 1 $\leq $ ss $\leq $ N, right?? (Wondering)

It's a little bit sharper.
We suppose $1 \leq ss < N$. (Mmm)
Now I read again the exercise and I saw the following:

The function Constitution should print the identifiers of the star systems:
$<ss_1>, <ss_2>, \dots , <ss_n>$
where $n$ is the number of the existing star systems and for each $i$, $1 \leq i \leq n$, $<ss_i>$ is the identifier of the $i-$th star system.So, can it not be that the identifier of a star system is greater than $N$ ?? (Wondering)

Yes.
With that description we can apparently not make assumptions about $ss$.
So then we should keep it as before, and track $n$ in [m]nrStarS[/m]. (Wasntme)

It also means that the function is supposed to print something specific.
 
  • #47
I like Serena said:
It's a little bit sharper.
We suppose $1 \leq ss < N$. (Mmm)

Ahaa... (Blush)
I like Serena said:
Yes.
With that description we can apparently not make assumptions about $ss$.
So then we should keep it as before, and track $n$ in [m]nrStarS[/m]. (Wasntme)

It also means that the function is supposed to print something specific.

Ok... So, is it as followed?? (Wondering)

Code:
int nrStarS = 0;

int Constitution(int ss) {
        int i;

        // Add a star system to the the array of stars
        starsy_t *starsystem = &StarS[nrStarS];
        nrStarS++;

        // Initialize the new star system
        starsystem->ss = ss;
        starsystem->plasy=starsystem->Sentinel;
	for(i=0; i<max; i++){
		starsystem->ffplan[i].fp=INT_MAX;
		starsystem->ffplan[i].ff=NULL;
	}

        // Return the index of the star system that was added
	return (nrStarS - 1);
}
	
	
int main(){
	int i, ss;
	printf("Give the identifier ss: \n");
	scanf("%d", &ss);
	Constitution(ss);
	for(i=0; i<nrStarS; i++){
		printf(" %d ", StarS[i].ss);
	}
        return 0;
}
 
  • #48
mathmari said:
Ok... So, is it as followed?? (Wondering)

Code:
int nrStarS = 0;

int Constitution(int ss) {
        int i;

        // Add a star system to the the array of stars
        starsy_t *starsystem = &StarS[nrStarS];
        nrStarS++;

        // Initialize the new star system
        starsystem->ss = ss;
        starsystem->plasy=starsystem->Sentinel;
	for(i=0; i<max; i++){
		starsystem->ffplan[i].fp=INT_MAX;
		starsystem->ffplan[i].ff=NULL;
	}

        // Return the index of the star system that was added
	return (nrStarS - 1);
}
	
	
int main(){
	int i, ss;
	printf("Give the identifier ss: \n");
	scanf("%d", &ss);
	Constitution(ss);
	for(i=0; i<nrStarS; i++){
		printf(" %d ", StarS[i].ss);
	}
        return 0;
}

I guess so. (Dull)
 
  • #49
The function Beginning should print the identifiers of the planetary systems:
$<solid_1>, <solid_2>, \dots , <solid_n>$
where $n$ is the number of planetary systems that exist in the star system with identifier "ss" and for each $i$, $1 \leq i \leq n$, $<solid_i>$ is the identifier of the $i-$th planetary system in the star system.

Code:
int k = -1;

int Beginning(int solid, int ss){
		int i;

        // Add a planetary system to the list of the planetary system of the star system
        plansys_t *plansystem = calloc(1, sizeof(plansys_t));
        plansystem->solid=solid;
        plansystem->asteroids=NULL;
        plansystem->next=NULL;
		for(i=0; i<nrStarS; i++){
			if(StarS[i].ss=ss){
				k=i;
				StarS[k].plasy=plansystem;
			}
		}
		if(k==-1){
			printf("The star system with identifier %d couldn't be found\n", ss);
		}
		return k;
}int main(){
    int solid, ss
    printf("Give the identifier solid and ss: \n");
    scanf("%d %d", &solid, &ss);
    Beginning(solid, ss);
    while(StarS[k].plasy != NULL){
            printf(" %d ", StarS[k].plasy->solid);
            StarS[k].plasy=StarS[k].plasy->next;
    }
    return 0;
}

Do we have to create a new node at the function Beginning, for the element that should be added?? (Wondering)
 
  • #50
mathmari said:
The function Beginning should print the identifiers of the planetary systems:
$<solid_1>, <solid_2>, \dots , <solid_n>$
where $n$ is the number of planetary systems that exist in the star system with identifier "ss" and for each $i$, $1 \leq i \leq n$, $<solid_i>$ is the identifier of the $i-$th planetary system in the star system.

Code:
int k = -1;

int Beginning(int solid, int ss){
		int i;

        // Add a planetary system to the list of the planetary system of the star system
        plansys_t *plansystem = calloc(1, sizeof(plansys_t));
        plansystem->solid=solid;
        plansystem->asteroids=NULL;
        plansystem->next=NULL;
		for(i=0; i<nrStarS; i++){
			if(StarS[i].ss=ss){
				k=i;
				StarS[k].plasy=plansystem;
			}
		}
		if(k==-1){
			printf("The star system with identifier %d couldn't be found\n", ss);
		}
		return k;
}int main(){
    int solid, ss
    printf("Give the identifier solid and ss: \n");
    scanf("%d %d", &solid, &ss);
    Beginning(solid, ss);
    while(StarS[k].plasy != NULL){
            printf(" %d ", StarS[k].plasy->solid);
            StarS[k].plasy=StarS[k].plasy->next;
    }
    return 0;
}

Do we have to create a new node at the function Beginning, for the element that should be added?? (Wondering)

Huh? :confused:Currently you are executing [m]StarS[k].plasy=plansystem;[/m].
If [m]StarS[k].plasy[/m] already contained planets, those will become inaccessible after the assignment. (Wasntme)
 
  • #51
I like Serena said:
Huh? :confused:Currently you are executing [m]StarS[k].plasy=plansystem;[/m].
If [m]StarS[k].plasy[/m] already contained planets, those will become inaccessible after the assignment. (Wasntme)

So is the for loop as followed?? (Wondering)

Code:
for(i=0; i<nrStarS; i++){
	if(StarS[i].ss==ss){
	      k=i;
	      while(StarS[k].plasy != NULL){
		      StarS[k].plasy=StarS[k].plasy->next;
	      }
	      StarS[k].plasy=plansystem;
         }
}
 
  • #52
mathmari said:
So is the for loop as followed?? (Wondering)

Code:
for(i=0; i<nrStarS; i++){
	if(StarS[i].ss==ss){
	      k=i;
	      while(StarS[k].plasy != NULL){
		      StarS[k].plasy=StarS[k].plasy->next;
	      }
	      StarS[k].plasy=plansystem;
         }
}

Let's see... suppose we initially have 2 planets. Something like:

$$plasy \to \boxed{solid_1} \to \boxed{solid_2}\to NULL$$
After the first execution of [m]StarS[k].plasy=StarS[k].plasy->next;[/m], we have:
\begin{array}{}
&plasy&&plansystem\\
&\downarrow &&\downarrow\\
\boxed{solid_1} \to &{\boxed{solid_2}}\to & NULL \quad & {\boxed{solid_{new}}}\to NULL
\end{array}

But... but... now ${solid}_1$ is not accessible any more.
That can't be right! (Worried)

We will finally have:
\begin{array}{}
&&&plasy\\
&&&\downarrow \\
\boxed{solid_1} \to &{\boxed{solid_2}}\to & NULL \quad & {\boxed{solid_{new}}}\to NULL
\end{array}

This is the same as previously!
The original list is not accessible anymore. (Worried)
 
  • #53
I like Serena said:
Let's see... suppose we initially have 2 planets. Something like:

$$plasy \to \boxed{solid_1} \to \boxed{solid_2}\to NULL$$
After the first execution of [m]StarS[k].plasy=StarS[k].plasy->next;[/m], we have:
\begin{array}{}
&plasy&&plansystem\\
&\downarrow &&\downarrow\\
\boxed{solid_1} \to &{\boxed{solid_2}}\to & NULL \quad & {\boxed{solid_{new}}}\to NULL
\end{array}

But... but... now ${solid}_1$ is not accessible any more.
That can't be right! (Worried)

We will finally have:
\begin{array}{}
&&&plasy\\
&&&\downarrow \\
\boxed{solid_1} \to &{\boxed{solid_2}}\to & NULL \quad & {\boxed{solid_{new}}}\to NULL
\end{array}

This is the same as previously!
The original list is not accessible anymore. (Worried)

We assign the value [m]plansystem[/m] to [m]StarS[k].plasy[/m] when [m]StarS[k].plasy==NULL[/m] or not?? (Wondering)
 
  • #54
mathmari said:
We assign the value [m]plansystem[/m] to [m]StarS[k].plasy[/m] when [m]StarS[k].plasy==NULL[/m] or not?? (Wondering)

If [m]StarS[k].plasy==NULL[/m], that is fine.
Otherwise we need to do something to properly insert [m]plansystem[/m] into the list. (Thinking)
 
  • #55
I like Serena said:
If [m]StarS[k].plasy==NULL[/m], that is fine.
Otherwise we need to do something to properly insert [m]plansystem[/m] into the list. (Thinking)

But doesn't the pointer plasy show always to NULL, after the while loop?? (Wondering)
 
  • #56
mathmari said:
But doesn't the pointer plasy show always to NULL, after the while loop?? (Wondering)

Yes it does.
However, plasy is always supposed to point the the first element in the list.
So you shouldn't change it to point to the last element. (Shake)Instead I suggest to either add the new plansystem to the beginning of the list, like this:
Code:
// Insert plansystem at the beginning of the list
plansystem->next = StarS[k].plasy;
StarS[k].plasy = plansystem;

Or add the new plansystem to the end of the list, like this:
Code:
if (StarS[k].plasy == NULL) {
    // List is empty: replace with new plansystem
    StarS[k].plasy = plansystem;
} else {
    // Find last node
    plansys_t *last = StarS[k].plasy;
    while (last->next != NULL) {
        last = last->next;
    }
    // Append new plansystem to the last node
    last->next = plansystem;
}
(Thinking)
 
  • #57
I like Serena said:
Yes it does.
However, plasy is always supposed to point the the first element in the list.
So you shouldn't change it to point to the last element. (Shake)Instead I suggest to either add the new plansystem to the beginning of the list, like this:
Code:
// Insert plansystem at the beginning of the list
plansystem->next = StarS[k].plasy;
StarS[k].plasy = plansystem;

Or add the new plansystem to the end of the list, like this:
Code:
if (StarS[k].plasy == NULL) {
    // List is empty: replace with new plansystem
    StarS[k].plasy = plansystem;
} else {
    // Find last node
    plansys_t *last = StarS[k].plasy;
    while (last->next != NULL) {
        last = last->next;
    }
    // Append new plansystem to the last node
    last->next = plansystem;
}
(Thinking)

I see... (Sun)

So that we print the identifiers "solid" which belong to a given star system "ss", do we return from the function Beginning the value of k ??

Then at the main do we write the following?? (Wondering)

Code:
printf("Give the identifier solid and ss: \n");
scanf("%d %d", &solid, &ss);
Beginning(solid, ss);
plansys_t *p = StarS[k].plasy;
while(p != NULL){
	printf(" %d ", p->solid);
	p=p->next;
}
 
  • #58
mathmari said:
I see... (Sun)

So that we print the identifiers "solid" which belong to a given star system "ss", do we return from the function Beginning the value of k ??

Then at the main do we write the following?? (Wondering)

Code:
printf("Give the identifier solid and ss: \n");
scanf("%d %d", &solid, &ss);
Beginning(solid, ss);
plansys_t *p = StarS[k].plasy;
while(p != NULL){
	printf(" %d ", p->solid);
	p=p->next;
}

That looks okay, although you should account for the possibility that the star system cannot be found.
Btw, didn't you forget to give $k$ a value? (Worried)
 
  • #59
I like Serena said:
That looks okay, although you should account for the possibility that the star system cannot be found.
Btw, didn't you forget to give $k$ a value? (Worried)

Code:
int k = -1;

int Beginning(int solid, int ss){
		int i;

        // Add a planetary system to the list of the planetary system of the star system
        plansys_t *plansystem = calloc(1, sizeof(plansys_t));
        
        plansystem->solid=solid;
        plansystem->asteroids=NULL;
        plansystem->next=NULL;
		for(i=0; i<nrStarS; i++){
			if(StarS[i].ss==ss){
				k=i;
				if (StarS[k].plasy == NULL) {
                     // List is empty: replace with new plansystem
                     StarS[k].plasy = plansystem;
                } else {
                    // Find last node
                    plansys_t *last = StarS[k].plasy;
                    while (last->next != NULL) {
                          last = last->next;
                    }
                    // Append new plansystem to the last node
                    last->next = plansystem;
               }
			}
		}
		if(k==-1){
			printf("The star system with identifier %d couldn't be found\n", ss);
		}
		return k;
}

int main(){
    int solid, ss;
    printf("Give the identifier solid and ss: \n");
    scanf("%d %d", &solid, &ss);
    Beginning(solid, ss);
    plansys_t *p = StarS[k].plasy;
    while(p != NULL){
	   printf(" %d ", p->solid);
	   p=p->next;
    }
    return 0;
}

At the function Beginning do we not account for the possibility that the star system cannot be found?? (Wondering)

By returning $k$ from the function Beginning doesn't it have the value at the main when calling the function?? (Wondering)
 
  • #60
mathmari said:
Code:
int k = -1;

int Beginning(int solid, int ss){
		int i;

        // Add a planetary system to the list of the planetary system of the star system
        plansys_t *plansystem = calloc(1, sizeof(plansys_t));
        
        plansystem->solid=solid;
        plansystem->asteroids=NULL;
        plansystem->next=NULL;
		for(i=0; i<nrStarS; i++){
			if(StarS[i].ss==ss){
				k=i;
				if (StarS[k].plasy == NULL) {
                     // List is empty: replace with new plansystem
                     StarS[k].plasy = plansystem;
                } else {
                    // Find last node
                    plansys_t *last = StarS[k].plasy;
                    while (last->next != NULL) {
                          last = last->next;
                    }
                    // Append new plansystem to the last node
                    last->next = plansystem;
               }
			}
		}
		if(k==-1){
			printf("The star system with identifier %d couldn't be found\n", ss);
		}
		return k;
}

int main(){
    int solid, ss;
    printf("Give the identifier solid and ss: \n");
    scanf("%d %d", &solid, &ss);
    Beginning(solid, ss);
    plansys_t *p = StarS[k].plasy;
    while(p != NULL){
	   printf(" %d ", p->solid);
	   p=p->next;
    }
    return 0;
}

At the function Beginning do we not account for the possibility that the star system cannot be found?? (Wondering)

By returning $k$ from the function Beginning doesn't it have the value at the main when calling the function?? (Wondering)

We account for it inside the function yes.
But when it fails, we have $k=-1$.
Then, in the main() function the StarS[] array still gets indexed with $k=-1$, which will result in a bit of madness and chaos. (Worried)Btw, it is bad programming style to keep the $k$ outside the function in the global namespace. It is better to declare it inside the function, and when you call the function keep track of the value that the function returned. (Nerd)
 
  • #61
I like Serena said:
We account for it inside the function yes.
But when it fails, we have $k=-1$.
Then, in the main() function the StarS[] array still gets indexed with $k=-1$, which will result in a bit of madness and chaos. (Worried)

So, we could write the following if in the main() function, after calling the function, instead of writing it in the function Beginning, right?? (Wondering)

Code:
if(k==-1){
	printf("The star system with identifier %d couldn't be found\n", ss);
}
I like Serena said:
Btw, it is bad programming style to keep the $k$ outside the function in the global namespace. It is better to declare it inside the function, and when you call the function keep track of the value that the function returned. (Nerd)

Ok, I changed it! (Yes)

Now it works! (Happy)
 
  • #62
mathmari said:
Code:
int nrStarS = 0;

int Constitution(int ss) {
        int i;

        // Add a star system to the the array of stars
        starsy_t *starsystem = &StarS[nrStarS];
        nrStarS++;

        // Initialize the new star system
        starsystem->ss = ss;
        starsystem->plasy=starsystem->Sentinel;
	for(i=0; i<max; i++){
		starsystem->ffplan[i].fp=INT_MAX;
		starsystem->ffplan[i].ff=NULL;
	}

        // Return the index of the star system that was added
	return (nrStarS - 1);
}
	
	
int main(){
	int i, ss;
	printf("Give the identifier ss: \n");
	scanf("%d", &ss);
	Constitution(ss);
	for(i=0; i<nrStarS; i++){
		printf(" %d ", StarS[i].ss);
	}
        return 0;
}

Should I declare the variable [m]nrStarS [/m] also inside the function Constitution?? (Wondering)
 
  • #63
mathmari said:
So, we could write the following if in the main() function, after calling the function, instead of writing it in the function Beginning, right?? (Wondering)

Code:
if(k==-1){
	printf("The star system with identifier %d couldn't be found\n", ss);
}

Yes. (Nod)
Ok, I changed it! (Yes)

Now it works! (Happy)

Good! (Smile)
mathmari said:
Should I declare the variable [m]nrStarS [/m] also inside the function Constitution?? (Wondering)

No.
[m]nrStarS[/m] tracks a global state, together with the StarS[] array.
It is appropriate that it is global. (Nerd)
 
  • #64
I like Serena said:
No.
[m]nrStarS[/m] tracks a global state, together with the StarS[] array.
It is appropriate that it is global. (Nerd)

Ahaaa... I see... (Star)Then I have to write also an other function for the constitution of an asteroid with identifier "as" at the planetary system "solid".
"gap" is the gap between the new asteroid and the object.
The new asteroid should be added in the appropriate position, in respect of the "gap", in the list of asteroids.
The field gap, that corresponds to the new asteroid, contains the gap from the previous asteroid in the list of asteroids and not the gap from the object.

I have done the following:

Code:
int asteroid_constitution(int as, int gap, int solid){
	int i, g=0;
	asteroid_t *ast = calloc(1, sizeof(asteroid_t));
	ast->as=as;
	ast->gap=gap;
	ast->prev=NULL;
	ast->next=NULL;
	plansys_t *p=StarS[0].plasy;;
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while(p->solid != solid){
			p=p->next;
		}
	}
	if (p->asteroids==NULL){
		p->asteroids=ast;
	}
	else{
		asteroid_t *last=p->asteroids;
		asteroid_t *q=p->asteroids;
		g=g+ast->gap;
		while(g<ast->gap){
			last=last->next;
		}
		ast->next=last->next;
		ast->prev=last;
		last->next=ast;
		ast->gap=ast->gap-g;
		q=ast->next;
		q->gap=q->gap-ast->gap;		
	}
	return 0;
}

Is this correct?? (Wondering)
 
  • #65
mathmari said:
Ahaaa... I see... (Star)Then I have to write also an other function for the constitution of an asteroid with identifier "as" at the planetary system "solid".
"gap" is the gap between the new asteroid and the object.
The new asteroid should be added in the appropriate position, in respect of the "gap", in the list of asteroids.
The field gap, that corresponds to the new asteroid, contains the gap from the previous asteroid in the list of asteroids and not the gap from the object.

I have done the following:

Code:
int asteroid_constitution(int as, int gap, int solid){
	int i, g=0;
	asteroid_t *ast = calloc(1, sizeof(asteroid_t));
	ast->as=as;
	ast->gap=gap;
	ast->prev=NULL;
	ast->next=NULL;
	plansys_t *p=StarS[0].plasy;;
	for(i=0; i<Sfreep; i++){
		p=StarS[i].plasy;
		while(p->solid != solid){
			p=p->next;
		}
	}
	if (p->asteroids==NULL){
		p->asteroids=ast;
	}
	else{
		asteroid_t *last=p->asteroids;
		asteroid_t *q=p->asteroids;
		g=g+ast->gap;
		while(g<ast->gap){
			last=last->next;
		}
		ast->next=last->next;
		ast->prev=last;
		last->next=ast;
		ast->gap=ast->gap-g;
		q=ast->next;
		q->gap=q->gap-ast->gap;		
	}
	return 0;
}

Is this correct?? (Wondering)

What is [m]Sfreep[/m]? (Wondering)

What if [m]q[/m] is NULL? (Wondering)

Shouldn't the asteroid after the new one have its [m]prev[/m] updated? (Wondering)
 
  • #66
I like Serena said:
What is [m]Sfreep[/m]? (Wondering)

[m]Sfreep[/m] should be replaced with [m]nrStarS[/m] (Blush)
I like Serena said:
What if [m]q[/m] is NULL? (Wondering)

We have initialized [m]q[/m] as followed

[m]asteroid_t *q=p->asteroids;[/m]

right?? (Wondering)

In the else statement [m]p[/m] is not NULL. So, can [m]q[/m] be NULL ?? (Wondering)
I like Serena said:
Shouldn't the asteroid after the new one have its [m]prev[/m] updated? (Wondering)

Could I maybe do it as followed?? (Wondering)

Code:
ast->next=last->next;
last->next->prev=ast;
ast->prev=last;
last->next=ast;
ast->gap=ast->gap-g;
q=ast->next;
q->gap=q->gap-ast->gap;
 
  • #67
mathmari said:
[m]Sfreep[/m] should be replaced with [m]nrStarS[/m] (Blush)

We have initialized [m]q[/m] as followed

[m]asteroid_t *q=p->asteroids;[/m]

right?? (Wondering)

That is at first, but you never use it like that.
So you might as well not initialize it, or initialize it to NULL. (Nerd)
In the else statement [m]p[/m] is not NULL. So, can [m]q[/m] be NULL ?? (Wondering)

When you do give [m]q[/m] a value in [m]q=ast->next[/m], it can be NULL. (Shake)
Could I maybe do it as followed?? (Wondering)

Code:
ast->next=last->next;
last->next->prev=ast;

In principle, yes, but what if [m]last->next[/m] is NULL? (Wondering)

Code:
q=ast->next;
q->gap=q->gap-ast->gap;

What if [m]q[/m] becomes NULL here?
Then you are following a NULL pointer to madness and chaos! (Evilgrin)
 
  • #68
I like Serena said:
That is at first, but you never use it like that.
So you might as well not initialize it, or initialize it to NULL. (Nerd)When you do give [m]q[/m] a value in [m]q=ast->next[/m], it can be NULL. (Shake)

In principle, yes, but what if [m]last->next[/m] is NULL? (Wondering)
What if [m]q[/m] becomes NULL here?
Then you are following a NULL pointer to madness and chaos! (Evilgrin)

So, should we add the condition [m]last->next!=NULL[/m] in the while?? (Wondering)

Code:
asteroid_t *last=p->asteroids;
asteroid_t *q=NULL;
g=g+ast->gap;
while(g<ast->gap && last->next!=NULL){
	last=last->next;
}
ast->next=last->next;
ast->prev=last;
last->next=ast;
ast->gap=ast->gap-g;
q=ast->next;
q->gap=q->gap-ast->gap;
 
  • #69
mathmari said:
So, should we add the condition [m]last->next!=NULL[/m] in the while?? (Wondering)

Code:
asteroid_t *last=p->asteroids;
asteroid_t *q=NULL;
g=g+ast->gap;
while(g<ast->gap && last->next!=NULL){
	last=last->next;
}
ast->next=last->next;
ast->prev=last;
last->next=ast;
ast->gap=ast->gap-g;
q=ast->next;
q->gap=q->gap-ast->gap;

No. (Shake)
We should distinguish 2 cases with an [m]if[/m], or actually 4.
It can be that "ast" has to be inserted before the first element of the list, and it can also be that "ast" has to be appended after the last element of the list.
These cases require special handling. (Sweating)
 
  • #70
I like Serena said:
No. (Shake)
We should distinguish 2 cases with an [m]if[/m], or actually 4.
It can be that "ast" has to be inserted before the first element of the list, and it can also be that "ast" has to be appended after the last element of the list.
These cases require special handling. (Sweating)

I added the case where "ast" has to be inserted before the first element of the list and the case where "ast" has to be appended after the last element of the list.

Code:
                asteroid_t *last=p->asteroids;	
	        asteroid_t *q=NULL;
		if(ast->gap<last->gap){
			ast->prev=NULL;
			ast->next=last;
			last->prev=ast;
			last->gap=last->gap-ast->gap;
		}
		g=g+ast->gap;
		while(g<ast->gap && last != NULL){
			last=last->next;
		}
		if(last != NULL && last->next==NULL){
			ast->prev=last;
			ast->next=NULL;
			last->next=ast;
		}
		else{
			ast->next=last->next;
			ast->prev=last;
			last->next=ast;
			ast->gap=ast->gap-g;
			q=ast->next;
			q->gap=q->gap-ast->gap;	
	    }

Is it correct?? (Wondering)
 

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top