CS35 Lab 0: C++ warm-up

Due 11:59pm Sunday, September 7, 2014

The goal of this lab is to introduce you to basic concepts in the C++ program language. Concepts you will be familiar with after this lab include:

Some of these programs may seem simple, but it is meant to prepare you for future labs and also to provide practice for in-class topics.

A skeleton directory for your solutions will appear in your cs35/labs/00 directory when you run update35. The program handin35 will only submit files in this directory. In later labs you may have the opportunity to work with a partner, but for this lab you should work on your own. Be sure that all solutions are placed in the cs35/labs/00 directory before submitting.

Exercises
  1. Loops and strings: In the file funStrings.cpp, complete a program that takes in a series of words from the user and outputs a string with the words in the reverse order. The loop should stop when the user enters "done". For example:
    $ ./funStrings
    Bad 
    programmers
    worry
    about
    the 
    code
    Good
    programmers
    worry 
    about
    data
    structures
    and
    their
    relationships
    Linus
    Torvalds
    done
    
    In Reverse:
    Torvalds Linus relationships their and structures data about worry
    programmers Good code the about worry programmers Bad
    

  2. For loops: In the file computeAvg.cpp, write a program that asks a user to enter up to 10 positive integers and computes the average. Your program should accomplish the following:
    • You must use a for loop and no while loops.
    • If the user enters 0 or less, the loop should end and compute the average of the preceding numbers. E.g., if the user enters:
      5
      10
      15
      -3
      
      you should report the average as 10
    • While the inputs are integers, your average should be allowed to take on any real value.

  3. Conditions, Loops, and Functions: In the file empathy.cpp complete a program that asks a user how they are feeling and generates an appropriate empathetic response. Specifically, your program should:
    • Ask the user whether they are (1) happy, (2) angry, or (3) sad
    • Ensure the user enters a valid choice (1, 2, or 3). If not, repeat the question.
    • Convert the valid choice into an appropriate response.
    • Output the result.
    • Your solution should employ functions and good design. That is, your main function should be simple and only a few lines of code. The major tasks should each be placed in separate functions.
  4. Below is some sample output of a correct solution. Your solution does not have to completely match this, as long as it correctly meets the requirements above.

    Sample Runs

    ./empathy 
    Hello!
    How are you feeling?
    
      (1) happy
      (2) angry
      (3) sad
    
    Which option would you like? 1
    Great! I am happy for you.
    
    ./empathy
    Hello!
    How are you feeling?
    
      (1) happy
      (2) angry
      (3) sad
    
    Which option would you like? 3
    That makes me sad too.
    
    ./empathy 
    Hello!
    How are you feeling?
    
      (1) happy
      (2) angry
      (3) sad
    
    Which option would you like? 9
    
    Incorrect entry.  Try again.
    How are you feeling?
    
      (1) happy
      (2) angry
      (3) sad
    
    Which option would you like? 2
    
    Oh no!  I am sorry you are angry.
    

  5. Written Response: In a file called README, answer the following questions to test your understanding of course policies:
    1. How many late days are allowed for the entire semester?
    2. Read the academic integrity policy. Is it acceptable to have an upper-level CS student look at your program to help figure out why it is not working?
    3. If you are encountering a problem with your program, what must you do before asking either a ninja or faculty member for assistance?
    4. If you are stuck on your program, rank the following support options by which you should attempt first. Note that some of these should never be attempted, so leave them off your priority list.
      1. Email the professor
      2. Attend a ninja session
      3. Post a question on Piazza
      4. Ask a student ninja that you run into around campus
      5. Work on the problem with your lab partner
      6. Ask a fellow student who is not your lab partner
      7. See either instructor during office hours
      8. See either instructor during non-office hours when their door is open
      9. Ask either instructor when you see them at Sharples enjoying ice cream.

General Requirements

Your program should follow good coding practices. Specifically, each file should maintain the following properties:

The following code snippet is a example of good commenting style.
/* findMax
 *
 * Inputs: a,b,c: three integers we want to compare
 * 
 * Returns: the maximal input 
 */
int findMax(int a, int b, int c) {
  int max = a;  // initialize to first input

  // compare max to b
  if(b > max) {
    max = b;
  }

  // now compare max to c
  if(c > max) {
    max = c;
  }

  // return maximal element
  return max;
}

Submit

Once you are satisfied with your code, hand it in by typing handin35. This will copy the code from your cs35/labs/00 to my grading directory. You may run handin35 as many times as you like, and only the most recent submission will be recorded.