MHB Why Use a Helper Variable When Inserting an Element in an Array?

  • Thread starter Thread starter mathmari
  • Start date Start date
  • Tags Tags
    Array
Click For Summary
The discussion centers on the insertion of a new star system into an array of star systems with the goal of achieving O(1) time complexity. Participants clarify that inserting an element at a specific index in an array is O(1), but managing the size of the array requires tracking the number of existing systems. A proposed solution involves using a separate variable to keep track of the number of star systems, allowing for efficient insertion. There is also debate about whether to treat the array as a linked list or maintain it as a fixed-size array. The final consensus suggests initializing only the new star system rather than all systems during insertion.
  • #91
I like Serena said:
The first is correct! (Smile)

But the second is not. (Worried)
Sentinal is a data member of StarS[k], and not of last->next.
You should set StarS[k].Sentinal to last->next, which is by now the same as plansystem.Alternatively, you could do at the end: [m]StarS[k].Sentinal = plansystem[/m].
That works because [m]plansystem[/m] is always added to the end, wherever that is. So afterwards it is the last node in the list, which is where [m]Sentinal[/m] should point. (Nerd)

Do you mean that we could write it as followed??

Code:
int Beginning(int solid, int ss){
        int i, k=-1;
        /* 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<Sfreep; 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;
                        }
                        StarS[k].Sentinal = plansystem;
                }
        }
	return k;
}

(Wondering)
I like Serena said:
The way the code is indented, it is hard to follow the flow of the program, and understand if and where something is wrong and should be changed. (Sweating)

Better is:
Code:
int Beginning(int solid, int ss){
    int i, k = -1;
    /* 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<Sfreep; i++){
        if(StarS[i].ss == ss){
            k=i;
            if (StarS[k].plasy == NULL) {
                /* List is empty: replace with new plansystem */
                StarS[k].plasy = plansystem;
                StarS[k].Sentinel = StarS[k].plasy;
            } 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;
                last->next->Sentinel = last;
            }
        }
    }
    return k;
}

Like this it becomes clear where the for-loop starts and ends, where the first if-statement is, and where the nested if-else statement is (and that there is one). (Whew)

Ok! (Blush)
 
Technology news on Phys.org
  • #92
mathmari said:
Do you mean that we could write it as followed?? (Wondering)

Yep! (Nod)
 
  • #93
mathmari said:
The structures have the following form:

View attachment 3540

I am looking again at the program and I got stuck...

Code:
asteroid_t *p=StarS[k].plasy->asteroids;
while(p != NULL && p->as != as){
		p=p->next;
}
if(p != NULL && p->as == as){
		k=i;				
}
After [m]p=p->next;[/m] where does [m]p[/m] point?? At the next node of the planetary system, plasy, or the next asteroid?? (Wondering)
 
  • #94
Hey! :o

mathmari said:
After [m]p=p->next;[/m] where does [m]p[/m] point?? At the next node of the planetary system, plasy, or the next asteroid?? (Wondering)

Code:
asteroid_t *p=StarS[k].plasy->asteroids;

After the initial assignment, [m]p[/m] points to the first asteroid of the first planetary system of star system [m]k[/m].

Code:
while(p != NULL && p->as != as){
		p=p->next;
}

After p=p->next, p points to the next asteroid of the first planetary system. (Nerd)
 
  • #95
Ok! (Malthe)

To find the asteroid with identifier "as" in the star system I had written the following part:

Code:
	int i, k=-1;
	asteroid_t *p=NULL;
	asteroid_t *ast=NULL;
	for(i=0; i<Sfreep; i++){
			ast=StarS[i].plasy->asteroids;
			while(ast != NULL && ast->as != as){
				ast=ast->next;
			}
			if(ast != NULL && ast->as == as){
				k=i;				
			}
	}

	if(k == -1){
		return -1;
	}
		
	p=StarS[k].plasy->asteroids;
	
	if(p == NULL){
		return -1;
	}

But in this way do we not look only the asteroids of the first planetary system?? (Wondering)

Should it be as followed??

Code:
	for(i=0; i<Sfreep; i++){
                        Plan_Syst=StarS[i].plasy;
                        while(Plan_Syst != NULL){ 
                                ast=Plan_Syst->asteroids;
			        while(ast != NULL && ast->as != as){
				        ast=ast->next;
			        }
			        if(ast != NULL && ast->as == as){
				        k=i;	
                                        A=ast;			
			       }
                               Plan_Syst=Plan_Syst->next; 
	                }
        }
	if(k == -1){
		return -1;
	}
		
	p=A;
	
	if(p == NULL){
		return -1;
	}

(Wondering)
 
  • #96
mathmari said:
Ok! (Malthe)

But in this way do we not look only the asteroids of the first planetary system?? (Wondering)

Should it be as followed??

(Wondering)

Yep. To search all asteroids it should be like that. (Wasntme)
 
  • #97
I like Serena said:
Yep. To search all asteroids it should be like that. (Wasntme)

Ok! (Malthe) I have also an other question...
I like Serena 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=StarS[i].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(){
	Constitution(12);
	for(i=0; i<nrStarS; i++){
		printf("The elements are: %d \n", StarS[i].ss);
	}
	return 0;
}

Could you explain to me why we write [m]starsystem = &StarS[nrStarS];[/m] and not [m]&StarS[nrStarS]=starsystem[/m] ?? (Wondering)
 
  • #98
mathmari said:
Could you explain to me why we write [m]starsystem = &StarS[nrStarS];[/m] and not [m]&StarS[nrStarS]=starsystem[/m] ?? (Wondering)

The structure [m]StarS[nrStarS][/m] is an entry in a global array. Its address is fixed and cannot change. That means it makes no sense to try and assign anything to [m]&StarS[nrStarS][/m].

The pointer [m]starsystem[/m] is a helper variable. We let it point to the location of [m]StarS[nrStarS][/m], so that we can manipulate it more easily, and so that the code reads better. That's why we assign the address [m]&StarS[nrStarS][/m] to it. (Nerd)
 
  • #99
I like Serena said:
The structure [m]StarS[nrStarS][/m] is an entry in a global array. Its address is fixed and cannot change. That means it makes no sense to try and assign anything to [m]&StarS[nrStarS][/m].

The pointer [m]starsystem[/m] is a helper variable. We let it point to the location of [m]StarS[nrStarS][/m], so that we can manipulate it more easily, and so that the code reads better. That's why we assign the address [m]&StarS[nrStarS][/m] to it. (Nerd)

How can the array [m]StarS[/m] have the value of the new starsystem at the position nrStarS, when we assign the [m]&StarS[nrStarS][/m] to [m]starsystem[/m] ?? (Wondering)

[m]nrStarS[/m] is the first free position of the array, so if we assign [m]&StarS[nrStarS][/m] to the new [m]starsystem[/m] doesn't it get the value [m]NULL[/m] ?? (Wondering)
 

Similar threads

  • · Replies 32 ·
2
Replies
32
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
7
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
235
Views
13K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 13 ·
Replies
13
Views
4K