|
course.wilkes.edu/CS125Labs |
||||||
Lab 0: Your First Program
Your First Program
Once you are "in" the directory
Once we have created a file containing our source program, the second step can be accomplished through the use of a C++ compiler: a program that translates C++ programs into machine language (the binary 1s and 0s that a computer "understands." Graphically the process can be pictured as follows:
As we shall see in a later lab exercise, this is a bit of a simplification, but it is sufficiently accurate to give you an idea of what is occuring. The C++ compiler used throughout this manual is named g++ and it (like emacs) is a freeware product of the Free Software Foundation.
Editing a File
Our first program will input a base-10 integer and display that same value in
base-10 (decimal), base-8 (octal) and base-16 (hexadecimal).
We will thus name the executable program % emacs bases.cpp(If you are using the X-window environment, use 'xemacs' and add an ampersand ( &) at the end.)
Since the file
Within the // bases.cpp demonstrates basic I/O in C++. // // Written by: Jane Doe, November 21, 2009. // Written for: CS 125, at Wilkes University. // // Specification: // Input(keyboard): aNumber, an integer; // Output(screen): the base 10, 8 and 16 representations of aNumber. //**********************************************************************If you are working from a terminal or via telnet, you will have to type in this program; but if you are using the X-window environment, you can instead copy-and-paste the text above from your browser's window into the xemacs window. Let's learn how to do this next. In the X-window system, a block of text can be selected by dragging (holding down the left mouse button while moving the mouse) from the beginning to the end of the desired text. Most X environments support a copy-paste short-cut:
Using one of these methods, copy the program shown above into the xemacs
Personalizing the Program
As given above, If you are using xemacs in the X-windows environment, you can use the mouse to position the cursor at an arbitrary point by pointing at that point and clicking the left mouse button.
The editor command If you are running xemacs outside of the X-windows environment, you can move the cursor by the editor commands:
delete
(or backspace) key.
If you make a mistake, you can always undo the effects of any xemacs command,
either by using the undo command:
The undo command can be used to undo the effects of an xemacs command
that has completed.
However some xemacs commands take a significant length of time, and
you may wish to get out of the command while it is still being
performed.
For this, you can use the get-out command:
Saving Your Work.
When the Save file .../labs/0/bases.cpp? (y, n, !, ., q or C-h)To save the contents of the buffer in a file named bases.cpp,
answer 'yes' by typing y:
Save file .../labs/0/bases.cpp? (y, n, !, ., q or C-h) yXemacs will then confirm the save operation by displaying the Wrote file ... message shown above.
Since we have saved bases.cpp in the subdirectory
~/labs/0, we can visualize our situation as follows:
Translating Your Program
When your source program is entered and saved, it is time to translate it
into the computer's machine language.
In xemacs, this can be done using the editor compile command
Compile command: make -k
For simple programs like this one, the easiest way to translate our program
is to replace the Compile command: g++ bases.cpp -Wall -o basesThis changes the compile command from a call to make
(a program we will learn about in a few weeks) to an explicit call
to the GNU C++ compiler g++,
asking it to translate the program in the file bases.cpp.
The -Wall is an optional switch that tells g++ to
warn us on all potential sources of error.
The switch -o bases tells g++ that it should write its output
(i.e., the binary executable program) to a file named bases.
Once you have edited the compile command, press the Compilation finished at Mon Sep 5 9:12:44in the *compilation* buffer. If you instead see an error listing: bases.cpp: In function `int main()': . . . Compilation exited abnormally with code 1 at Mon Sep 5 9:12:44then you have made typing errors in entering the program. The editor next-error command: C-x `
(Control and x, followed by the left-quote (not the single-quote) key)
will move the cursor to the line in your source program where it discovered
the error (but the error may be on a previous line).
Compare the program above with what you have written and then change
what you have written to fix the error.
Then use the compile command again to see if you fixed the mistake.
Repeat this edit-compile cycle as many times as necessary,
until your program compiles correctly.
Once your program compiles correctly, g++ writes the resulting
executable file to the working directory, giving us this picture:
Use
Running Your Program from the Command-Line.
Running program
If you are using the X-window environment, the xemacs window is just one of
many independently running windows.
Locate a different window in which the system prompt is displayed
(i.e., the
If you are using a terminal or telnet instead of the X-window system,
use the xemacs suspend command:
Once you are at the operating system prompt, use % basesand your program should execute successfully. If it does not, ask your instructor for help.
Returning from the Command-line to XemacsIf you are using the X-window environment, simply move the mouse from your xterm window back into the emacs window. (If necessary, click the mouse.)
If you are using a terminal or telnet and you suspended xemacs using
% fgwhich changes xemacs from being suspended to being a foreground process.
Printing a Hard Copy
A paper copy of electronic information is called a hard copy.
It is often useful to have a hard copy of one's source program,
which is in the file In an AT&T UNIX command-line environment, you can usually print a hard copy of this file by entering % lp bases.cppor % lp -d PrinterName bases.cppwhile in the Berkeley UNIX command-line environment, you can usually print a hard copy by entering % lpr bases.cppor % lpr -PPrinterName bases.cppYour instructor will inform you of the details of printing at your institution.
Applying the Scientific Method
An important part of any science, including the science of computing,
is to be able to observe behavior, form hypotheses, and then design
and carry out experiments to test your hypotheses.
The next part of this exercise involves applying the scientific method
to infer (from the statements within
Observe
Using the mouse, position your xemacs and xterm windows so that you can
see both of them simultaneously.
Run the
HypothesisConstruct a hypothesis (i.e., a statement) that states how you think output text can be made to begin on a new line in a C++ program.
Experiment
Design an experiment using
Then modify
When your experimental hypothesis is not proven false, print a hard copy of
your modified As noted above, an experiment in which your hypothesis is not rejected does not prove that it is correct!
Miscellaneous Other Useful CommandsIn the remainder of this exercise, we examine some other useful commands.
Quitting xemacs
To quit xemacs (as opposed to suspending it), you can type
the editor quit command:
Copying a File
It is sometimes useful to be able to make a copy of a file,
so that we can modify a copy without modifying the original.
To make a copy of % cp bases.cpp bases.2
The
Use the
It's more often the case that we need to make a copy of a file in a
directory different from the one in which the file currently resides.
How are we to do this?
The key is to learn a bit about how files and directories are named.
Both of the names
However, every file or directory also has an absolute name:
a name that starts with the root directory (i.e., To use a file's (or directory's) absolute name, you need not be in the same directory as that file! For example, try entering: % mkdir ~/projects/0
What did we just do?
We used the
Almost all of the UNIX file manipulation commands accept either relative or absolute file names. For example, you don't need to be "in" a directory to see its contents: Enter % ls ~/projectsand you should see the directory you just made.
Similarly, to put a copy of % cp bases.cpp ~/projects/0and a copy of bases.cpp will be stored in that directory.
The
Using
Renaming a File
Sometimes we decide after the fact that a file would be better off
with a different name.
To change the name of % mv bases.2 bases.saveThe mv command changes the name of (or moves) the name of the file
on the left to the name on the right.
Our picture changes as follows:
It can be used with both relative and absolute file names, and can be used to rename directories.
Creating and Printing a Hard Copy of an ExecutionBesides a hard copy of one's program, it is often useful to have a hard copy of the output from one's program. To do this, we must
% scriptThe script command runs a program that records everything
that appears on the screen of a terminal
(or everything that appears within an xterm window).
Thus, if you subsequently execute bases,
then its output will be recorded by script.
Once bases has terminated, enter
% exitto terminate the script program,
which informs you that its recording is in a file named
typescript.
A hard copy of the file typescript can then be printed
using lp (or lpr), as described previously.
Cleaning Up Your Directory
The various files you have created take up valuable disk space,
and so we will remove all of the files from % rm bases.oldThe rm command removes (or deletes) whatever file you specify.
Note that it does not ask you for confirmation!
(If you want such confirmation, call rm with the -i switch.)
Earlier we saw that when rm is invoked with the -r switch,
it recursively removes a directory and everything within it.
Use the Then end your session with the computer, following your instructor's instructions. Note: If your head feels ready to explode, don't panic! This first lab covers a great deal of material, that you will use over and over again, and as you do so, you will begin to naturally memorize those commands that you use most frequently. You can speed up the process by reviewing each of the steps you took in this exercise. Until such a time as you remember the various commands, here are two quick reference pages to help you "look up" commands: one listing some of the commonly used UNIX commands and one listing some of the commonly used xemacs commands. Print hard copies of these, and store them in a convenient place! To learn to use these quick references, I would recommend that you review today's exercise, and find each of the commands we used in the exercise on the appropriate quick reference sheet. Then study the description of that command on the quick reference sheet, so that you associate the two in the future.
Submit:
Your original hard copy of
Phrases you should now understand:Environment, Command-Line, Directory, File, Editor, Compiler, Hard Copy, Printer.
|
Home Membership
|
|||||
|
Last update: Tuesday, August 28, 2001 at 4:30:55 PM. |
||||||