The goal of this lab is to give you practice with writing, compiling, and running basic C++ programs, and get you used to the handin system using Github. Concepts you will be familiar with after this lab include:
To fetch the skeleton for this assignment run:
$ git clone firstname.lastname@example.org:cs35-f15/lab1-<your-username>.gitA skeleton version the program will appear in the lab-1-<your-username> directory when you run this.
In the file
fibonacci.cpp write a function
n and returns the
number as an
int, indexed starting at 0 (so
2, and so on).
See the Wikipedia for Fibonacci Numbers.
cout to prompt the user for a
number, and display the fibonacci result.
If the input is below 0 or not a number, print a message describing the
error rather than computing the fibonnaci number:
$ clang++ -o fibonacci fibonacci.cpp $ ./fibonacci Which fibonacci: 6 8 $ ./fibonacci Which fibonacci: 0 0 $ ./fibonacci Which fibonacci: -1 Can't get fibonacci of negative number $ ./fibonacci Which fibonacci: asdfasdf Error reading input, expected positive integer
Hint –if you have trouble implementing the
function with a
for loop, try a
reverse_string.cpp, write a function
reverse_stringthat takes a string as an argument, and returns a new string that is a reversed version of the input. Then use
mainto prompt the user for a string when the program is run, and reverse that string. An interaction should look like:
$ clang++ -o reverse_string reverse_string.cpp $ ./reverse_string String to reverse: reverseme emesrever
Things to use:
while loop, string
s.length() (which returns
the length of a string), and string indexing with
nth character in a string.
Answer the questions in
README.txt by inserting answers in that
file and committing the changes (see instructions below).
git add <file>on each file you change or add; this tells
gitwhich changes are going to be saved, or committed to the repository.
git committo save the
added changes to the repository. When you run
git commit, you also have the opportunity to write a commit message that logs what changes you made with that commit.
git pushto push out the changes to Github. Just
git commitonly makes a log of the changes locally, so there's an extra step that actually puts it on the Web. This last step is necessary, because we pull your code from Github in order to grade it, so if you don't
git push, you haven't submitted!
There's another command, called
git status that will tell you the
state of your repository. Here's what a sample interaction might look like
for this assignment:
mint:lab1$ git status On branch master Changes not staged for commit: (use "git addNote that according to the directions in the reference above, I used
..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: README.txt Untracked files: (use "git add ..." to include in what will be committed) fibonacci.cpp reverse_string.cpp no changes added to commit (use "git add" and/or "git commit -a") mint:lab1$ git add README.txt fibonacci.cpp reverse_string.cpp mint:lab1$ git commit -m "You can add a message with the -m command-line option" [master bc4ecd0] You can add a message with the -m command-line option 3 files changed, 4 insertions(+) create mode 100644 fibonacci.cpp create mode 100644 reverse_string.cpp mint:lab1$ git push origin master Counting objects: 8, done. Delta compression using up to 8 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (8/8), 738 bytes | 0 bytes/s, done. Total 8 (delta 1), reused 0 (delta 0) To email@example.com:cs15-f15/lab1 * [new branch] master -> master
git push origin masterthe first time I pushed, if you get an error message talking about
push.defaultthe first time you push, try
git push origin masterinstead.
If you run into problems, feel free to post the output of your git commands on Piazza with a description of the problem you're having, and that will be the quickest way to get help. Be sure to check any existing answers first, since someone may have had the same problem as you!
We will pull your solutions down at Sunday midnight, so make sure you run
git push by 11:59pm on Sunday.