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 the204sub
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
-
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 * z
evaluates as(x + y) * z
ORx + (y * z)
due to: precedence associativityx > y + z
evaluates as(x > y) + z
ORx > (y + z)
due to: precedence associativityx = y = 1
evaluates as(x = y) = 1
ORx = (y = 1)
due to: precedence associativity
- Explain why
1 < x < 10
always returns1
no matter whatx
is. -
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:
Username Password supe
beatarmy
bbq
brisket
(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
using204sub
command. In addition, use thecodeprint
command 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.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
using204sub
command. In addition, use thecodeprint
command to make a PDF of your file, print it out on paper, and include that with what you turn in (stapled together).