MHB How many times will this statment be run?

  • Thread starter Thread starter find_the_fun
  • Start date Start date
AI Thread Summary
The discussion revolves around the execution count of a nested loop program segment, specifically questioning how many times the print statement is executed. The initial assumption was that it would run 12 times for the outer loop, 5 times for the middle loop, and 7 times for the inner loop, totaling 420 executions. However, the answer key states 576 executions, prompting confusion over the loop boundaries and whether they are inclusive or exclusive. Participants highlight the ambiguity in the loop definitions, particularly the interpretation of "for i := 1 to 12" and similar constructs in different programming languages. Ultimately, the conversation emphasizes the need for clarity in loop definitions to avoid miscalculations in execution counts.
find_the_fun
Messages
147
Reaction score
0
Consider the following program segment where i,j and k are integer variables
for i := 1 to 12 do
for j := 5 to 10 do
for k := 15 downto 8 do
printf (i-j)*k

How many time is the print statement executed?

I thought it would be 12x5x7 but the answer key has 576?
 
Physics news on Phys.org
If "for j := 5 to 10" runs 10 - 5 = 5 times, then why are you saying that "for i := 1 to 12" runs 12 times?

See off-by-one error in Wikipedia.
 
Evgeny.Makarov said:
If "for j := 5 to 10" runs 10 - 5 = 5 times, then why are you saying that "for i := 1 to 12" runs 12 times?

See off-by-one error in Wikipedia.

Isn't the question ambiguous? For example "for i := 1 to 12" you don't know if another iteration will occur at 12 i.e. you don't know if it's i < 12 or i <= 12
 
Well, I have not seen a programming language where a construction similar to "for i := m to n", without explicitly using < or ≤, runs the last iteration for i = n - 1. For example, in Fortran, the code

Code:
do i=1,10
  print*,i**2
end do

runs 10 times. The same thing happens in Pascal with "for i := 1 to 10 do".
 
Interesting question.

I did a test in Matlab and it seems to run both the lower bound and the upper bound (obviously everything in between as well).

The very inefficient code:
Code:
testmatrix = [];
for i=5:10;
    testmatrix = [testmatrix;i];
end;

Outputs:
View attachment 1619
 

Attachments

  • Screen Shot 2013-11-03 at 9.24.15 PM.png
    Screen Shot 2013-11-03 at 9.24.15 PM.png
    731 bytes · Views: 73
I was reading documentation about the soundness and completeness of logic formal systems. Consider the following $$\vdash_S \phi$$ where ##S## is the proof-system making part the formal system and ##\phi## is a wff (well formed formula) of the formal language. Note the blank on left of the turnstile symbol ##\vdash_S##, as far as I can tell it actually represents the empty set. So what does it mean ? I guess it actually means ##\phi## is a theorem of the formal system, i.e. there is a...

Similar threads

Replies
4
Views
1K
Replies
4
Views
4K
Replies
5
Views
1K
Replies
18
Views
1K
Replies
1
Views
1K
Replies
1
Views
2K
Replies
10
Views
3K
Back
Top