CSC 161 Grinnell College Spring, 2009 Imperative Problem Solving and Data Structures

# Laboratory Exercise on Conditionals in C

## Goals

This laboratory exercise provides practice with conditional (if) constructs in C.

## The VIM Editor

When you started C in this course, we introduced the the Emacs Text Editor. In this lab, you are asked to use the the VIM editor for your work.

After this lab, you are free to use whatever editor you wish for writing your C programs. However, you are strongly encouraged to use either emacs or VIM, since these have powerful capabilities that can help with C programming.

## Steps for this Lab

1. Do King, Exercise 5.1 (page 93).

2. Do King, Exercise 5.3 (page 94).

3. Do King, Exercise 5.5 (page 94). (Be sure you can explain your answer!)

4. Do King, Programming Project 5.5 (page 96).

5. Do King, Programming Project 5.9 (page 97).

6. Classifying Triangles:
[This problem is based on an exercise by Darrah Chavey, Beloit College.]

Write a C program that reads the lengths of three sides of a triangle and prints out the classification of the triangle as follows:

• invalid input: all sides must have lengths that are strictly positive
• not a triangle: the sum of any two sides must be greater than the length of the third
• scalene triangle: no two sides have equal length
• isosceles triangle: two sides (but not the third) are equal
• equilateral triangle: all three sides are equal

In this problem, you should assume that the input values are all numbers, although error checking may indicate some numbers are negative or zero.

7. A nonnegative integer K is divisible by a positive integer L, if the remainder after dividing K by L is zero.

Write a program that reads two integers, M and N, and determines if either is a divisor of the other.

Hint: A list of C's arithmetic operators is given in Section 4.1 of King (page 54).

8. If x is a nonnegative number, then its square root may be computed directly by the sqrt function that is defined in C's math.h library. However, if x is negative, then its square root is a complex number (y i), where y2=|x| — that is, y is a square root of the absolute value of x.

Write a program that reads a real number x and computes its square root (x could be either negative or nonnegative).

9. (Challenge Problem — Optional)
A Baby Sitting Problem: A baby sitter charges \$1.50 per hour until 9:00 pm (while the kids are still up), \$1.00 per hour between 9:00 pm and midnight, and \$1.25 per hour after midnight (since late night baby sitting interferes with morning classes).

Write a program that reads the sitter's starting time in hours and minutes and the ending time in hours and minutes and then computes the sitter's fee. Assume all times are between 6:00 pm and 6:00 am, and hours should be entered as being between 0 and 12 (inclusive). Hours outside the range of 0 to 12 should be considered invalid.

• The hour 6 should be considered as 6:00 pm, when it is entered as a starting time.
• The hour 6 should be considered as 6:00 am, when it is entered as an ending time.

The following table may clarify allowed time values for this problem.

Starting Starting Ending Ending StartingEnding
Hour Minutes Hour Minutes Time time
8 0 3 30 8:00pm 3:30am
6 0 0 45 6:00pm 12:45am
12 0 6 0 12:00am (midnight) 6:00am

```     http://www.walker.cs.grinnell.edu/courses/161.sp09/lab-if-c.shtml
 created 31 August 1998 last revised 25 January 2009  For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.