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

  1. Log into the Local Area Network (LAN) by giving your username and password.
  2. Open `My Computer' and connect \\home\deptfile to the T: drive.
  3. 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

  1. 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.
  2. 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.
  3. 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
  4. When is the result of an integer division operation negative?
  5. Show the results of the following % operations:
    a. 20 % 4
    b. 23 % 4
    c. 20 % - 4
    d. 23 % - 4
  6. When is the result of a % operation negative?
  7. "(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:
  1. To determine idiosyncrasies of a compiler
  2. To evaluate and compare software design techniques
  3. 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:
  1. Identification: Who, when, and what system.
  2. Question: What the experiment is to determine.
  3. Method: A description of the experiment's design. Be sure to consider all possibilities. Use more than one sample run.
  4. Results: The outcome of the experimental program(s).
  5. 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
  1. evaluates the expressions
    1. (x+y)*z
    2. x+(y*z)
    3. x+y*z
    and reports the results.
  2. evaluates the expressions
    1. -x+y
    2. -(x+y)
    3. (-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.