[Previous Homework] [Next Homework]

hw08 - Intro to While Loops


From Linux on the GL machine:
  • Copy your dectobin.c program in your ./cmsc104/hw07 directory into your ./cmsc104/hw08 and rename it to dectobinWhile.c

  • Modify the code (see below)

  • Compile the program:
    gcc -lm -o dectobinWhile dectobinWhile.c

  • Run the program to make sure it works:
    ./dectobinWhile

  • Use the SUBMIT utility turn in the homework.
    submit cs104_grasso hw07 dectobinWhile.c dectobinWhile


Modify the program so that it does the following:

  • Add #include <math.h> at the top of your source file

  • Use #define to create an IDENTIFIER for the maximum and minimum allowable input values
    • Make sure to use those identifiers where ever necessary in your code
    • There should not be any hard-coded values left in your code.

  • Prompt the user to enter a positive integer value between 0 and 65,000
    • Read an input number from the user.
    • Repeat the request until the user has entered a correct value.

  • Based on the decimal value entered by the user, print out the binary equivalent.
    • Create a variable called exponent and initalize it to 16.
    • Create a variable called powerOf2
    • Program a while loop to iterate down over all the powers of 2, starting with 216
      • Set the powerOf2 variable equal to pow(2,exponent) to calculate 2 to that power.
      • Set the result variable to the user's number divided by that power of 2 to get the binary digit in that position.
      • Print out the digit
      • If result is 1, subtract powerOf2 from the user's number.
      • Subtract 1 from the exponent.


Make sure that you follow all the coding standards