(January 9, 2018 at 2:08 pm)FlatAssembler Wrote: Hey, guys! I am from Croatia, and, when I finish my high school, which will be in around 6 months, I will have to go to the maturity test. I have to choose the subjects I will take with respect to the university I am planning to attend. I am planning to study informatics, so I have to take informatics at my maturity test. However, I didn't have informatics as a subject at all in my high-school. In the maturity test in informatics, there are some tasks about programming, but they have little to do with what I happen to know about programming.
Here is an example from the last-year maturity test in informatics:
25. What will the following program (in pseudocode) output if a=14 and b=17?
Code:m := 0;
p := 0;
for i := a to b do
{
t := i;
s := 0;
while t > 0 do
{
z := t mod 2;
s := s + z;
t := t div 2;
}
if s > m then
{
m := s;
p := i;
}
}
print (p);
Do you have any idea how to solve that task? I don't. Last year, there were around ten of such tasks (plus the tasks from Word, Excel and the binary-system-related things) and they had only 90 minutes to solve that all.
Can you please explain me how to solve that task (and similar ones) in reasonable time? Calculators aren't allowed (and, as far as I can see, wouldn't help much).
The output of this will depend on the specifics of the rules in your pseudocode. Since a=14 and b=1, the for statement will either 1. not run at all (since a>b) or run with i starting at 14 and decreasing down to b=1. My guess is that the first is intended. If so, then the whole for loop is skipped, leaving p=0, which is printed.
Otherwise, the internal while loop counts the number of initial 1s in the binary representation of i and the following if statement finds the number with the largest such sequence. The number with that sequence (between 14 and 1) is 7, so p=7, which is printed.
So, the answer depends on the grammar for your for statements.