[Previous Homework] [Next Homework]

hw07 - Conditional Statements 1


From Linux on the GL machine:
  • Write a C program (see below) and save it in a file called dectobin.c in your ./cmsc104/hw07 directory

  • Compile the program:
    gcc -o dectobin dectobin.c

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

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


Using the dectobin.c code as a template, create a program that does the following:

  • Create a function name DecToBin that does the following:
    • Prompt the user to enter a number between 0 and 1024
    • Echo the value back to the user
    • Check to see if the value is in range. If not, print an error message and return -1 from the function to indicate failure
    • Convert that decimal value to it's equivalent in binary
      • Divide the decimal number by 210 (1024)
      • If the result is 0
        • print a 0 (leave out the \n newline character)
      • If the result is 1, print a 1 (leave out the \n newline character)
        • print a 0 (leave out the \n newline character)
        • subtract 1024 from the original number
    • Do this for all the other powers of 2, working your way back from 29 (512)
    • print a \n newline character
  • Call the function from main()
  • For main()'s return statement, return the same value that was returned from the function dec


Make sure that you follow all the coding standards