Homework 01: some Pythonic C


Due 9/10/2020 by 10am


Below I offer you a series of C++ problems for you to solve by writing the code they eacxh request. You can work on these either by using the repl.it C++ site, on your own computer’s fresh set-up of C++, by using gyoza on campus, or whatever other C++ compiling and executing system you have access to. For later homework assignments, I’ll want you to work on your own machine, so you should work to set that up soon, if not now.

I’m allowing repl.it here because it’s a nice, friendly system, and I want you to be able to start writing C++ code right away. Expect to learn more about using a Unix-based environment and tools in next Tuesday’s lab and next Wednesday’s lecture.

Using repl.it, you can edit several C++ programs within that Files pane, and each one can be compiled and executed, with input/output in the “clang/Ubuntu” pane on the far right.

Be aware that when you use repl.it your code lives “in the cloud” rather than on your computer’s filesystem. This means that you’ll need to explicitly save your work, both when finished and when its just a work-in-progress, when you leave the site. There is a Download as zip option when you click the vertical dots icon in the upper-right corner of the Files pane at that site. This will take that collection of files and download it as a compressed folder onto your computer. (You can also upload files onto repl.it when you start a fresh connection there.)

I haven’t yet provided the infrastructure and instructions for submitting your completed code. This will be through GitHub using the git tools or its website. We will learn more about this in lab next Tuesday, and I can also answer any further questions about it in lecture next Wednesday. You can then use that Wednesday afternoon/evening to submit your work on GitHub, armed with that lab experience and GitHub knowledge.


Instructions

You should create a C++ source file (with suffix .cc) for each of the exercises below. Follow each specification carefully, test your code with several inputs to see whether your code works.

Please also work to use readable coding “style” and formatting, for now using your best judgement. We’ll talk more about this practice throughout the semester, and I will work to publish a set of guidelines that are good to follow. We’ll also talk soon about strategies for debugging and testing your code. We will suggest schemes for generating test inputs, and ways of scripting your tests and capturing your tests’ output using Linux.

The first three exercises should only use basic integer operations and std::cout to compute and report its output. You should not use string operations for these other than the << operation for output of a string to std::cout.

A final note: you should work to complete each of these exercises so that they compile and run correctly. Should you face problems in solving an exercise, you can still submit broken code. In your top comment in that file PLEASE let us know in what way the code is broken (why it doesn’t compile, or what tests failed, etc.). In some cases, we may be willing to give partial credit if you were diligent/good about identifying the problem.


Exercise 1: six guesses

In class (and on the web page) I shared with you the code for a random number guessing game guess.cc. Write a better version of the code in a file named guess6.cc that limits the game to allowing at most six guesses.

Exercise 2: divisors

Write a C++ program divisors.cc that requests an integer value, and then lists all the positive divisors of that integer. Your code can assume they enter an integer that’s encodeable as a C++ int type, but if the integer they enter is less than 1, it should complain about their input. Your code should exactly mimic the sample interactions I show just below. This includes use of an Oxford comma before the mention of the last divisor.

% ./divisors
Enter a positive integer just below:
35
The divisors of 35 are 1, 5, 7, and 35.
% ./divisors
Enter a positive integer just below:
1
The divisor of 1 is 1.    
% ./divisors
Enter a positive integer just below:
2
The divisors of 2 are 1 and 2.
% ./divisors
Enter a positive integer just below:
0
The number 0 is not a positive number.

Note that the % line is me running the command in Linux. The next three lines are my interaction with the C++ program.

You should only use the basic integer calculations to compute this. There is no need to use any library functions and/or floating point.

Hint: recall that you can check divisibility with the integer remainder (sometimes called “mod”) operator. In C++ the operator is %.

Exercise 3: triangle

Write a C++ program that, when given a positive integer, outputs a diagram to the terminal that looks like a triangular stack of that integer’s height, like so:

% ./triangle 
Enter a height: 4
   *
  * *
 * * *
* * * *     
% ./triangle 
Enter a height: 1
*    
% ./triangle 
Enter a height: 2
 *
* *

It should exactly mimic the interactions just above. The bottom row of asterisks should start at the leftmost column on the console, and there should be a single space between each asterisk on a row. You can assume that they type in an integer whose bottom row fits on their command-line window, but it should work, in principle, for any positive int value. Historically, terminals were 80 columns wide, so make it work for values from 1 to 80.


Exercise 4: square root

There’s a classic algorithm for computing the square root of a double-precision floating point number a invented by Newton that works as follows: Come up with an initial estimate x0 for the square root (usually half of a) and then refine that guess with the formula given by

Write a C++ program that demonstrates the execution of this algorithm on a value that’s input by the user. It should use the double floating point type and stop the iteration when the square of the successive estimates differ by less than 0.000000001. It should output the successive estimates, too.

Note that you do not need to keep track of the whole sequence of estimates. Just track the current and previous estimates in two variables, while your loop is running.

You can assume that they enter numbers between 0.5 and 100.0, though it’ll likely work with other positive values, too.