SI 204 Spring 2017 / HWs


This is the archived website of SI 204 from the Spring 2017 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

Homework 07: Loops practice

Name: _____________________________________________ Alpha: ___________________

Describe help received: ________________________________________________________

  • Due before class on Friday, January 27
  • This homework contains code to be submitted electronically. Put your code in a folder called hw07 and submit using the 204sub command.
  • This is a written homework - be sure to turn in a hard-copy of your completed assignment before the deadline. Use the codeprint command to print out your code and turn that in as well.

Assignment

  1. Circle one to indicate how you did for the reading assignment from Homework 5 before class on Monday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  2. Circle one to indicate how you did for the reading assignment from Homework 6 before class on Wednesday:

    How carefully did you complete the reading? (Circle one)

    Not at all
    Skimmed it
    Read some
    Read all
  3. For each of the code snippets below, circle expression, statement, or neither as appropriate.
    1. expression statement neither 3+x
    2. expression statement neither int y;
    3. expression statement neither 3 + x /
    4. expression statement neither y = 3
    5. expression statement neither y = x + 5;
  4. For each of the collowing, do two things: circle the correctly parenthesized version of the expression and circle "precedence" or "associativity" as appropriate.

    1. x + y * z evaluates as (x + y) * z OR x + (y * z)
      due to: precedence associativity
    2. x > y + z evaluates as (x > y) + z OR x > (y + z)
      due to: precedence associativity
    3. x = y = 1 evaluates as (x = y) = 1 OR x = (y = 1)
      due to: precedence associativity
  5. Explain why 1 < x < 10 always returns 1 no matter what x is.
  6. Write a program called userpass.c that reads in a username and password from the user, and then outputs "yes" or "no" depending on whether it's a valid username/password combination. Note, you should always read in a password, even if you know the username isn't right. For example:
    roche@ubuntu$ ./userpass
    Username: supe
    Password: beatarmy
    yes
    roche@ubuntu$ ./userpass
    Username: roche
    Password: iloveemacs
    no

    The only username/password combinations you should accept are the following:

    UsernamePassword
    supebeatarmy
    bbqbrisket

    (And of course, unlike a real application, the password will be displayed on the screen as it is typed in to your C program.)

    Submit the file userpass.c using 204sub command. In addition, use the codeprint command to make a PDF of your file, print it out on paper, and include that with what you turn in (stapled together).

  7. A year is a leap year if it is evenly divisible by 4, Except that years that are evenly divisible by 100 are not leap years unless they are evenly divisible by 400. Clear? For example:
    Year divisible by 4? divisible by 100? divisible by 400? Is leap year?
    703nonononot leap year
    704yesnonoleap year
    700yesyesnonot leap year
    800yesyesyesleap year

    Write a program called leap.c to find the user's favorite leap year. Keep asking again and again until they enter a valid leap year. Here's how your program should work:

    roche@ubuntu$ ./leap
    What is your favorite leap year? 703
    703 is not a leap year!
    What is your favorite leap year? 1900
    1900 is not a leap year!
    What is your favorite leap year? 1996
    Yes, 1996 was a good year.

    Submit the file leap.c using 204sub command. In addition, use the codeprint command to make a PDF of your file, print it out on paper, and include that with what you turn in (stapled together).