CS 117, Introduction to Computer Science

Introductory Labs

The purpose of these labs is to help familiarize you with the basics of C++ and UNIX. Experiment and see what happens! This is your chance to get your "feet wet." Ask lots of questions. Make sure to give everyone in your group a chance to "drive" (use the keyboard and mouse). There is likely more here than you can finish in the lab period, which is ok - though you may want to finish up the remaining labs on your own. Note that Lab #2 involves handing something in.

Lab #1: Starting out
Goal: To practice logging in and out of Linux, and compiling a C++ program.

  1. Go to the department home page at  http://www.mathcs.carleton.edu .
  2. Click on "course information" on the left hand side.
  3. Under "Specific Course Resources," click on "CS 117: Introduction to Computer Science."
  4. This web page has lots of information which you may find useful, and you may want to bookmark it. In particular, click on "Intro to C++ on Linux."
  5. This web page, titled "How to Write and Compile a C++ Program Using g++," contains detailed instructions on building a C++ program. Follow all the instructions on this web page.
  6. What happens if you remove the "<< endl" and recompile and rerun the program?
  7. What happens if you remove the "#include <iostream>" and recompile the program?
  8. You don't have to name the executable program the same thing as the source code, though it is usually a good idea to do so.  Try recompiling like so:

  9.  

     
     
     

    g++ -o schillerhead myprogram.cpp

    Do an "ls" to make sure the new executable is in your directory, then run the program using the new executable.
     

  10. Type "ls -l".  This gives you the detailed listing of the files in the current directory.  The fourth column in this listing gives you the size, in bytes, of each file.  How does the size of the executable program compare with the size of the source code?  To save space in your account, you can remove old executables using, for example, "rm hello".

Lab #2: Starting out
Goal: To see how comments and arithmetic operations behave.

  1. Create a new C++ program called arithmetic.cpp. Copy and paste the code from http://www.mathcs.carleton.edu/faculty/dmusican/classes/cs117/introlabs/introlab2.cpp into your new program.
  2. Compile and run this program.  When it asks for two integers, enter two integers separated by a space. What does the operation "%" do?  How about "/"?  (Try running the program several times with different input.)
  3. Now change the type of a and b from "int" to "double".  Recompile.  How does the behavior of "%" change?  Get rid of the "%" line and recompile.  How does the behavior of "/" change? The type "double" is badly named--what does it mean in this context?
  4. Suppose you want to "comment out" a block of code that's bugging you.  That is, you want to make the compiler ignore some code without actually removing the code from your file. The /*...*/ style comments give you a convenient tool for doing this.  Try commenting out everything in main() except for the return(0) statement. Recompile. What happens, and why?  How can you fix the problem? Which style of comments is appropriate for documentation in your code, and which style of comments is appropriate for commenting out blocks of code at a time?
  5. Put the answers to the above questions in documentation at the top of the program. Add the names of all students in your group.
  6. Submit this program using the hsp electronic submission program. To find out how to do so:

  7. Go to the department home page at  http://www.mathcs.carleton.edu .
    Click on "course information" on the left hand side.
    Under "Specific Course Resources," click on "CS 117: Introduction to Computer Science."
    Click on "Using the Homework Submission Program."

Lab #3: Decisions, decisions...
Goal: To introduce conditional behavior into a program.

  1. Create a new C++ program called decisions.cpp. Copy and paste the code from http://www.mathcs.carleton.edu/faculty/dmusican/classes/cs117/introlabs/introlab3.cpp into your new program.
  2. Read through this program and predict its behavior. Compile it and run it several times with varying input. Can you get all of the different output messages?
  3. One message asks "What do I know about that first number?" If that message appears, what _do_ you know about the first number?
  4. What does "&&" mean (see the "third and fourth messages" section) in English?  Try replacing "&&" with "||", which means "or".  Does the program behave as expected?  (You can test it by entering two numbers, only one of which is zero).

  5.  

Lab #4: Lather, Rinse, Repeat
Goal: To introduce repetition into a program.

  1. Create a new C++ program called loops.cpp. Copy and paste the code from http://www.mathcs.carleton.edu/faculty/dmusican/classes/cs117/introlabs/introlab4.cpp into your new program. This program demonstrates a simple "while" loop.
  2. Compile and run this program.  Yee-hah!  Take a look at the "while" loop and see if you can make sense out of how it works. What do you expect to happen if you leave out the "i = i + 1" line?  Try removing that line, saving, recompiling, and executing.  Were you right?  (By the way, you can stop a program running out of control by typing a CTRL-C.  That is, hold down the CTRL key while typing C.)
  3. Put the "i = i + 1" back in.  What do you expect to happen if remove the "i = 1" line?  Try it and see.
  4. Try changing the program to deliver only odd-numbered greetings. Then try delivering only even-numbered greetings.
  5. A little terminology:  a "loop" is a program structure that allows you to do something over and over.  When you add one to a variable (e.g.  i = i + 1), you are "incrementing" that variable.  The question asked in a while loop or other loops (in the case below the question is "i <= nGreetings", or in English, "is the value of variable i less than or equal to the value of variable nGreetings?") is called the "loop condition" or "loop test."

Lab #5: Strung out
Goal: To learn about C++ character strings.

  1. Create a new C++ program called cppstrings.cpp. Copy and paste the code from http://www.mathcs.carleton.edu/faculty/dmusican/classes/cs117/introlabs/introlab5.cpp into your new program.
  2. Note that to use the string type, you need to #include <string>.
  3. Compile and run the program.
  4. What's the \" about in the output statements?
  5. What if you type a return or two between your first and second words?  What if you have punctuation between them?
  6. What does thisWord[i]  mean?  What values of i make sense in this context?

Lab #6: Beginning to function
Goal: To introduce C++ functions.

  1. Create a new C++ program called printwords.cpp. Copy and paste the code from http://www.mathcs.carleton.edu/faculty/dmusican/classes/cs117/introlabs/introlab6.cpp into your new program.

  2. Compile this program and try running it in two different ways:
    a) Use gEdit to create a text file called (for example) somefile.txt, with any kind of text in it.  Then, at the prompt, type:
                   printwords < somefile.txt
    b) Type in response to the prompt:
            printwords
        Then, type whatever bunch of words you wish, on as many lines as you wish.  When you're done typing, type CTRL-D at the start of a new line.
  3. Try running this program on input data that includes a "word" that consists entirely of digits and punctuation. What does CleanWord() do to such a word? How could you change the main program to prevent the program from printing these kinds of words?