CS35 Lab 1: C++ Warmup

Due 11:59pm Sunday, September 6, 2014

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 git@github.swarthmore.edu:cs35-f15/lab1-<your-username>.git 
A skeleton version the program will appear in the lab-1-<your-username> directory when you run this.
Programs
  1. In the file fibonacci.cpp write a function fibonacci that takes an int n and returns the nth Fibonacci number as an int, indexed starting at 0 (so fib(0) is 0, fib(1) is 1, fib(2) is 1, fib(3) is 2, and so on). See the Wikipedia for Fibonacci Numbers.

    Then, use cin and 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 fibonacci function with a for loop, try a while loop.

  2. In the file reverse_string.cpp, write a function reverse_string that takes a string as an argument, and returns a new string that is a reversed version of the input. Then use cin and cout in main to 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: for or while loop, string concatenation with +, s.length() (which returns the length of a string), and string indexing with s[n], which returns the nth character in a string.

Written

Answer the questions in README.txt by inserting answers in that file and committing the changes (see instructions below).


Using git to Submit
First, read the documentation at Commands for Shared Git Repos. You will need to do three different things:
  1. Use git add <file> on each file you change or add; this tells git which changes are going to be saved, or committed to the repository.
  2. Use git commit to 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.
  3. Use git push to push out the changes to Github. Just git commit only 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 add ..." 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 git@github.swarthmore.edu:cs15-f15/lab1
 * [new branch]      master -> master

Note that according to the directions in the reference above, I used git push origin master the first time I pushed, if you get an error message talking about push.default the first time you push, try git push origin master instead.

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.