CMSC18 Lab Exer 3 - Statements Expressions and Operators
CMSC18 Lab Exer 3 - Statements Expressions and Operators
Name: Score:
Laboratory Section: Date:
Objectives
At the end of this exercise, you should be able to:
1. Write C programs with operators and expressions and understand operator
precedence.
Total: 55 pts
Activity 1
Type or write your answers for Activity 1 and submit the document in PDF format.
1. Write a code that stores the value of a variable x to a variable y if, and only if, the
value of x is from -10 to 10. Else, do nothing. (2 pts)
a. (1 + 2 * 3 < 5)
b. 10 && 3 * 3 – (1 + 2) * 2 || 5
c. ((1 + 2) * 3)
d. (5 == 5)
3. Consider this program. Explore the program – provide input and analyze what
happens.
1. #include<stdio.h>
2.
3. #define SECS_PER_MIN 60
4. #define SECS_PER_HR 3600
5.
6. unsigned seconds, minutes, hours;
7. unsigned secs_left, mins_left;
8.
9. int main()
10. {
11. printf(“Enter number of seconds (<65000): ”);
12. scanf(“%u”, &seconds);
13.
14. hours = seconds / SECS_PER_HR;
15. minutes = seconds / SECS_PER_MIN;
CMSC 18: Computer Programming I
4. If x=4, y=6, and z=2, determine the evaluated value of each of the following:
a. x != y - z
b. !z == 1
c. y && z > y
5. Rewrite this code so that it will use a single if-statement only (3 pts):
if ( x < 1 ) {
printf (“hello!”);
}
if ( x > 10 ) {
printf (“hello!”);
}
Rewritten code:
6. Rewrite this code so that it will use a single if-statement only (3 pts):
if ( x < 1 )
if ( x > -5 )
printf (“hello!”);
Rewritten code:
Activity 2
Archive your C source code files in a .zip file with “CMSC18LabExer3_surname” as the
filename and submit this together with the document containing your answers to Activity 1.
Each C source code file should contain your name and student number as a comment at the
beginning of the code. Also include an honor statement signifying your honesty in the
accomplishment of the exercises. You may use this format: “Upon my honor, I have
accomplished this laboratory exercise relying upon my own honest efforts without cheating.”
1. (15 pts) AreaPerimeter: Write a C program that computes and displays the area and
the perimeter of the rectangle from the length and width provided by the user, and
the area and the circumference of the circle from the radius provided by the user.
Use the value 3.1415 for the PI. The process should proceed only when the inputs
are greater than 1. Your program should ask the user for the values of length, width,
and radius.
2. (15 pts) EasterSunday: Write a C program that determines and prints the date of the
Easter Sunday for a given year. You can compute the date for any Easter Sunday
from 1982 to 2048 as follows (all are integers):
a is year % 19
b is year % 4
c is year % 7
d is ( 19 * a + 24 ) % 30
e is ( 2 * b + 4 * c + 6 * d + 5 ) % 7
Easter Sunday is March ( 22 + d + e )
Note that the last expression can make it possible to generate a date in April. Adjust
the date accordingly. Your program should ask the user for the year and then print
the date (day Month year format) of the Easter Sunday for the given year.