Lab03, CISC105 Summer 2005

Directions

Programs

  1. (30) Using a loop and the mod operator, create a calendar for a month.

    Example Run:

    $ ./a.out 
    Enter the start day of the calendar (Sunday=0, Monday=1, etc.): 2
    Enter the number of days in the calendar: 31
    
     Sun Mon Tue Wed Thu Fri Sat
               1   2   3   4   5
       6   7   8   9  10  11  12
      13  14  15  16  17  18  19
      20  21  22  23  24  25  26
      27  28  29  30  31
    
    
  2. (40) It's hard to get good help. You have to be careful with who you hire to make sure they give the customers correct change. (If they give too much change, they're costing you money; if they give too little change, they are ripping off the customer, and that will hurt business.) To help your workers, you'll write a program that computes how much change to give a customer.

    Example Program Run:

    How much was the bill?  14.27
    How much did the customer give you? 20.00
    
    The total change is $5.73.
    Give the customer 
    $5
    2 quarters
    2 dimes
    3 pennies
    
    Note that since the ideal change does not include nickels, no information about nickels is printed.

    Don't forget to handle "unusual" circumstances. (What would those situations be? Make sure you show tests for these situations.)

  3. (60) The first prime number is 2. Write a program that takes a user input integer larger than 2 and determines if it is prime. (Enforce the constraint on the input.)

    How can you determine if a number is prime? What does it mean to be prime? What math operator will be helpful?

    Use a while loop and count from 2 to the number, checking each number along the way to see if it is a factor of the input. EACH TIME through the loop, print the number you tested; when you find a factor, set a flag to make the loop stop and then print an appropriate message with both factors, as in: 90 is not prime, 90 is a product of 3 and 30.

    Now put the program inside a function. Leave the print statement for the tested numbers so the TA can see your work as it runs, but move the other I/O into main(). Write a loop around the function call and I/O so that a user can check as many primes as she likes, then terminate the program by entering -1. Note: By moving the program into a function, you have another alternative besides setting a flag to change the program's control flow and stop the loop after you find a factor.

    That is not very attractive output, is it? What are two tricks you can think of to reduce the number of possible factors that are tested? Implement each optimization for 5 points extra credit each.

    Example runs:

    Enter a number: 17
                                                
    Testing 17:                                                                   
            2 is not a factor of 17
            3 is not a factor of 17
            4 is not a factor of 17 
            ...  
            16 is not a factor of 17  
    17 is prime!  
    
    Also, you can stop looping when you find the first factor of a number.
    Enter a number: 9
    
    Testing 9:
          2 is not a factor of 9
    9 is not a prime: 3 and 3 are factors of 9.
    

    So we don't get pages of output, you can limit your test input to numbers less than 200.

  4. (40) Copy power.c that we wrote in class.

    Add a function power_recursive(double n, int exponent) that returns a double. It is very short, consisting of an if and an else. Use an if statement to test if the exponent is zero. If it is, return the correct answer.

    If exponent is not zero, return the product of n and the value returned from a call to power_recursive(n, exponent - 1). (Notice how we can use a function as part of another expression.)

    This kind of function is called ``recursive'', meaning that it calls itself. Place a call to the function in main, and test it. After you are convinced it works properly for all whole number exponents, put the function call inside a loop and add a prompt so that the user can calculate as many powers as she wants before exiting the program. Add a prototype if you haven't already.

    Now add another call in main to the math library function pow(n, exponent) so that your main displays the result from your function and the math library function next to one another for comparison. Display appropriately.

    Your comments should be more complete and descriptive than the sparse comments in the course's version of the program.

You should have a total of 4 programs named lab03.1.c to lab03.4.c. Make a single script file (see lab00 for the instructions) where you cat, compile, and run each one in its final form (if it didn't compile, don't run it in the script - mark the place in the printed script file with a marker so it stands out).

Note: Cat, compile, and run each program in order! Do not cat all programs, then compile, etc.

Execute your program multiple times to show that you tested the program well.

Practicing UNIX

In class, we talked about using "." and ".." as shorthands. "." means "this directory." ".." means the parent directory.

As part of your script file, in your home directory, list all your files. Change into a subdirectory. Do an ls, an ls ., and an ls ..

Submission

Email the tar file to Gang by midnight on Wednesday (see lab00 for directions). Give the paper version to Gang at the beginning of your next lab. (10 points for each submission.)

Grading

Grading is as noted above for each program, 10 points for demonstrating your use of UNIX plus 10 points each for the electronic and paper submissions, for a total of 200 points.


Some problems adapted from Terry Harvey and Phill Conrad.