+ 1
I have two questions:
Question 1: What does it mean if we inicialize variable like this nxtBgElem = -1? for (j = i+1, nxtBgElem = -1; j < arr1_size; j++) Questions 2: And what is the difference between these two for loops for(int i=0; i < arr1_size; i++) for(int j=i+1; j < arr1_size; j++) and for (int i=0, j = i+1; i < arr1_size; i++, j++)
5 odpowiedzi
+ 1
Q1: It means that nxtBgElem is set to -1 right before the loop starts.
Q2: The difference is certainly in the number of iterations. While the first has (arr1_size)*(arr1_size-1)/2 (roughly), the second has only arr1_size many iterations. In the first loop, i and j advance independently, in the second the advance together. If you think of a square matrix of size arr1_size, then the first loop's indices i and j would hit the full upper triangle, the second loop only the upper off-diagonal.
+ 1
Yes I know it is set to -1, but why, what does it mean?
+ 1
That I cannot tell 🙂 All I can say is, that it is set to -1 🤷 The purpose of which will reveal itself 🔮 in the code that follows thereafter ...
0
I have seen in some code that inicialised -1 has special meaning. ???
0
It will mean something, yes. But only the code can tell. It has no universal meaning.