Lab 0: Gitting Started with C++
Due on Sunday, January 24th at 11:59 PM. This is an individual lab. You are to complete this lab on your own, although you may discuss the lab concepts with your classmates. Remember the Academic Integrity Policy: do not show your code to anyone outside of the course staff and do not look at anyone else’s code for this lab. If you need help, please post on the Piazza forum or contact the instructor or ninjas. If you have any doubts about what is okay and what is not, it’s much safer to ask than to risk violating the Academic Integrity Policy.
Overview
In this lab, you will:
- Learn how to use Git, a popular revision control system.
- Write some simple C++ programs to become familiar with the basics of the language.
This lab comes in two parts; please be sure to complete both. Please read this lab carefully as it prepares you for all of the labs you will be completing this semester and includes instructions for making sure that you have submitted your work properly!
Part I: Gitting Started
Throughout this course, we will be using a revision control system known as Git. The purpose of a revision control system is to track changes to software over time. This has several advantages. The most notable for our purposes are the following:
- Tracking individual changes is similar to (but better than) keeping copies of your program as you develop it. If you decide that you made a mistake, you can go back to an old version of the software and work from there.
- When you are working in pairs on the later labs, the revision control system will coordinate changes. This prevents you from erasing each others’ work.
Most students in this course have taken CS21 at Swarthmore, in which we used a script called handin21
to turn in homework assignments. The handin21
script is using Git behind the scenes. The way we use Git in this course requires a bit more understanding but gives you much greater control.
We will learn about Git throughout the course of the semester. This part of the lab will teach you just what you need to know to complete the first few labs.
About Git
![Git Logo](git_logo.png)
Git is a piece of open-source software which is installed on the Swarthmore CS systems. You can run it by typing git
on the command line. We’ll talk about a few things you use the git
command to do shortly.
A Git repository is a directory containing files that are managed by a revision control system. This looks just like a normal directory under most circumstances, but it contains a hidden .git
subdirectory (that you generally shouldn’t touch). By using the git
command inside of a repository, you can manage the files in that directory.
Git is a distributed revision control system: there can be multiple different copies of the same repository on different computers, some of which might contain different information than others. When developing software, we occasionally use the git
tool to move information from one copy of a repository to another. Two operations you’ll be performing in this lab are cloning (which creates a copy of a repository) and pushing (which sends your changes back to the original repository).
About GitHub
![GitHub Logo](GitHub_Logo.png)
![GitHub Octocat](GitHub_Octocat.png)
GitHub is a popular website which provides the service of storing and managing Git repositories; software developers can keep their repository on GitHub and then copy content to and from it from their own computers. We will not be using that website, although we will be using something very similar. Swarthmore has its own GitHub site which provides several advantages, so we will use that for our course.
In order to start using the Swarthmore GitHub website, you should follow the one-time setup instructions on Dr. Tia Newhall’s webpage. Of course, these are one-time instructions; if you have already followed them for a different course (such as CS31), you don’t need to do so again.
Cloning a Repository
For the labs in this course, you will be using repositories that we have already created; this is similar to how students use the update21
script in CS21 to fetch the initial files for a given assignment. That repository – the directory of files we have created for you – exists on the Swarthmore GitHub site. To edit those files, you will have to clone the repository into your home directory on the CS lab machines. You can make the clone by running this command:
git clone git@github.swarthmore.edu:cs35-s16/lab00-<your-username>.git
Make sure to replace <your-username>
with your Swarthmore username. For instance, you might write
git clone git@github.swarthmore.edu:cs35-s16/lab00-zpalmer2.git
if your Swarthmore username were zpalmer2
. (It isn’t.)
Running the clone
command will cause git
to create a directory named e.g. lab00-zpalmer2
in your current directory. You should cd
into that directory now.
Editing Files
![Pencil and Paper](editing.png)
Your newly-cloned repository contains several files; we will edit one named Questions.txt
now. Use your favorite editor to open Questions.txt
and answer the questions it contains. Note that you’ll need to review the Academic Integrity Policy.
Once you are finished, run git status
. You should see something like this:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Questions.txt
no changes added to commit (use "git add" and/or "git commit -a")
$
(The $
is a command prompt.) This indicates that changes have been made to the file named Questions.txt
but that Git has not yet been told about those changes. In order to record the changes that we have made, we must commit them. Run the following commands:
git add Questions.txt
git status
You should now see something like this:
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: Questions.txt
$
This indicates that Questions.txt
has now been “staged”; that is, it will be part of the next set of changes we create. In a more complicated scenario, we might want to run git add
several times or do something else more complicated to create a single “change”. We use git add
to add the changes for certain files to the staging area in order to create a new version of our directory.
In this case, the only file we’re changing is Questions.txt
, so we’re ready to create our change. Run the following command:
git commit -m "Answered the questions."
This command will create and record the change. It will attach the comment (here, “Answered the questions.”) to the new versions of our files. If we run git status
now, we see something like this:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
$
The message “working directory clean” indicates that our Git repository knows about all of the changes that we’ve made. You can even look at the history of the changes you have made by running git log
. (If you do, you can exit the log viewer by pressing q
.)
Pushing Your Changes
![Package](package.png)
Remember: your directory on the CS network is only a copy of the “real” repository stored on the Swarthmore GitHub site! At this point, your copied repository is aware of your new changes, but the Swarthmore GitHub version is not. We can see this because the previous git status
message included “Your branch is ahead of ‘origin/master’ by 1 commit.”, which tells us that we have a commit that the GitHub does not. To send the commit that we have made to the Swarthmore GitHub, we can run git push
.
$ git push
Counting objects: 3, done.
Writing objects: 100% (3/3), 209 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.swarthmore.edu:cs35-s16/lab00-zpalmer2.git
* [new branch] master -> master
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$
The git push
command attempts to send every new commit we have made back to the original place from which we cloned our repository.
Double-Checking Your Push
To be certain that the Swarthmore GitHub website has your changes, visit your repository on that webpage. The URL will be https://github.swarthmore.edu/cs35-s16/lab00-<your-username>
(again replacing <your-username>
appropriately). If everything worked out, you should be able to view the GitHub copy of Questions.txt
by clicking on the like-named link on that page. Make sure that the contents of that file match what you expect.
If You Have Trouble…
…then ask us for help! Post on the course Piazza forum or e-mail the professor or a ninja. That’s why we’re here!
Summary
In order to use Git to update your lab repository, you should perform the following steps:
- Clone the repository (only necessary once per lab!)
- Make changes to your files
git add
each file you changedgit commit -m "message"
to create a commitgit push
to send your changes to the Swarthmore GitHub- Repeat steps 2-5 as necessary until you are satisfied with your repository
Part II: Some Simple Programs
![](C++Logo.png)
Next, you will create some C++ programs. You will submit them by pushing them to your Swarthmore GitHub repository; we will grade the contents of that repository only, so make sure you git push
!
Factorial
Create a file called factorial.cpp
. In that file, write a function factorial
which takes an int
and returns the factorial of that number. Then, use cin
and cout
to write a main
function that prompts the user for a number and displays the factorial of the user’s input. If the input is negative or not a number, print a message describing the error.
$ clang++ -o factorial factorial.cpp
$ ./factorial
Which factorial do you want? 4
The factorial of 4 is 24.
$ ./factorial
Which factorial do you want? 0
The factorial of 0 is 1.
$ ./factorial
Which factorial do you want? 10
The factorial of 10 is 3628800.
$ ./factorial
Which factorial do you want? -1
Negative integer factorials don't exist!
$ ./factorial
Which factorial do you want? all of them
That is not a valid integer.
$
Dosido
Create a file called dosido.cpp
. In that file, write a function dosido
which takes a string
and returns a new string with every other letter transposed. For instance, computer
becomes ocpmture
. Strings with an odd number of characters simply append the last character: for instance, mouse
becomes omsue
. Then, as above, write a main
function to prompt the user for a string and show the output of the dosido
function.
$ clang++ -o dosido dosido.cpp
$ ./dosido
What string to use? computer
The dosido of computer is ocpmture
$ ./dosido
What string to use? mouse
The dosido of mouse is omsue
$ ./dosido
What string to use? 123456789
The dosido of 123456789 is 214365879
$
Remember to Commit and Push
Once you are finished writing the above programs, remember to commit and push them! You can double-check your submission by looking at the GitHub website. We will grade what is shown there by the due date.