DAY15
Thursday, 14 June 2012


Today is a LAB day.

HOMEWORK #4 is due today. You should submit it to Detelina by email.

We have a LAB #5 to be done during class time, for credit. When you have completed the lab, please let Detelina know so that she can approve and record your work.


LAB#5:

Here is a sequence of numbers:

        3, 10, 5, 16, 8, 4, 2, 1
      
The sequence is created by choosing a starting number (in this case 3) and then repeatedly applying the following rule:
  1. if the number is 1, stop.
  2. if the number is even, divide it by two;
  3. if the number is odd, multiply by 3 and add 1;
Look at the sequence that starts with 3 and make sure you understand how the rules apply.

Let's call the starting number for the sequence "the seed", and symbolize it by s. Write a program that accepts a positive integer s from the user. Your program should then apply the rules to s repeatedly, until s is 1. Display each step of your results, giving the step number and current value. The very first output line should give the step number 0 and the seed value.

For the seed of 3, your output should look like this:

        0 3
        1 10
        2 5
        3 16
        4 8
        5 4
        6 2
        7 1
      

Check out the behavior of the program for seed values between 5 and 20. The number of steps seems to vary a great deal.

Test your program using the seed of 9, which should take 19 steps.

When you think your program is working, try it on the seed value of 27.

Run your program for the seed value of 27, and save your program output to a file, and plot it.

        ./a.out > output.txt

        gnuplot
          set style data linespoints
          plot "output.txt" using 1:2
          quit
      
Watch out though! If your program prints a message asking you to enter the seed value, that message will end up in "output.txt", which will confuse gnuplot. An easy fix is just to edit the output file and delete that line.

When you have your plot ready, show Detelina.


HOMEWORK #5 (must be turned in by next Thursday):

Homework 5.1: what day of the week is June 20, 2012? Write a program which will accept any day in June, 2012, that is, a number d between 1 and 30, and which will determine the day of the week and print out a message like this:

        June 20 2012 is a Wednesday.
      
(George: You are free to print this as 20 June 2012, or any other convention that makes sense!) Your program must refuse to accept illegal input. Your program must NOT consist of 30 "if" statements that check each day separately.

Test your program by showing what it does when you give the input "22" and when you give "39".

Homework 5.2: Write a program that "considers" each integer from 1 to 100. A typical line of output will simply contain that integer. However, if the integer is divisible by 5, the program should print "Quint". If the integer contains the digit "3" anywhere, the program should print "Trip". If the integer is divisible by 5 AND contains a digit "3", print "TripQuint".

Lines 20 through 30 of your output should look like this:

        Quint
        21
        22
        Trip
        24
        Quint
        26
        27
        28
        29
        TripQuint
      
There are intelligent and unintelligent ways of solving this problem. Be intelligent.

Homework 5.3: How many days since January 1st?. Write a program that accepts a month and day from the user, and prints out the number of days between January 1st, 2012, and the date. You may assume that the date is always a legal date between January 1st and December 31 of this year. If the user types in 1 1, the answer should be 0. If the user types in 12 31, the answer should be 365 (because there are 366 days this year.)

You can solve this problem by "counting". That is, you can start at day 1, month 1, and compute the next day, then the next day and so on, until your date matches the user's date. The number you need is the count of how many times you had to go to the next day.

You can also find the answer a little faster by realizing that the months that are completely past are easy to process.

If you are comfortable using a function, you can copy the file "mdays.c" from this page and use it to help you. It assumes the year is 2012, and is used like this:

        days = mdays ( m );
      
and returns the number of days in any month (29, 30 or 31).

Test your program by asking for the number of days between January 1 and June 21. (This should come out to be 172 days)

For each of your homework programs, turn in a copy of the program, and a copy of the output.


Programs we might discuss:


Last revised on 14 June 2012.