The main purpose of this lab is to introduce you to the computing environment of your laboratory. You will use the ideas in this lab again and again throughout this course, so you should make every effort to understand not only what but why you are doing what you are doing at each step.
Before we can begin our session, your instructor will inform you how to begin a session with the computer at your particular institution. The way this is done differs from school to school, according to the kind of computer being used, whether they are networked or stand-alone, whether a security system is in place, and so on. You should ask and get answers to these questions:
Throughout this lab manual, instructions will be printed in this
default font (the one you are reading). To help you distinguish the
instructions from what appears on your screen, text that you should
see displayed on your screen will be shown in this typewriter
font
.
There's a subtle distinction between typing and entering information:
y
.y
, followed by the key marked Return
or Enter
.First, you have to become familiar with the operating system you're using (if you're not already). The answer to the first question in the list of questions at the beginning of this exercise will tell you which to read:
You may find it challenging enough getting used to your operating system, so don't be afraid to play around to get used to it. Keep the appropriate instructions handy for every lab since you'll need to create folders and copy files for every lab.
There are many different compilers you can use to compile C++ programs. Pick yours from this list:
As with the computing environment, you'll continually need these instructions for every lab. Bookmark the appropriate instructions so that you can use them for every lab.
This is the program that you need for the environment instructions in the previous section.
/* bases.cpp demonstrates basic I/O in C++. * * Written by: Jane Doe, Feb 29, 1999. * Written for: CS I, at City University. * * Specification: * Input(keyboard): aNumber, an integer; * Output(screen): the base 10, 8 and 16 representations of aNumber. ***********************************************************************/ #include <iostream> using namespace std; int main() { // declare an integer container to hold the input number int aNumber; // 0. print a message explaining the purpose of the program. cout << "\nThis program inputs a base-10 integer" << "\n\tand displays its value in bases 8 and 16\n"; // 1. ask the user to enter an integer. cout << "\nPlease enter an integer: "; // 2. input an integer, storing it in variable aNumber. cin >> aNumber; // 3. output the base-8 and base-16 representations of aNumber. cout << "\n\nThe base-8 representation of " << aNumber << " is " << oct << aNumber << ",\n\tand the base-16 representation is " << hex << aNumber << "\n\n"; }
After you've successfully copied, compiled, and executed the program above, continue on with this exercise.
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 bases.cpp
) how the certain aspects of
C++ output work. Since two heads are (sometimes) better than one,
feel free to work through this section with the person sitting next
to you.
Run the bases
program one or more times and study its
behavior; then study the text of bases.cpp
. Here's the
question we want to address: What in the world do the symbols \n
and \t
do for the program?
Question #0.1: Construct a hypothesis (i.e., a statement) that states what you think the symbols\n
and\t
do in the program.
Hint: they do slightly different things, but they both have an effect on the output of the program.
You do not have to be right yet; you're simply making a prediction. If your experimentation proves the hypothesis, then your conclusion is easy; if your experimentation proves the hypothesis is wrong, then you change it for your conclusion. It's really the conclusion that matters.
Design an experiment using bases.cpp
that tests your
hypothesis. Modify bases.cpp
as necessary to perform your
experiment, compile the program, and run it.
Question #0.2: What results did you get?
Question #0.3: Do these results confirm or contradict your hypothesis?
If the experiment confirms the hypothesis, construct another experiment, a tougher experiment to test the hypothesis further. If the experiment contradicts the hypothesis, come up with a new hypothesis, and try it all again.
For each iteration through this process, write down your hypothesis, your experiment, and your results.
Question #0.4: What conclusion have you reached about the\n
and\t
symbols?
Question #0.5: Why do you suppose that these symbols are known as whitespace characters?
You may find it very useful to compare your hypotheses and experiments with the hypotheses and experiments of your fellow classmates; remember: the more experiments you have, the stronger your hypothesis.
Turn the answers to the questions in the lab exercise. Turn in a copy of the program that you copied over and compiled. Also turn in a sample execution of your program.