# Laboratory: Conditionals

Summary: In this laboratory, you will begin working with simple conditional statements in C.

Contents

## Preparation

a. Create a directory for this lab. I'd suggest csc161/labs/conditionals.

b. In that directory, create our standard Makefile.

## Exercises

### Exercise 1: Signing Numbers

a. Write a program, sign, that prompts the user for a number, determines the sign of the number, and then prints out a message about the sign of the number. For example

23.0 appears to be positive.
-123.2 appears to be negative.
0.0 appears to be zero.

Recall that you can use scanf to read in a number.

b. Experimentally determine whether there are any inputs that you would consider non-zero that your program considers zero.

### Exercise 2: The Two's Bit

Write a program that takes an integer as input (either on the command line or read after a prompt) and prints out whether or not the two's bit is set.

### Exercise 3: Nicknames

Pick five friends with nicknames (real or invented) and write a program that takes a nickname on the command line and prints out the corresponding full name.

% nickname SamR
SamR is a nickname for Samuel A. Rebelsky
% nickname Herbie
Herbie is a nickname for Herbert Q. Namsak
% nickname RayK
RayK does not appear to be a nickname for anyone I know.

### Exercise 4: Inverse Operations

Consider the following program.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/**
* Fun with approximation.
*/
int
main()
{
double d = 2.0;
double root = sqrt(d);
double squared = root * root;
printf ("The square root of %lf is %lf\n", d, root);
printf ("%lf squared is %lf\n", root, squared);
if (d == squared)
printf ("Yay!  The square of the square root of %lf is %lf.\n", d, d);
else
printf ("Boo!  The square of the square root of %lf differs from %lf.\n",
d, d);

return EXIT_SUCCESS;
} // main

The first two lines of output read

The square root of 2.000000 is 1.414214
1.414214 squared is 2.000000

a. What do you expect the third line of output to be?

c. Explain the answer you got.

d. How do you expect the output to change if you change the line that reads d=2 to read d=4?

f. Explain the answer you got.

### Exercise 5: Near Equality

As you discovered in your explorations with numbers, values of type double approximate real numbers. One problem with approximation is that a sequence of operations can lead to less and less good approximations. For example, you just discovered that if you square the square root of two, you do not get something exactly equal to two.

In response to this problem, many programmers write code that determines whether two values are approximately equal.

Write a program that reads in two real numbers and determines whether they are approximately equal. You may choose the metric for approximate equality, but it should be a sensible metric.

### Exercise 6: Simple Sorting

Write a program that reads in three integers and prints them out in order from smallest to largest. For example,

Enter a number: 42
Enter a number: 23
Enter a number: 32
In numerical order: 23 32 42

You might also take the three numbers on the command line.

## For Those with Extra Time

Rewrite the binary search example so that it does only one test.

This problem is closely based on Exercise 3-1 in K&R.

## History

Monday, 20 September 2010 [Samuel A. Rebelsky]

Tuesday, 21 September 2010 [Samuel A. Rebelsky]

Wednesday, 23 February 2011 [Samuel A. Rebelsky]

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Wed Feb 23 10:18:42 2011.
The source to the document was last modified on Wed Feb 23 10:18:40 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Labs/conditionals-lab.html.

Samuel A. Rebelsky, rebelsky@grinnell.edu