Lab 1 editing and running C++ programs



CSC230 Lab 1

Goal: This lab will give you practice editing and running C++ programs, using Emacs,
cin, cout, two-dimensional array, and nested for loops.
Please try your best to finish the lab in class, and submit it to CANVAS. Use command
“jar” to zip all your .cpp files and .o files into lab1.jar. Submit lab1.jar to CANVAS. To
use command “jar”, type the following command at prompt
jar –cvf lab1.jar *
The above command will put all files (that is meaning of *) under current directory into
lab1.jar. If you want put several selected files from the directory, list the filenames at *
place. For example, you want to put two files, foo and bar, into lab1.jar. You can type the
following command
jar –cvf lab1.jar foo bar
Sometimes, you may have a jar file, such as lab1.jar. If you want to open this jar file and
extract all the files inside this jar file, you can type the following command
jar –xvf lab1.jar
Please note lab1.jar can be any jar file you may have.
PART I: Getting started
1. Please login the computer by using your TCNJ email username and password. If the
login process does not work, check your Internet cable connection. If cable
connection is fine, restart your computer and try again.
2. Open a terminal in the computer. You can type Unix commands inside the terminal
now. For a Mac machine, you can find terminal icon in the bottom of the window. If
you want to do the work on your own Linux machine, like Ubuntu. You can do this
by selecting the “Dash Home” icon at upper left, typing “terminal” in the search bar,
then clicking on the “Terminal” program. There’s also a keyboard shortcut: hold
down the Ctrl and Alt keys while typing “t”.
3. Warning: DO NOT, at this time or any point in the future, delete the .login, .bashrc,
and .emacs files under your directory. If you would like to customize your account,
do so by adding new commands to the end of these files, not replacing the files.
PART II: Read a file, input the data to a two dimensional matrix
After logging in, please do the following steps.
1. Create a directory for lab1 by typing
mkdir lab1
2. Change into your lab1 directory:
cd lab1
3. Download 0505matrix file from Canvas to lab1 directory.
4. Run Emacs by typing
emacs Lab1.cpp
In this C++ program, Lab1.cpp, please write code that will do the following
• Use cin to read two integers x and y
• Use x and y to create an two dimensional array, which has size x (row)
and y (column)
• Write a nested for loop to read (cin) char values, assign these values to the
elements of the two dimensional array
• Write another nested for loop to print out the values of the twodimensional array. The values should be printout out row-by-row.
5. Save the file by using “C-x C-s”. Compile the program within Emacs using:
This will allow you to run a shell command:
g++ -g Lab1.cpp
“g++” is the name of the C++ compiler. “-g” is a switch to tell the compiler to
generate the information the debugger will need.
6. Use the control sequence C-x ‘ to jump directly to errors found by g++. (Be sure
to use the single opening quote, not the apostrophe.) Figure out what’s wrong,
correct the errors, save the file using “C-x C-s”, and compile the code.
PART III: test the code
Once g++ is able to compile the code, it will create a file called a.out in the same
directory. In a shell (an xterm or an Emacs buffer called *shell*), type
./a.out < 0505matrix
Here “< “ sign means input redirection. Whenever program a.out tries to read data
from keyboard, it will get data from file 0505matrix. The file 0505matrix contains
the following data
5 5
u r a q o
f t c n j
k r h p r
e a v o t
z h g a h
The program read the first two values, 5 and 5, to x and y. Then the program uses
x and y to create a two-dimensional array. Then it reads the following values and
assigns them to each element of the array. After reading is done, the program
prints out the values of the array, row by row. For the file of 0505matrix, the
result will be
u r a q o
f t c n j
k r h p r
e a v o t
z h g a h
• The array size must be from the input. Do NOT hard code the array size.
For example, do NOT write a code like:
int arr[5][5];
Your program should work with different input, which may have different
array sizes. If the array size is hard-coded, the program will not be able to
work with the input values of different sizes.
• It is normal that you will have questions about C++ language itself, you
can check the following website
This link points to “Standard C++ Library Reference”. There is a lot of
information, not all of which will make sense right now, but you should be
able to find a description of the data type or the statement that you have
• When you think you have found the error, correct it, save the file,
recompile it, and execute it to see if the problem is solved.
• Aside: You may think the executable file generated by g++ is Lab1. In
fact, by default, the executable file name is a.out. If you prefer a different
executable filename, such as myLab1, you type the following command to
compile the source file
g++ -g Lab1.cpp –o myLab1
Wrap up
When you’re done, jar three files to lab1.jar
jar –cvf lab1.jar Lab1.cpp a.out
Submit lab1.jar to Canvas.
Make sure you logout before you leave!
If you cannot finish lab in class, please save all your files. Next time you login the
computer in the lab, you can continue work on your files. Please save them before you
• If you work in a Linux lab, please save the file to your machine. Whenever you
come back to any computer in the Linux lab, you will see the saved files.
• If you are working in the Mac lab, please save the file to a CLOUD. The Mac
machine will erase everything you saved once you logout.


There are no reviews yet.

Be the first to review “Lab 1 editing and running C++ programs”

Your email address will not be published.