0
Arrays ; can someone please explain line by line for I stuck in arrays so far .. :) specially line 3
int a [3] = {8, 3, 2} ; for (int I =1; I <2; I++} ; a [0]* = a [I] ; } cout << a [0] ;
3 Respuestas
+ 8
int I = 1; I < 2;
This clearly states that the value of I in the loop will only be 1. Not 0 and not 2. The only computation which will be carried out is
a[0] *= a[1]
where the final value of a[0] is 24.
+ 1
Ok, so let's go:
Before we start: the space in line 3 after the aterisk is a bit irritating, it should be a[0] *= a[l].
This is an abbreviation for: a[0] = a[0]*a[l].
1st line:
You declare and initialize an array of 3 integers, containing the numbers 8,3,2. Simple as that.
You could have left the cling squares emty, because the compiler knows, that the array contains 3 integers, if you initialize it, how you did.
2nd line:
A for loop, i guess you know what it does.
BUT in this case, the loop is completely useless, because, you start at 1 and your loop ends at <2, which actually is 1. So the loop loops exactly once.
3rd line:
a[0] is the first element of your Array (the count starts at 0 not at 1). So 8 in this case.
As i said a[0] *= a[l] means a[0] = a[0]*[l]
l is the same variable, you use in your for loop.
so for example when l=0 a[l]=8.
if your loop had a second iteration a[1] would be 3.
So your loop count is alsways the index of your array element, but remember the first items index is 0 not.1.
So the line is:
a[0], which is 8 = 8*a[l], which is 3 at the Moment.
so the new a[0] is 24 and your array now is 24,3,2
if your loop hat another iteration, you had to multiply the new a[0] by a[2], which is the 3rd Element in the array. So the new a[0] would be 48..and so on
Hope, i could help you!
+ 1
@Josef Stangl ; don't know what to say !!! that was so much helpful and easy and clean many thanks to you... you're the best I was so stuck