CS1711 Lab 2 - September 29 and October 1, 1998
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.
Make sure you have signed the log book
so that you receive credit for the lab.
Review of login
- Log into the Local Area Network (LAN) by giving your username and
password.
- Open `My Computer' and connect \\home\deptfile to the T:
drive.
- Open the `Turbo C++ 3.0' item under the PC Lab item from the
Start Menu.
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
TC30 directory and delete all files except the source files
(.CPP). 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 in the same place 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. Or you
might have the original file on the server and the backup copy on a floppy
disk.
Investigating Integers
- Obtain the file TESTINTS.CPP from
T:\COMP_SCI\FORD\1711\LABS\LAB2. 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.
- Prepare the program for execution. Run it and use it
to answer the questions listed in the remainder of this lesson.
To get the maximum benefit from this lesson,
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 that can be used in Computer
Science to help design languages and user interfaces and to evaluate
solutions to problems.
In the experimental method, a problem or hypothesis is posed and an
experiment is designed to help resolve the problem or
prove/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 classic 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.
FOR THIS EXPERIMENT YOU SHOULD SHOW YOUR REPORT TO THE LAB ASSISTANT AND
DEMONSTRATE YOUR PROGRAM.
Question:
In the text, Section 2.3, Arithmetic Operators
and Expressions, various rules for expression evaluation
are described (see page 61). Verify that your 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.