Advanced Placement Computer Science AB:
Assignment 3
    Sewickley Academy, 2000-2001

See Course Home Page.  Also, see the Solutions to Assignment 3

Note:  Solo only -- you may not work in teams on this assignment.
 
Date Assigned: Mon Sep-18
Date Due: Wed Sep-20 by start of P8

1.  Write a program which reads in nonnegative integers repeatedly and for each number indicates the smallest type necessary to store that number.  Your program should terminate if the number input is 0 (be sure to provide a suitable message to that effect).  It should also limit itself to the following types:  char, unsigned char, short, unsigned short, int, unsigned int, long, unsigned long.  You are guaranteed that the number input will fit into at least one of those types.  You may assume the types are the size found in the CS Lab machines running NT 4 (since the sizes do depend on the machine and potentially the OS).  Finally, if more than one type has the same size, then print them all out.  Here is some sample output (your code must match this output as exactly as possible, including, for example, placing double quotes around the types, and the carriage return before the input prompt):

Sample Output (underlined portions are user input):

Please enter a number (0 to exit) -->
123
     123 requires the type "byte"

Please enter a number (0 to exit) -->
128
     128 requires the type "unsigned char"


2.  Write a program which finds the largest possible unsigned short by starting at 0 and iterating upwards until overflow occurs.  Print out the largest possible unsigned short which this program discovers.

3.  Write a program which prints out a 16-bit number in hexadecimal, initially all 0's, and repeatedly takes one input.  If the input is between 1 and 16, the program sets that bit (16 is far left, 1 is far right) to 1.   If the input is between -1 and -16, the program sets the corresponding bit to 0.  If the input is 0, the program exits.  Otherwise, the program prints some informative and kind message and the user tries again.

Sample Output:

Current output:  0x0000
Enter bit to set (1 to 16) or clear (-1 to -16) or 0 to end: 1

Current output:  0x0001
Enter bit to set (1 to 16) or clear (-1 to -16) or 0 to end: 2

Current output: 0x0003
Enter bit to set (1 to 16) or clear (-1 to -16) or 0 to end: -1

Current output: 0x0002
...



See Course Home Page.