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
hw07and submit using the204subcommand. - This is a written homework - be sure to turn in a hard-copy
of your completed assignment before the deadline. Use the
codeprintcommand to print out your code and turn that in as well.
Assignment
-
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 allSkimmed itRead someRead all -
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 allSkimmed itRead someRead all -
For each of the code snippets below, circle expression, statement, or neither as appropriate.
- expression
statement
neither
3+x - expression
statement
neither
int y; - expression
statement
neither
3 + x / - expression
statement
neither
y = 3 - expression
statement
neither
y = x + 5;
- expression
statement
neither
-
For each of the collowing, do two things:
circle the correctly
parenthesized version of the expression and circle
"precedence" or "associativity" as appropriate.
x + y * zevaluates as(x + y) * zORx + (y * z)
due to: precedence associativityx > y + zevaluates as(x > y) + zORx > (y + z)
due to: precedence associativityx = y = 1evaluates as(x = y) = 1ORx = (y = 1)
due to: precedence associativity
- Explain why
1 < x < 10always returns1no matter whatxis. -
Write a program called
userpass.cthat 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$./userpassUsername:supePassword:beatarmyyesroche@ubuntu$./userpassUsername:rochePassword:iloveemacsnoThe only username/password combinations you should accept are the following:
Username Password supebeatarmybbqbrisket(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.cusing204subcommand. In addition, use thecodeprintcommand to make a PDF of your file, print it out on paper, and include that with what you turn in (stapled together). -
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? 703 no no no not leap year 704 yes no no leap year 700 yes yes no not leap year 800 yes yes yes leap year Write a program called
leap.cto 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$./leapWhat is your favorite leap year?703703 is not a leap year!What is your favorite leap year?19001900 is not a leap year!What is your favorite leap year?1996Yes, 1996 was a good year.Submit the file
leap.cusing204subcommand. In addition, use thecodeprintcommand to make a PDF of your file, print it out on paper, and include that with what you turn in (stapled together).