RE: My maturity test
January 9, 2018 at 10:27 pm
(This post was last modified: January 9, 2018 at 10:49 pm by SteelCurtain.)
Looks like the Tiger programming language, but with curly braces.
The first step in solving tasks such as these is to make sure you understand basic control structures in programming languages. For loops, while loops, if-then, if-then-else. Once you understand how these work, then stepping through small programs such as this becomes an exercise in maintaining your place as you step through.
For this program, we start off by initializing two variables, m and p, to zero.
The we go into a for loop that encompasses the rest of the code, except for a print statement at the end. We initialize a loop variable, i, which acts as a counter for the for loop, it starts at a (14) and increments by one for every iteration of the for loop until it reaches 17.
So, t is initialized to i, 14, in the first iteration. s is initialized to 0. Now we enter into the while loop, with a test condition that the loop will continue until t > 0 gets evaluated to false. Right now, 14 > 0, so we enter into the while loop.
1) 14 % 2 gets evaluated and assigned to z, so z is initialized to 0. (if you're not familiar with the modulo operator, read about it here)
2) 0 + 0 gets evaluated and assigned to s, so s still equals 0
3) t / 2 is evaluated and assigned to t, so t now equals 7.
7 is still > 0, so we're still in the while loop.
1) z = 7 % 2 = 1
2) s = 0 + 1 = 1
3) t = 7 / 2 (integer division in programming) = 3
3 > 0, still in while loop
1) z = 3 % 2 = 1
2) s = 1 + 1 = 2
3) t = 3 / 2 = 1
1 > 0, still in while loop
1) 1 % 2 = 1
2) s = 2 + 1 = 3
3) t = 1 / 2 = 0
0 > 0 is false, so we're finally out of the while loop.
Now we get to the if statement. If the test condition 's > m' is evaluated to true, then the entire block will get executed.
In this iteration of the for loop, s is now 3, and m is unchanged, zero. So, we enter the block, and assign s to m, so m is now 3, and we assign the loop variable i to p, so p is now 14.
That is how to step through an iteration of the for loop. You can repeat that the three more times necessary in order to determine the value of p after the for loop returns control to the main block.
The final answer is:
The first step in solving tasks such as these is to make sure you understand basic control structures in programming languages. For loops, while loops, if-then, if-then-else. Once you understand how these work, then stepping through small programs such as this becomes an exercise in maintaining your place as you step through.
For this program, we start off by initializing two variables, m and p, to zero.
The we go into a for loop that encompasses the rest of the code, except for a print statement at the end. We initialize a loop variable, i, which acts as a counter for the for loop, it starts at a (14) and increments by one for every iteration of the for loop until it reaches 17.
So, t is initialized to i, 14, in the first iteration. s is initialized to 0. Now we enter into the while loop, with a test condition that the loop will continue until t > 0 gets evaluated to false. Right now, 14 > 0, so we enter into the while loop.
1) 14 % 2 gets evaluated and assigned to z, so z is initialized to 0. (if you're not familiar with the modulo operator, read about it here)
2) 0 + 0 gets evaluated and assigned to s, so s still equals 0
3) t / 2 is evaluated and assigned to t, so t now equals 7.
7 is still > 0, so we're still in the while loop.
1) z = 7 % 2 = 1
2) s = 0 + 1 = 1
3) t = 7 / 2 (integer division in programming) = 3
3 > 0, still in while loop
1) z = 3 % 2 = 1
2) s = 1 + 1 = 2
3) t = 3 / 2 = 1
1 > 0, still in while loop
1) 1 % 2 = 1
2) s = 2 + 1 = 3
3) t = 1 / 2 = 0
0 > 0 is false, so we're finally out of the while loop.
Now we get to the if statement. If the test condition 's > m' is evaluated to true, then the entire block will get executed.
In this iteration of the for loop, s is now 3, and m is unchanged, zero. So, we enter the block, and assign s to m, so m is now 3, and we assign the loop variable i to p, so p is now 14.
That is how to step through an iteration of the for loop. You can repeat that the three more times necessary in order to determine the value of p after the for loop returns control to the main block.
The final answer is:
"There remain four irreducible objections to religious faith: that it wholly misrepresents the origins of man and the cosmos, that because of this original error it manages to combine the maximum servility with the maximum of solipsism, that it is both the result and the cause of dangerous sexual repression, and that it is ultimately grounded on wish-thinking." ~Christopher Hitchens, god is not Great
PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---
PM me your email address to join the Slack chat! I'll give you a taco(or five) if you join! --->There's an app and everything!<---