Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC...
-
Upload
lucas-cole -
Category
Documents
-
view
214 -
download
1
Transcript of Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC...
Copyright © 2002 W. A. Tucker 1
Chapter 7 Lecture Notes
Bill Tucker
Austin Community College
COSC 1315
Copyright © 2002 W. A. Tucker 2
Brief Review
• This course has presented the following different ways for programs to utilize values stored within the computer– Named Variables– Named Constants– Literal Values– Objects
• Attributes
Copyright © 2002 W. A. Tucker 3
Brief Review
• All values, in this course, can be represented by five different data types– int – whole number– double – real number– char – a single character – bool – boolean – string – a “collection” of characters
• How are these stored in memory?– Every value is stored as a binary number
Copyright © 2002 W. A. Tucker 4
Decimal Number System
• Our comfort zone is with the decimal number system (base 10)
• This system uses 10 characters, the digits 0 through 9
• Look at a typical number such as 57310
– 5 is called the “hundreds position” – 7 is called the “tens position”– 3 is called the “units position”
Copyright © 2002 W. A. Tucker 5
Decimal Number System
10 2 10 1 10 0
= 100 = 10 = 1
5 7 3
5 x 100 = 500 7 x 10 = 70 3 x 1 = 3
573
Copyright © 2002 W. A. Tucker 6
Binary Number System
• This system uses only 2 characters, the digits 0 through 1 (binary system)
• This is how computers work internally since logic circuits are either ON or OFF
• Look at a typical number such as 10102
– What does this mean?
Copyright © 2002 W. A. Tucker 7
Binary Number System
2 3 2 2 2 1 2 0
= 8 = 4 = 2 = 1
1 0 1 0
1 x 8 = 8 0 x 4 = 0 2 x 1 = 2 0 x 1 = 0
10102 = 1010
Copyright © 2002 W. A. Tucker 8
Hexadecimal Number System
• Hexadecimal refers to a number system to the base 16
• Since writing or reading a long string of 1’s and 0’s is quite difficult, the hexadecimal system is often used to represent 4 binary digits with just one hexadecimal digit.
• The hexadecimal digits are 0-9, and A-F where:
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15
Copyright © 2002 W. A. Tucker 9
Sizes
• A single binary digit is called a bit• A collection of 8 bits is called a byte• A collection of byes is called a word• The word size may vary, computer by
computer• How many numbers of what size can be
stored inside a computer?– n bits can represent a number up to (2n -1)– n bits can represent 2n different numbers
Copyright © 2002 W. A. Tucker 10
Maximum Number of Numbers
n (bits) 2n n (bits) 2n
1 2 9 512 2 4 10 1024 3 8 11 2048 4 16 12 4096 5 32 13 8192 6 64 14 16384 7 128 15 32768 8 256 16 65536
Copyright © 2002 Jade Lindquist 11
Maximum Numeric Values
Since 0 is a positive number, the maximum positive numeric value that can be stored in a number of n bits is 2n – 1.
For example, the numbers 0-7 (0002 – 1112)
can be stored in a number consisting of 3 bits.
Copyright © 2002 Jade Lindquist 12
Maximum Numbers
n (bits) 2n - 1 n (bits) 2n - 1 1 1 9 511 2 3 10 1023 3 7 11 2047 4 15 12 4095 5 31 13 8191 6 63 14 16383 7 127 15 32767 8 255 16 65535
Copyright © 2002 Jade Lindquist 13
In Practice
Although 16 bits could store a number as large as 65535, in practice, one bit is used to store the sign of the number.
Thus a short integer (16 bits or 2 bytes) contains numbers in the range of -32768 to 32767 because 1 bit is used to store the sign of the number and only 15 bits are available to store the number.
Copyright © 2002 W. A. Tucker 14
Whole Numbers (Integers)
• Integers are represented internally in the computer exactly
• This means that a 12 is stored in binary as a 12 (11002)
• The maximum size of an integer depends upon how many bytes are used to store integers
• For Visual Studio 6.0 on a PC integers take 4 bytes– Largest value is +2,147,483,647– Smallest value is -2,147,483,648
Copyright © 2002 W. A. Tucker 15
Real Numbers (double)
• Real numbers are represented internally in the computer approximately
• Since the decimal point may move, the term floating point number is often used
• Real Numbers are stored in logarithm format, with a characteristic and mantisa
• The number of bytes used for a double value in Visual Studio 6.0 on a PC is 8 bytes– Values are ± 1.7 e ± 308
Copyright © 2002 W. A. Tucker 16
Characters
• Characters are encoded – Except for IBM mainframes, most computers encode
characters in ASCII (American Standard Code for Information Interchange)
– An upper case letter has a different code than the lower case of the same letter
– Numbers, stored as characters, are also stored in ASCII representation
– Characters on a Personal Computer using C++ are stored in a single byte of memory
• This allows for 256 different ASCII characters
Copyright © 2002 W. A. Tucker 17
Some ASCII Examples
Character ASCII Binary
Letter A 6510 010000012
Letter a 9710 011000012
Number 1 4910 001100012
Copyright © 2002 W. A. Tucker 18
String Values
• String is a “user defined data type” which is defined as a class
• All variables of the string “data type” are actually objects of the class string
• Strings are stored internally in the computer as a collection of characters
• The length of the string is an attribute (data value) of each object of class string
Copyright © 2002 W. A. Tucker 19
Arithmetic and Data Types
• The compiler will perform arithmetic on values based on their data types– Arithmetic with all integers produces an
integer result– Arithmetic with all doubles produces a double
result– Arithmetic with mixed data types will produce
a result equal to the most complex data type in the expression
Copyright © 2002 W. A. Tucker 20
Mixed Type Arithmetic
• The compiler will “promote” a data type so that the computer can do arithmetic with same type values– This promotion is only done when it is required– In the following example the division is performed as
integer division, with an integer result, but the result (which has no decimals) is then promoted to double prior to the multiplication.
– EX: int a, b;double c, result;result = a / b * c;
Copyright © 2002 W. A. Tucker 21
Explicit Type Casting
• The compiler can be instructed to change the type of a value, for one instance, to produce different results.– The previous example can be changed to keep the
decimal value by inserting an explicit type cast statement. The value of b will be promoted to a double forcing the division to produce a double result prior to the multiplication step.
EX: int a, b;double c, result;result = a / double (b) * c;
Copyright © 2002 W. A. Tucker 22
Type Casting
• Type casting can also be used to prevent compiler warning messages which would occur when trying to store a complex data type (like double) into a simpler data type (like int) since precision would be lost by deleting all decimal placesEX: int result;
double a, b;result = int (a / b);
Copyright © 2002 W. A. Tucker 23
Type Casting
• Type casting may also be used to convert characters to their ASCII integer and visa versa:– EX: int ascii;
char letter;
letter = char (ascii);
ascii = int (letter);
Copyright © 2002 W. A. Tucker 24
Character Built In Functions
• There are many built-in functions for character operations that are available by merely adding another compiler directive #include <cctype>
• A partial list of these functions can be found on page 356 of the text
• Some of the most useful are toupper and tolower, that will force a character to be either the upper case ASCII or lower case ASCII code
• Note that many of the functions return boolean values and are called “is ____”, like asking a question
Copyright © 2002 W. A. Tucker 25
String class functions
• There are several member functions of the string class that are useful when working with strings– A partial list may be found on page 150
• The format for calling a member function uses the dot member operator
• To call the member function length for object lastName of class string and assign the length to the integer size
int size; size = lastName.length();
Copyright © 2002 W. A. Tucker 26
Using Boolean Functions
• The use of a boolean function, with an appropriate name, often makes programs more readable
Ex: looping a program while the user wants to continue
do{
// body of program
} while (wantToContinue()); // loop while the user// wants to continue
Copyright © 2002 W. A. Tucker 27
Boolean Functions
• Many useful functions can be written to return boolean values
bool wantToContinue(){
char answer;cout << “Do you want to continue(Y/N?);cin >> answer;if (answer == ‘Y’ || answer == ‘y’) return true;
else return false; }
Copyright © 2002 W. A. Tucker 28
Boolean Return Values
• Since condition statements equate to a boolean value, they may be returned directly as boolean values
bool wantToContinue(){ char answer; cout << “Do you want to continue(Y/N?); cin >> answer; return (answer == ‘Y’ || answer == ‘y’)}