How there are eight assignments in this code?

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Hi,
Code:
for (i=4; i<n; i++) {
for(j=i-3, sum = a[i-4]; j<= i; j++)
   sum +=a[j];
   System.out.println("sum for sub array "+(i-4) + "through " +i+ "is" + sum);
}
In my view there are 5 assignments:
1. i=4
2. i++
3. j=i-3
4. sum = a[i-4]
5. j++
6. sum +=a[j];

Can somebody please tell me what are the two other assignments?

Zulfi.
 

WBahn

Joined Mar 31, 2012
29,976
Code:
for (i=4; i<n; i++) {
for(j=i-3, sum = a[i-4]; j<= i; j++)
   sum +=a[j];
   System.out.println("sum for sub array "+(i-4) + "through " +i+ "is" + sum);
}
As usual, Zulfi, your code formatting is atrocious.

The for() loops look like a sequence and the System.out statement looks like it's within the second for() loop.

PLEASE learn to pay attention to good code formatting.

Code:
for (i = 4; i < n; i++)
{
   for(j = i - 3, sum = a[i - 4]; j <= i; j++)
      sum += a[j];
   System.out.println("sum for sub array " + (i - 4) + "through " + i + "is" + sum);
}
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
I don't know that those would be considered "assignments".
Hi,
Thanks for your response. These are increments to index variables and we have to change the value of previous index. This is not possible without assignments.

Zulfi.
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
As usual, Zulfi, your code formatting is atrocious.

The for() loops look like a sequence and the System.out statement looks like it's within the second for() loop.

PLEASE learn to pay attention to good code formatting.

Code:
for (i = 4; i < n; i++)
{
   for(j = i - 3, sum = a[i - 4]; j <= i; j++)
      sum += a[j];
   System.out.println("sum for sub array " + (i - 4) + "through " + i + "is" + sum);
}
Hi my friend,
Thanks for your concern. Actually i have typed the book's code. I might have ignored formatting & you are right. Sorry for that.
They did not create a block for second i.e. inner 'for'. System.out... actually belongs to outer 'for'.

Zulfi.
 

WBahn

Joined Mar 31, 2012
29,976
Hi,
Thanks for your response. These are increments to index variables and we have to change the value of previous index. This is not possible without assignments.

Zulfi.
Huh?

So what if there was a statement in the loop that was just

sum += a[0];

Would you count that as two assignments?

What about

sum += *p;

Or what above

sum = *(a + i - 4);

which is the same as

sum = a[i-4];

and many simpler compilers will convert the latter to the former before it is actually sent to the compiler.
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Huh?

So what if there was a statement in the loop that was just

sum += a[0];

Would you count that as two assignments?

What about

sum += *p;

Or what above

sum = *(a + i - 4);

which is the same as

sum = a[i-4];

and many simpler compilers will convert the latter to the former before it is actually sent to the compiler.
Hi,
I think you are right. Actually the book says that:
"For each iteration of outer loop, there are eight assignments in the inner loop"
I am able to figure it out.
Actually inner loop runs only 4 times.
it runs from j=i-3 to j<= i;
initially i=4, so it runs
j=1 to j<=4 i.e. 4 times
Now 'i' would become 5. In that case, inner loop will run from:
j=2 to j<=5 i.e. 4 times
Now 'i' would become 6. In that case, inner loop will run from:
j=3 to j<=6 i.e. 4 times.

Now its clear that whatever be the value of 'i' inner loop runs four times.
Now check the assignments in the inner loop:
There are two assignments:
j++ & sum+= a[j]
because the inner loop runs 4 times for each value of 'i' so we can say that for each value of 'i' inner loop has
2 * 4 = 8 assignments.
I think this is right.
& outer loop runs n-4 times so there would be 8* (n-4) assignments in both the loops. However, there is one assignment left:
i.e. i=4
If we include this assignment then there would be 1+ 8 * (n-4) assignments in the whole code.
Thanks for pointing out my mistake.
Zulfi.
 
Last edited:

WBahn

Joined Mar 31, 2012
29,976
Hi,
I think you are right. Actually the book says that:
"For each iteration of outer loop, there are eight assignments in the inner loop"
You do realize that this is a very, very different question than what you originally asked.

Since no value for 'n' was given, the idea of determining how many assignments were executed by the code you gave is impossible. So we were left with trying to determine the number of assignment statements present in the code.

Now the question is how many assignments are executed on each pass of the inner loop.

Please take the time to make sure that what you are asking matches what you are trying to find out.
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
You do realize that this is a very, very different question than what you originally asked.

Since no value for 'n' was given, the idea of determining how many assignments were executed by the code you gave is impossible. So we were left with trying to determine the number of assignment statements present in the code.

Now the question is how many assignments are executed on each pass of the inner loop.

Please take the time to make sure that what you are asking matches what you are trying to find out.
Hi,
My Q was why we have 8 assignments? And I have shown you how there are eight assignments. And one person actually found it out. And i gave the reason for it but you showed why I am not correct. Then I have to check the code again & realized how we can have eight assignments. But the assignments were just related to the inner loop. At this point I realized that there is some mistake in grasping the concepts of book. So I checked the book again & found that it was just related to inner loop.

I cant say that Q was v v different. It was about 8 assignments and Mr. Chips also focused on proving 8 assignments.

<Since no value for 'n' was given, the idea of determining how many assignments were executed by the code
you gave is impossible.>
i did not use any value of 'n' nor the book used any value of 'n' & proved that there were eight assignments in the inner loop.
If I could have understood that it was just related to inner loop, I wont have even posted this Q. This is the advantage of forum that different sort of people interact with each other to solve a problem. I hope you must realize this.

Zulfi.
 

xox

Joined Sep 8, 2017
838
Hi,
My Q was why we have 8 assignments? And I have shown you how there are eight assignments. And one person actually found it out. And i gave the reason for it but you showed why I am not correct. Then I have to check the code again & realized how we can have eight assignments. But the assignments were just related to the inner loop. At this point I realized that there is some mistake in grasping the concepts of book. So I checked the book again & found that it was just related to inner loop.

I cant say that Q was v v different. It was about 8 assignments and Mr. Chips also focused on proving 8 assignments.

<Since no value for 'n' was given, the idea of determining how many assignments were executed by the code
you gave is impossible.>
i did not use any value of 'n' nor the book used any value of 'n' & proved that there were eight assignments in the inner loop.
If I could have understood that it was just related to inner loop, I wont have even posted this Q. This is the advantage of forum that different sort of people interact with each other to solve a problem. I hope you must realize this.

Zulfi.
Don't try to shift the blame onto someone else here. Had you posted the question exactly as it appeared in your book there would have been no confusion. End of story.
 

Thread Starter

zulfi100

Joined Jun 7, 2012
656
Hi,
I myself a teacher. Its challenge for you to find out this question in the book which i have referred otherwise this is not a blame game.

Zulfi
 

xox

Joined Sep 8, 2017
838
Hi,
I myself a teacher. Its challenge for you to find out this question in the book which i have referred otherwise this is not a blame game.
Your original question was simply "how are there eight assignments in this code". From the point of view of an experienced programmer, the term "assignments" (without any further qualification) ostensibly refers to literal source code statements. Later you reveal that the book actually asserts that "for each iteration of outer loop, there are eight assignments in the inner loop". That's an entirely different thing altogether, Professor.

So when you respond by saying "[...] And I have shown you how there are eight assignments. And one person actually found it out. And i gave the reason for it but you showed why I am not correct [...]" without even acknowledging that the source of confusion was due to your own imprecise statements in the first place, yes, it sounds a lot like you're trying to blame someone else.
 

WBahn

Joined Mar 31, 2012
29,976
Hi,
Have you found the question from the book?

Zulfi.
How is anyone supposed to be able to do that, given that you didn't give any indication of what book it's from, let alone which problem within the book?

Are they supposed to use a crystal ball or remote viewing?

And why should anyone else try to find the question in your book? YOU are the one that wanted help with a problem from whatever book you are using. Doesn't it seem reasonable that YOU should provide the problem, as stated, from that book?
 
Top