Problem of the Week Problem C and Solution A Solo Trio

Problem

Sandip is writing a computer program that will play sounds that imitate a piano, cello, and violin. He has commands that start or stop the sound of each instrument, as well as a command to wait for a given period of time while the instruments play. He uses these commands to write the following program.

REPEAT 10 times:

• start(piano)

• wait(3sec)

• start(cello)

• start(violin)

• wait(5sec)

• stop(violin)

• wait(2sec)

• start(violin)

• stop(piano)

• wait(2sec)

• stop(violin)

• stop(cello)

1. How long does it take for the program to execute completely from start to finish? Assume that Sandip’s computer is so fast that it doesn’t take any time at all to execute each command.

2. Which instruments are playing exactly $$33$$ seconds after the program starts running?

Solution

1. First we will calculate how many seconds it takes to go through the repeat block once. Since $$3+5+2+2=12$$, it takes $$12$$ seconds to go through the repeat block once. Since we go through the repeat block $$10$$ times, it takes $$12 \times 10=120$$ seconds (or $$2$$ minutes) to execute the program completely from start to finish.

2. There are two solutions for this part.

Solution 1:

We know from part (a) that it takes $$12$$ seconds to execute the repeat block once. Since $$12\times 2+9=33$$, that means after $$33$$ seconds, the program will have executed the repeat block twice, and be $$9$$ seconds into its third pass.

If we look at the code in the repeat block, we can see that all three instruments are started in the beginning and stopped at the end. This tells us we don’t need to consider the first two times through the repeat block and can focus only on which instruments are playing $$9$$ seconds into the repeat block. We will walk through the code until we reach $$9$$ seconds.

start(piano)
wait(3sec)
Thus, after $$3$$ seconds, only the piano is playing. We will look through the next part of the code.

start(cello)
start(violin)
wait(5sec)
After $$3+5=8$$ seconds, the piano, cello, and violin are all playing. We will look through the next part of the code.

stop(violin)
wait(2sec)
The violin stops, and then the program waits for $$2$$ seconds. Since $$8+1=9$$ seconds, that means $$9$$ seconds into the repeat block, the program is waiting. At this point, only the piano and cello are playing.

Therefore, $$33$$ seconds after the program starts running, the piano and cello are the only instruments playing.

Solution 2:

We can walk through the first $$33$$ seconds of the code and record which instruments are playing at each second. We will let “P” represent piano, “C” represent cello, and “V” represent violin and summarize our findings in a table.

Seconds Elapsed Instruments Playing
$$1$$ P
$$2$$ P
$$3$$ P
$$4$$ P C V
$$5$$ P C V
$$6$$ P C V
$$7$$ P C V
$$8$$ P C V
$$9$$ P C
$$10$$ P C
$$11$$ C V
$$12$$ C V
$$13$$ P
$$14$$ P
$$15$$ P
$$16$$ P C V
$$17$$ P C V
$$18$$ P C V
$$19$$ P C V
$$20$$ P C V
$$21$$ P C
$$22$$ P C
$$23$$ C V
$$24$$ C V
$$25$$ P
$$26$$ P
$$27$$ P
$$28$$ P C V
$$29$$ P C V
$$30$$ P C V
$$31$$ P C V
$$32$$ P C V
$$33$$ P C
$$34$$ P C
$$35$$ C V
$$36$$ C V

Looking at the table, we can see that $$33$$ seconds after the program starts running, the piano and cello are the only instruments playing.