(E) Number series game¶
Goal: I will practice the use of control structures in C++ (i.e. if-statements and loop structures) and learn more about using the version control.
Instructions:
To start this exercise, you first need to retrieve the template
from the directory templates/02/number_series_game
(copy it onto
the directory student/02/number_series_game
) and open the
project in Qt Creator (stages 1-2 of exercise 1.3.4).
Attention
The solution will be programmed in three separate stages according to the instructions given in the assignment. At every stage, you should first export the changes you have made from your working copy to your local data repository (commit), and after that, from the local repository to your central data repository (push). In other words, you will repeat stage 4 of the previous exercise three times. At the end, you will submit your program to Plussa, meaning you perform stage 5 from the instructions of the previous exercise.
Please follow the instructions of the assignment closely. The exercise will also test other characteristics of version control. It will not just be a three-time repetition of things learned earlier.
Assignment¶
There is a game in which the players sit in a circle and take turns listing numbers. The first player says ”1”, the next says ”2”, and then the next person says their number, obviously. To keep the game interesting, every number divisible by three will be replaced with the word ”zip”, every number divisible by seven will be replaced by ”boing”, and finally, every number divisible by both three and seven, ”zip boing.”
Use the program code template to create a program that prints a cheat sheet for someone playing the game but not knowing how to divide. To begin, the program asks how many numbers you want to print.
An example of how the program should work:
How many numbers would you like to have? 10
1
2
zip
4
5
zip
boing
8
zip
10
Writing the code divided into steps¶
Create the program in the following steps:
Start by editing the template. First, implement a program that only asks the user how many numbers to print on the cheat sheet, and then prints the numbers on separate rows, so that it works like this:
How many numbers would you like to have? 6 1 2 3 4 5 6
As you finish this step, first save your work in your local repository, and after that, in the central repository (step 4 in the previous exercise - commit and push). Write a commit message, for example ”Reading the input and printing the numbers”.
Then, edit the program so that it prints the string ”zip”, i.e. numbers divisible by three will not be printed but instead replaced with the string ”zip”.
When you have finished this step, again save your work via your local data repository into the central data repository (commit and push). This time, write a commit message along the lines of ”Added processing numbers that are divisible by 3”.
Finish your program by adding also the printing of the string ”boing”. Now your program should work similarly to the example run at the start of these instructions. Please note that it should print ”zip boing” at numbers that are divisible by both 3 and 7.
Don’t go ahead and save your work in the central repository just yet (commit and push)! Let us first test some of the different functions of the version control.
The
diff
function in Git can show which changes were made in the program code after the last commit. Save the file in Qt Creator and then see what the functionTools > Git > Current File > Diff of main.cpp
shows you. Or, alternatively use the command line.Now, take the changes you have made and move them to your local data repository (commit), but do not move them to the central data repository yet (don’t push). This time, set your commit message to something like ”Added also processing numbers that are divisible by 7”.
At this point, take another look at what the function
diff
tells you in Qt Creator.Now we will try out one of the typical mistakes you can make at submitting a program. Submit the exercise by clicking the Submit button at the bottom of this page. This faulty submission will cause you no harm. Compared to most of the other exercises, this one has significantly more submissions.
If you have followed the instructions above, you have just failed at submitting your exercise. Let us now find out why this happened.
Open your Git central data repository in the browser by inserting the URL of your repository in the browser search bar (from the two addresses you have, it is the one without the ending .git). Via this web interface, you will be able to access and explore your central data repository. The contents of the central data repository need to be examined in your browser, because it is not located on the computer you are using (for most of you, a linux-desktop) but on the server.
The Files tab always shows the latest state of the whole project, and the commit message of the last commit concerning each folder or file.
Move to the tab Commits. Here you can see which of the commits are stored in your central repository. All of the commits that have been taken to the central repository with push are visible here. The automatic evaluation system of the course will evaluate the last version of code shown here.
You can see the changes made after the last commit also via the web interface of the central repository by clicking the so-called tag on the commit line. The last item on the right at the end of the row is called ”Browse files”, next to it on the left is the copy button, and to the left of the copy button is the tag (which looks like a mess of letters and numbers, but we will get back to this later). Click the tag and you will see how the web interface presents the changes.
Now go back to Qt Creator or command line. Do the push operation you ”forgot”, and submit your work with the form below. Also, go to the browser to see if you can now view the last commit.
While following the instructions above you noticed that using the version control and submitting your work are two different things. Since you implemented the program in more than one step, your repository will show you how your work progressed, and you also have the possibility to have a look at your intermediate versions.
Version control is not used only for submissions, but it also has a purpose of its own ‒ it stores the different versions of your programs. When the size of your programs grows, you will have more benefit from having saved intermediate copies, for example, when you run into a problem. In the future, this course will include both exercises that tell you how to divide your work into parts, and exercises where you can choose how to split them. The most essential thing for you is to practice using the version control. That means you can only submit the smallest exercises without intermediate versions.
A+ presents the exercise submission form here.