Computer Science 3300
Fall 2015
Section 001
Programming Assignment 1

Assigned: Wednesday, September 2
Due: Tuesday, September 15, 11:59pm

Table of contents

  1. Note about this assignment
  2. Background
  3. Assignment requirements
  4. Design issues and a development plan
  5. Compiling and testing your program
  6. Submitting your work


Note about This Assignment

This assignment is intended to familiarize you with C++, loops, functions and contracts.

It is important that you follow the instructions. Do not try to improve on the design described here. Read the entire assignment before you start working on it.


Background

Given any positive integer n, the hailstone sequence starting at n is obtained as follows. You write a sequence of numbers, one after another. Start by writing n. If n is even, then the next number is n/2. If n is odd, then the next number is 3n + 1. Continue in this way until you write the number 1.

For example, if you start at 7, then the next number is 22 (3 times 7 plus 1). The next number after 22 is 11. The hailstone sequence starting at 7 is

[7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1],

and it contains 17 numbers. The hailstone sequence starting at 6 is [6, 3, 10, 5, 16, 8, 4, 2, 1], and the hailstone sequence starting at 1 is [1].


Assignment Requirements

Functional Requirements

The functional requirements tell what the program is supposed to do when you run it.

Write a C++ program that reads a number n from the user (after giving a suitable prompt) and then tells the user the following information:

  1. the entire hailstone sequence starting at n, all on one line, with the numbers separated by spaces;
  2. the length of the hailstone sequence that starts with n;
  3. the largest number in the hailstone sequence that starts with n;
  4. the largest length of any hailstone sequence starting with a number from 1 to n.
  5. the starting number of the longest hailstone sequence starting with a number from 1 to n.

The output needs to be sensible and easy to read, not just numbers. For example, a session with this program might look as follows. Parts in black are written by the program. Parts in blue are typed by the user.

  What number shall I start with?  7
  The hailstone sequence starting at 7 is:
  7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
  The length of the sequence is 17.
  The largest number in the sequence is 52.
  The longest hailstone sequence starting with a number up to 7 has length 17
  The longest hailstone sequence starting with a number up to 7 begins with 7

Here is another example.

  What number shall I start with?  1
  The hailstone sequence starting at 1 is
  1
  The length of the sequence is 1.
  The largest number in the sequence is 1.
  The longest hailstone sequence starting with a number up to 1 has length 1
  The longest hailstone sequence starting with a number up to 1 begins with 1

And here is another.

  What number shall I start with?  8
  The hailstone sequence starting at 8 is
  8 4 2 1
  The length of the sequence is 4.
  The largest number in the sequence is 8.
  The longest hailstone sequence starting with a number up to 8 has length 17
  The longest hailstone sequence starting with a number up to 8 begins with 7

Nonfunctional Requirements

The nonfunctional requirements are additional requirements on how the program must be written, documented, etc., that are not directly concerned with what the program does when you run it.

The program is required to follow the coding standards for this course, which include the following.


Design issues and a development plan

The design of a program includes issues such as which functions it includes, how those functions are organized into modules, and what each function and module accomplishes. It normally does not include details of how each function works.

This section both describes the required design and suggests how to write the program. You are required to follow the design.

For this program, use loops. Do not use recursion. Use type int for all of the integers.

  1. Create a directory (folder) to hold assignment 1. Put all of the files for this assignment in that directory.


  2. Create a file called hailstone.cpp. Copy and paste the template into it. Edit the template. If you will use tabs, say where the tab stops are. (If you don't know, type a line of about 8 x's. Then, in the line beneath that line, type a tab. How many x's were skipped over?)


  3. Write a contract for a function that takes an integer value n and returns the number that follows n in the hailstone sequence. If you call this function next, then next(7) = 22 and next(22) = 11.

    Since there is no number that follows 1 in the hailstone sequence, this function requires its parameter n to be greater than 1. State that in the contract. You program must never compute next(1).

    Any time you want to get the next number in the sequence, you must use this function.


  4. Now write a C++ definition of the function that you have described in the preceding item. The C++ definition is called the implementation of the function.


  5. Write a main function that reads an integer n and shows the value of next(n). Test it on a few values to make sure that the next function works. Make sure that your tests, taken together, make use of every line that you have written.


  6. Write a contract, then an implementation, of a function that takes an integer n and writes the entire hailstone sequence starting at n. This function has a void return type.

    Modify your main function to so that it no longer shows next(n), but shows the hailstone sequence starting at n. Test it.


  7. Write a contract, then an implementation, of a function that takes an integer n and returns the length of the hailstone sequence starting at n. This function must not write anything.

    Modify your main function to so that it shows both the hailstone sequence and the length of the hailstone sequence starting at n Test it.


  8. Write a contract, then an implementation, of a function that takes an integer n and returns the largest value in the hailstone sequence starting at n. This function must not write anything.

    Modify your main function to so that it also shows the result of this function. Test it.


  9. Write a contract, then an implementation, of a function that takes an integer n and returns the length of the longest hailstone sequence starting at a number from 1 to n. This function must not write anything.

    Modify your main function to so that it also shows the result of this function. Test it.


  10. Write a contract, then an implementation, of a function that takes an integer n and returns the start value k, from 1 to n, that has the longest length. This function must not write anything.

    Modify your main function to so that it also shows the result of this function. Test it.


  11. Submit your work.


Compiling and Testing Your Program

Get file Makefile and put it in the same directory as your program. Then, use the following commands.

make
Just compile hailstone.cpp.
make run
Run hailstone.cpp (and to compile it if it needs compiling).
make debug
Run hailstone.cpp via the gdb debugger.
make clean
Remove all machine-generated files. After you do this, the next time you do a make, hailstone.cpp will be recompiled.


Submitting your work

You must submit your program using the following method. Email submissions will not be accepted. An excuse that you do not know how to use Linux will not be accepted.

To turn in assignment 1, change your directory to the one that holds assignment 1 and do the following command.

  ~abrahamsonk/3300/bin/submit 1 hailstone.cpp
After submitting, you should receive confirmation that the submission was successful. If you do not receive confirmation, assume that the submission did not work. Command
  ~abrahamsonk/3300/bin/submit 1
will show you what you have submitted for assignment 1.

You can do repeated submissions. New submissions will replace old ones.

Late submissions

Late submissions will be accepted for one day after the due date. If you miss a late submission deadline by a microsecond, your work will not be accepted.