CS1711 Lab 2 - October 5 and 7, 1999
Exploring Arithmetic
Introduction
This lab will introduce you to the use of the numerical data structures
in C++. We begin with a note on saving executable files.
Executable and Object Files
A quick note. Whenever you compile a program, your .cpp file is
converted into machine instructions and stored in a file with a
.obj extension. When the program is linked, the .obj
file is then combined with a set of library files to produce the
executable program with extension .exe. If you find yourself
running out of room on your M: drive you may wish to open the
Debug folders and delete all files except the executable and
object files (or you may delete them all as you can quickly recreate
them.) The object and executable files will generally be larger
than the sources and you don't need them except to run the program. You
can alway recompile a program from the source file.
Note on storage:
The best way to protect your files from hazards is to
make duplicate copies of them. We call these backup copies.
You can do this simply by saving the file in two separate
places. If a file is important to you, always make sure you have
at least one backup copy of it. If it is extremely important, you
probably want more than one backup. The backup copies of your
files should not be kept at the same physical location as the original copy.
You might have one copy of a file on your personal computer's
hard disk and another copy on a floppy disk on your shelf (if the file is
important to you, the shelf should be in a different building!!). Or you
might have the original file on the server and the backup copy on a floppy
disk.
Investigating Integers
- Create a project called Testints on your M: drive
(in the folder CS1711Labs\Lab2 of course!)
- Add to your project the source file testints.cpp from the
T: drive folder for Lab 2. It contains a program
called TestInts that can be used to experiment with the C++
int data type. You may look at the program if you like,
although it contains C++ constructs not yet
covered in class.
- Compile and build the program. Run it and use it
to answer the questions listed in the remainder of this lab.
To get the maximum benefit from this lab,
think about each question and the answers you obtain as
you proceed. When possible, try to predict the answers before
using the program.
- Show the results of the following integer division operations:
(WRITE YOUR ANSWERS ON A SEPARATE SHEET OF PAPER!!)
a. 20 / 4
b. 23 / 4
c. 23 / - 4
d. - 20 / - 4
e. - 23 / - 4
f. - 23 / 4
- When is the result of an integer division operation negative?
- Show the results of the following % operations:
a. 20 % 4
b. 23 % 4
c. 20 % - 4
d. 23 % - 4
- When is the result of a % operation negative?
- "(A % B) % C" does not always equal "A %
(B % C)". In other words, the % operation on integers is not
associative. List values of A, B, and C that will result in the
given values of "(A mod B) mod C" and "A mod (B mod C)". The first
line is completed. Although this is not an easy exercise, there
are many correct answers to each question. Try to arrive at your
answers logically, instead of just guessing and testing.
A
|
B
|
C
|
(A % B) % C
|
A % (B % C)
|
11
|
5
|
2
|
1
|
0
|
16
|
?
|
?
|
1
|
0
|
?
|
?
|
?
|
0
|
0
|
?
|
?
|
?
|
0
|
1
|
?
|
?
|
?
|
2
|
1
|
Experiment: Expression Evaluation
The classic experimental method can be used in Computer
Science to help design languages and user interfaces and to evaluate
solutions to problems.
In the experimental method, a an hypothesis is posed and an
experiment is designed to help
prove or disprove the hypothesis. The experiment is performed, data
is collected, and results are analyzed.
This method could be used in computer science is such diverse ways
as:
- To determine idiosyncrasies of a compiler
- To evaluate and compare software design techniques
- To determine the efficiency of a specific program on a
specific computer
We will do some exercises called
Experiments that require you to devise and run an experiment and
complete a report.
The report is similar to a Science Lab
report such as one you might use for a Physics lab. Each report will
have the same basic sections:
- Identification: Who, when, and what system.
- Question: What the experiment is to determine.
- Method: A description of the experiment's design.
Be sure to consider all
possibilities. Use more than one sample run.
- Results: The outcome of the
experimental program(s).
- Conclusions: Based on the results, answer the
question, if possible. Otherwise, explain why it is not possible to
answer the question.
After completing this experiment, show your report to the lab assistant
and demonstrate your program.
Question:
In the text, Section 2-5, Working with C++ Operators,
various rules for expression evaluation
are described (see pp 66-67). Verify that our system adheres to
the operator precedence rules.
To answer this question you should write a program that asks the user
for several double numbers (x, y, z, ...) as input and then
-
evaluates the expressions
-
(x+y)*z
-
x+(y*z)
-
x+y*z
and reports the results.
-
evaluates the expressions
-
-x+y
-
-(x+y)
-
(-x)+y
and reports results.
Be prepared to show the lab instructor a run of your program that
demonstrates the results of your investigation.
End of Lab
Make sure you have shown your report to the lab assistant
so that you receive credit for the lab.