CS21 Week2 Tuesday In-Class Exercises

  1. Here is a review of some things from last week: C Basics Review.

  2. Create a week2 subdirectory in your cs21 subdirectory, and from your week2 subdirectory, make a copy of my round.c file:
    % cd                # remember you can use ls and pwd to figure out where you are in the directory tree
    % cd cs21
    % mkdir week2
    % cd week2
    % pwd                # should list /home/yourusername/cs21/week2
    
  3. Now open up the add.c program from last week in emacs or vi and we will go over some new things together using add.c as an example:
    % cp ~newhall/public/cs21/week1/add.c .   # grab a new copy of my add.c 
    % ls 					  # should list add.c
    % emacs add.c &				  # & means run emacs in the background
    or
    % vim add.c &
    

  4. Grab a copy of my round.c file:
    % pwd                	# should list /home/yourusername/cs21/week2
    
    % cp ~newhall/public/cs21/week2/round.c .
    
    % ls			# should list round.c 
    round.c
    

  5. There is a bug in this program, what is it (try different test cases, input values, to find the bug)? Can you fix it?

  6. Modify the program to sum 10 floats and round to the nearest integer. Can your program be easily modified to sum 100 floats? If not, think about how to re-design your solution so that changing the number of floats to sum would be trivial.

  7. Write a program that reads in a list of floats until the user enters the value -1 as a sentinel. At that point, the program should display the sum rounded to the nearest integer. Your program should be able to duplicate the following sample execution:
    This program sums a list of floats and 
    rounds the sum to the nearest integer.
    Enter -1 to signal the end of the list.
    ? 95.2
    ? 100.7
    ? 34.6 
    ? -1
    The closest int to their sum is 230