CSC161 2010F Imperative Problem Solving

Laboratory: More Conditionals

Summary: In this laboratory, you will work with a variety of kinds of conditionals in C.



a. Create a directory for this lab. I'd suggest CSC161/Labs/MoreConditionals.

b. In that directory, create our standard Makefile.


Exercise 1: Max

Here is a procedure that returns the larger of two integers.

max (int a, int b)
  if (a > b)
    return a;
    return b;
} // max

Rewrite this procedure using the conditional expression. That is, the body of your revised max procedure should have only one line, a return statement which uses a conditional expression to determine what to return.

Exercise 2: A Confused Conditional

Suppose we've decided to represent information on a student by using bit fields in an integer. The last two bits represent the student's year, with 00 representing freshling, 01 representing wise fool, 10 representing subordinate, and 11 representing elder.

Consider the following procedure that is intended to print out the textual version of the fields.

student_year (int student)
  int year = student & 0x3;
  switch (year)
      case 1:
        printf ("freshling");
      case 2:
        printf ("wise fool");
      case 3:
        printf ("subordinate");
      case 4:
        printf ("elder");
    } // switch
} // student_year

There is a flaw in the design of this procedure. Identify and fix the flaw. (Hint: You should incorporate the procedure in a program and try different inputs.)

Exercise 3: Speed Dial

Here is a sample procedure that takes as input a single digit and returns a string representing a phone number. (This procedure might be used in a quick-dial feature for a phone)

char *
speed_dial (int digit)
  if (digit == 0)
    return "555-555-5555";
  else if (digit == 1)
    return "555-555-5551";
  else if (digit == 2)
    return "555-555-1555";
  else if (digit = 3)
    return "555-555-5511";
  else if (digit = 4)
    return "555-555-1155";
  else if (digit == 5)
    return "555-555-1111";
  else if (digit == 6)
    return "555-555-1511";
  else if (digit == 7)
    return "555-555-5555";
  else if (digit == 8)
    return "555-555-5551";
  else if (digit == 9)
    return "555-555-5555";
    return "";
} // speed_dial

a. There is a significant error in this code. Identify the error and correct it.

b. Rewrite the procedure using a switch statement.

c. What advantages and disadvantages do you see from using a switch statement rather than a series of nested conditionals.

d. You may note that the same number is returned for some different inputs. Rewrite your switch statement to take advantage of this similarity.

Exercise 4: Counting Vowels

Write a program that reads characters from input until it hits EOF and prints out the following information:

For example, if the input is The Answer Is 42, you would report four vowels (e, A, e, I) and two capital vowels.

Be prepared to discuss your solution.

Exercise 5: Adding Numbers

Consider the following program taken from (I've reformatted it to match GNU coding standars.)

 * Program name is "conditional_exp_op_example".
 * This program uses the conditional expression to
 * see if the user wants to continue adding numbers. 

#include <stdio.h>
main (void)
  int a, b, c, d, again, total; 
  char answer;  
  printf ("\n"); 
  again = 1; 
  while (again) 
      printf ("Enter four numbers separated by spaces that\n"); 
      printf ("you want added together: "); 
      scanf ("%d %d %d %d", &a, &b, &c, &d); 
      fflush (stdin); 
      total = a + b + c + d; 
      printf ("\nThe total is: %d\n", total); 
      printf ("Do you want to continue ? "); 
      scanf ("%c", &answer); 
      again = (answer == 'y' || answer == 'Y') ? 1 : 0; 
    } // while

  return EXIT_SUCCESS;
} // main

Why might your professor think this is a bad example of conditional expressions?

For Those With Extra Time

If you find yourself with extra time, work on the exam.



Sunday, 26 September 2010 [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 Sun Sep 26 22:50:58 2010.
The source to the document was last modified on Sun Sep 26 22:50:54 2010.
This document may be found at

Samuel A. Rebelsky,