CPSC 110-08: Computing on Mobile Phones
Spring 2012

Quiz 2

This set of questions is about number conversions. Show your work.

  1. Convert the binary value 1010 0110 to decimal.
    1×27 + 1×25 + 1×22 + 1×22 = 128 + 32 + 4 + 2 = 166


  2. Convert the hexadecimal value A9 to decimal.
    10 × 161 + 9 × 160 = 10 × 16 + 9 = 169


  3. Convert the decimal value 143 to binary.
    143/2 = 71 r1
    71/2 = 35 r1
    35/2 = 17 r1
    17/2 = 8 r1
    8/2 = 4 r0
    4/2 = 2 r0
    2/2 = 1 r0
    1/2 = 0 r1     1000 1111


  4. Convert the decimal value 143 to hexadecimal.
    143/16 = 8 r15 = F
    8/16 = 0 r8     8F



Instructions: The next two problems ask you to read a Pseudocode or App Inventor algorithm. Trace through each of them by hand, keeping track of the variables, to determine the answer to the questions.

  1. Suppose you have a global variables named X, Y and N. What value would Y have after the following Pseudocode segment? Explain.
    Set X to 33
    Set N to 3
    Set X to X / N       X is set to X/N = 33/3 = 11 and since 11 > 10, Y will be set to "Hello"
    If X > 10 then do:
       Set Y to "Hello"
    else-do:
       Set Y to "Ciao"
    
  2. Consider the App Inventor procedure on the right.
    1. Suppose the procedure was called 100 times. Approximately how many times would you expect the app to say "You win!". Explain.

      Since we are selecting from 10 values (0, 1, ..., 9) you would expect 9 to come up approximately 10% of the time. So "you win" will print around 10 times.




    2. Approximately how many times would you expect it to say "5". Explain.

      You would expect 5 to come up approximately 10% of the time. But in this example we are literally saying "N" (never a number). So the correct answer is "never". But this was a typo in the question, so an answer of 10% was also accepted as correct.




Instructions: The next problem asks you to write a Pseudocode algorithm. Model your answers after the Pseudocode algorithm in question 5.

  1. Suppose you have the global variables A, B, and Smallest and you're trying to store the smaller of their two values in Smallest. Write a Pseudocode if/else algorithm that will set the variable Smallest to the smaller of A or B. For example, if A = 3 and B = 4 then your algorithm should should set Smallest to A's value (i.e., 3). If A = 4 and B = 1, then Smallest should be set to B's value (i.e., 1). If you get the algorithm right, the case when A=B will take care of itself.

    
    If A < B then do:
       Set global Smallest to A
    else do:
       Set global Smallest to B