lab 1: computing the size of a gridEN47/COMP9, Fall 2009 overviewIn this lab, you will start by organizing your The electronic version of this handout is available on the course website at
structuring your directoryLog in to a computer and open a Terminal window. At the command
prompt, type
Create a new directory called computing the size of a grid (80%)In this part of the lab, you will use the C++ compiler, which
is called creating a simple c++ programUsing Emacs, create a new file called 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
compiling a c++ programOnce 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 Note that to execute this program, we have to include the " Since your program currently includes no commands, writing codeNow it is time to fill in the code for your first C++ program. Using Emacs,
modify
How many lines in the grid? 10
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 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 inWhen 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 errorsOften, 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 "
g++ grid_size.cpp
Notice that 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
Notice again that Now fix this in Emacs and save it. Then, re-compile your program and run it
( 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 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 comp9 lab1 grid_size_initials.cpp |