CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

Laboratory Exercise on Integer Processing

Goals:

This laboratory exercise provides practice with

Preliminaries

This lab utilizes several programs for processing integer values:

Work Started in Class

Sizes and Numeric Ranges of Various Integer Types

Program integer-sizes.c prints the number of bytes and bits allocated on your local computer for various types of integer data types.

  1. Compile and run program integer-sizes.c.
    1. Record how many bits and bytes are allocated for each type.
    2. Which, if any, integer types utilize the same amount of storage on your local machine?

Program integer-ranges.c reports the numerical ranges for various integer types for your local computer.

  1. Compile and run program integer-ranges.c
    1. Record the numeric ranges for each integer data type.
    2. Which, if any, integer types cover identical numerical ranges on your local machine?

The Fixed-Size-Storage Approach for Positive Integers

The first several experiments in this lab utilize a program integer-rep. This program is written in the C programming language. In this language (and in most others), integers are stored using a fixed-size-storage approach, as described in the reading.

  1. Copy integer-rep.c to your account and compile it. Run integer-rep, which initially shows the binary representation of the number 1. Use the operation "A" to add 1 to the values. Then use "A" again, and again, and again. (When starting with the value 1, the integers will become 2, 3, 4, and 5.)

    Review the binary representation of each integer, and explain why it has the binary representation printed. (In writing this explanation, consider how the 0's and 1's in a binary number are used to determine the number's value. Do NOT look at the coding for integer-rep.c, as that utilizes several C-programming techniques that are well beyond what we have studied to date in this course.)

  2. Use the "I" option to set the value to 12. Then use the "M" option 5 times, each time multiplying the values by 2 to obtain 24, 48, 96, 192, and 384. Explain why the binary representation of each integer looks as it does, including how the pattern of 0's and 1's obtained changes as you go from one of these numbers to the next.

  3. Try to use the "I" option to set the integer to a 5-digit positive integer (e.g., 10000), a 10-digit positive integer (e.g., 2000000000), a 15-digit positive integer, and a 20-digit positive integer. Explain what happens in each case.

The Variable-Size-Storage Approach for Positive Integers

While integer-rep.c illustrates how integers usually are stored in computers, a few environments utilize the variable-size-storage approach. The Scheme programming language and environment illustrates this alternative approach. With variable-size-storage, the binary representation does not have a 16-bit or 32-bit form; rather, the binary representation uses as many bits as needed. Our experiments will use the Scheme program integer-rep.ss.

  1. Copy integer-rep.ss, and run the program with the command

    scheme -q integer-rep.ss
    

    This program has much the same interface as the C version you used above.
    Check that the operations for entering integers, addition, subtraction, multiplication, and division work as they did with the C version:

  2. Following the same approach as Step 1, determine the binary representations for the positive integers 1, 2, 3, 4, and 5. To what extent are these the same or different from the fixed-size-storage approach?

  3. Repeat Step 5 with this Scheme-based script.

Negative Integers and Their Representations

Now that we have some experience with non-negative integers, we look at a few negative numbers.

The Fixed-Size-Storage Approach for Negative Integers

  1. As in Steps 1-3, use the C program integer-rep.c and the "I" option to set the program's values to -1, -2, -3, and -5. Review the 0's and 1's to determine whether the computer uses sign-magnitude notation, ones complement notation, or twos complement notation. Write a paragraph to justify your conclusion.

  2. Use the "I" option to enter -32766. Then use the "S" option four times to subtract 1 from -32766 . What results do you get with the 16-bit integer form and with the 32-bit integer? Explain why you get each result.

Homework

The Fixed-Size-Storage Approach for Negative Integers, Continued

  1. Use the "A" option several times to add 1 to your result of step 10. What can you conclude about the maximum integer that can be stored in 16 bits (assuming the processing is allowed to handle both negative and positive numbers)?

  2. Use a similar approach to find the maximum integer that can be stored in a 32-bit (signed) integer. Explain your result and how you got it.

  3. Read the news account of the computer-related difficulties that grounded all of Comair's flights on December 25, 2004. The article states, the computer system for Comair "has a hard limit of 32,000 changes in a single month." Other articles confirmed that this problem was due to a field in a database that was designed as a 16-bit integer.

    1. What do you think was the real (not rounded) limit for changes to crews at Comair?

    2. From what you know about the fixed-storage-approach for storing integers, identify one or two ways this problem could have been avoided.

The Variable-Size-Storage Approach for Positive Integers

  1. Following up on Steps 6-8 of this lab, use the command, scheme -q integer-rep.ss, typed in your terminal window, enter very large negative numbers (e.g., over 20 digits). Explain what happens.

Summary

  1. Review your experiments with both the fixed-size-approach and the variable-size-approach for storing integers, and write a paragraph summarizing the observations you have made.




created 14 January 2005 by Henry M. Walker
revised 11 February 2009 by Henry M. Walker
revised 29 October 2011 by Henry M. Walker
revised (program display) 20 July 2012 by Henry M. Walker
revised (reorganized, compressed) 16 January 2014 by Henry M. Walker
revised (updated with 2011 C Standard) 12 April 2016 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.