I have two questions: | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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++)

20th Dec 2021, 7:59 AM
TeaserCode
5 Answers
+ 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.
20th Dec 2021, 12:18 PM
Agnes Ahlberg
Agnes Ahlberg - avatar
+ 1
Yes I know it is set to -1, but why, what does it mean?
20th Dec 2021, 12:54 PM
TeaserCode
+ 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 ...
20th Dec 2021, 2:07 PM
Agnes Ahlberg
Agnes Ahlberg - avatar
0
I have seen in some code that inicialised -1 has special meaning. ???
20th Dec 2021, 2:17 PM
TeaserCode
0
It will mean something, yes. But only the code can tell. It has no universal meaning.
20th Dec 2021, 2:23 PM
Agnes Ahlberg
Agnes Ahlberg - avatar