Homework 1
Due Monday, September 16, 2002
Solutions

A.  Count in binary from 1101101 to 1110010

    1101101, 1101110, 1101111, 1110000, 1110001, 1110010

B.  Convert 1101010 to decimal.  Show your work.

    The first row below shows the values for the bit in that position.
    The second row shows the binary number we want to convert:
    64   32   16   8    4    2    1
    1    1    0    1    0    1    0
    In decimal, we have a total of 
    64 + 32 + 0 +  8  + 0  + 2  + 0 = 106 
 
C.  Convert 47 (decimal) to binary.  Show your work.

    The largest power of 2 that fits in 47 is 32.  47-32 = 15.
    The largest power of 2 that fits in 15 is 8.  15-8 = 7
    The largest power of 2 that fits in 7 is 4.  7-4 = 3
    The largest power of 2 that fits in 3 is 2.  3-2 = 1
    We need 1's in the 32 position, 8 position, 4 position, 2 position and 1 position
    Again, the first row below shows the values for the bit in that position,
    and the second row shows the resulting binary number.
    32   16   8    4    2    1
    1    0    1    1    1    1
    The answer is 101111.

D.  What ways are there of testing an algorithm for correctness?

    According to the textbook, page 12, you can sometimes test an algorithm;s
    correctness by using sample data, and at other times, you might need
    to perform some mathematical analysis to test the algorithm's correctness.

1. a. True; b. False; c. False; d. False; e. False; f. True; g. False;
   h. True; i. True;  j. True

7. Every computer directly understands its machine language. Therefore,
   for the computer to execute a program written in a high level language,
   the high level language program must be translated into the computer's
   machine language.

9. A well-analyzed problem leads to a well designed algorithm. Moreover,
    in a program that is well-analyzed it is easier to make modifications,
    and spot and fix errors.

10. To find the weighted average of four test score, first you need to know
    each test score and its weight. Next, you multiply each test score with its
    weight and then add these numbers.
    1.  Get testScore1, weightTestScore1
    2.  Get testScore2, weightTestScore2
    3.  Get testScore3, weightTestScore3
    4.  Get testScore4, weightTestScore4
    5.  average = testScore1 * weightTestScore1 +
              testScore2 * weightTestScore2 +
              testScore3 * weightTestScore3 +
              testScore4 * weightTestScore4

11. To find the average mile per gallon, first we need to find the total
    distance traveled; which can be computed by subtracting starting odometer
    reading from the ending odometer reading. Next you find the total amount
    of gasoline used by adding the amount gasoline used each day. To find
    average mile per gallon, divide the distance traveled by the total amount
    of gasoline used. Therefore,
    1.  Get startingOdometerReading
    2.  Get endingOdometerReading
    3.  distanceTraveled = endingOdometerReading - startingOdometerReading;
    4.  Get gasUsedDay1, gasUsedDay2, gasUsedDay3, gasUsedDay4, gasUsedDay5
    5.	totalGasUsed = gasUsedDay1 + gasUsedDay2 + gasUsedDay3 + 
		    	  gasUsedDay4 + gasUsedDay5
    6.	averageGasPerMile = distanceTraveled / totalGasUsed