lab 1: computing the size of a grid

EN47/COMP9, Fall 2009

Out: September 17, 3:00pm
Due: September 24, 3:00pm

overview

In this lab, you will start by organizing your comp9 directory to store the files you will create in the labs separately. Then you will write a C++ program, compile it, run it, and extend its input and output.

The electronic version of this handout is available on the course website at http://www.cs.tufts.edu/comp/9.

structuring your directory

Log in to a computer and open a Terminal window. At the command prompt, type cd comp9 to go into the comp9 direrctory. Then type pwd to see the full pathname of the comp9 directory. Now type ls, and you should see a list of some of the files you created last week. Create a directory called lab0 and move all of last week's files into it. Refer to Lab 0 to review the Linux commands you will need for this.

Create a new directory called lab1 and change to it (cd lab1). You should do all of your coding today in the lab1 directory.

computing the size of a grid (80%)

In this part of the lab, you will use the C++ compiler, which is called g++, on a very simple program. A compiler is a tool that translates a program written in a high-level language such as C++ into machine language (1s and 0s) which the computer can execute.

creating a simple c++ program

Using Emacs, create a new file called grid_size.cpp

In this new file, type the lines below.

     // File: grid_size.cpp 
     // Name: [put your name here]
     // Date: [put the date here]
     // Calculate the number of cells in a grid.

     #include <iostream>
     #include <string> 

     using namespace std; 

     int main() {

       return 0;
     }

Save this file. Then bring the Terminal window to the foreground and type ls. You should see your file grid_size.cpp listed. If not, ask the TA for help.

compiling a c++ program

Once you have located grid_size.cpp, compile the program, by typing:

g++ grid_size.cpp

If all goes well, you will see nothing, and the shell will give you another Linux prompt. After the compiler translates your file into machine language, it will save the result in an executable file called a.out. Type ls again and see that this file actually exists. You can run this program by typing its name followed by the return key in the Terminal:

./a.out

Note that to execute this program, we have to include the "./" in front of its name to make its location explicit. The "." indicates the current working directory. (Recall that ".." indicates the parent directory.)

Since your program currently includes no commands, a.out will run just fine, but no output will appear on the screen.

writing code

Now it is time to fill in the code for your first C++ program. Using Emacs, modify grid_size.cpp to ask the user to enter the number of lines and columns of a rectangular grid. Once it has the input, your program should compute the nmber of cells in the grid and print it out for the user. When executed, the program should look like this:

How many lines in the grid? 10
How many columns? 3
The grid size is 30 cells.

Note: The underlined text was entered by the user; you have no control over the formatting of user input.

Before you type your program, work it out on paper. Refer to your lecture notes if you need help, and don't be afraid to ask questions. When you are done, use g++ to compile your program again. If you get any compiler errors, see below.

Now, test your program with some other inputs, e.g. one line and two columns, or something more extreme like 100 lines and 0 columns. Testing your code is very important. Often it will work with common input, but fail on special cases.

handing in

When you are satisfied that your program works perfectly with normal and extreme inputs, submit it to be graded using the following command in the Terminal:

provide comp9 lab1 grid_size.cpp

Please also print a copy and give it to the TA.

compiler errors

Often, when you compile a program, there will be mistakes in it. When this happens, the compiler cannot continue, so it stops and tries to tell you what went wrong. The compiler doesn't know what you wanted to say, so its guess as to what you did wrong is not always accurate; the only thing it can be sure of is that it does not understand what you said.

To see what compilation errors look like, go back your program in Emacs. Find the line of code where you print the prompt "How many lines in the grid?" Delete the semicolon (';') at the end of this line. This character is required by C++ to terminate this statement, so removing it will be sufficient to confuse the compiler. Now compile the program again. You should see an error like the following:

g++ grid_size.cpp
grid_size.cpp: In function 'int main(...)':
grid_size.cpp:23: parse error before '}'

Notice that g++ isn't smart enough to tell you that there was a missing ';'. All it could figure out was that something went wrong before line 23. (The actual line number will depend on your code.)

Go back into Emacs and put the semicolon back in. Now try deleting the quotation mark that is just before the semicolon. Compile the program again:

g++ grid_size.cpp
grid_size.cpp:22: unterminated string or character constant
grid_size.cpp:22: possible real start of unterminated constant

Notice again that g++ doesn't tell you that there was a missing quote. It doesn't notice the problem until it gets to line 22.

Now fix this in Emacs and save it. Then, re-compile your program and run it (./a.out). Test it several times with a wide range of inputs.

fancier input and output (20%)

In this part of the assignment, you will extend the grid size program to request the initials of the user before getting the grid dimensions. Do not modify your grid_size.cpp file (which you should have submitted). Rather, make a copy to a new file named grid_size_initials.cpp using the command:

cp grid_size.cpp grid_size_initials.cpp

In the code for this new program, you will need to declare variables to store the first, middle, and last initials. You should add code that asks the user for the initials and reads in what was typed. Extra credit: Try to accomplish the input step with as few as two lines of code.

At the end of the program, output something like, "EKR, you defined a grid of size [...]" When you are satisfied and confident that your program works correctly, print a copy for the TA and also submit it using provide:

provide comp9 lab1 grid_size_initials.cpp