Homework 01: some Pythonic CDue 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 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 Instructions You should create a C++ source file (with suffix 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 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 guessesIn class (and on the web page) I shared with you the code for a random number guessing game Exercise 2: divisorsWrite a C++ program
Note that the 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: triangleWrite 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:
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 Exercise 4: square rootThere’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 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. |