Download presentation
Presentation is loading. Please wait.
Published byLucas Cole Modified over 9 years ago
1
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315
2
Copyright © 2002 W. A. Tucker2 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
3
Copyright © 2002 W. A. Tucker3 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
4
Copyright © 2002 W. A. Tucker4 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 573 10 –5 is called the “hundreds position” –7 is called the “tens position” –3 is called the “units position”
5
Copyright © 2002 W. A. Tucker5 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
6
Copyright © 2002 W. A. Tucker6 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 1010 2 –What does this mean?
7
Copyright © 2002 W. A. Tucker7 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 1010 2 = 10 10
8
Copyright © 2002 W. A. Tucker8 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
9
Copyright © 2002 W. A. Tucker9 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 (2 n -1) –n bits can represent 2 n different numbers
10
Copyright © 2002 W. A. Tucker10 Maximum Number of Numbers n (bits) 2 n n (bits) 2 n 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
11
Copyright © 2002 Jade Lindquist11 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 2 n – 1. For example, the numbers 0-7 (000 2 – 111 2 ) can be stored in a number consisting of 3 bits.
12
Copyright © 2002 Jade Lindquist12 Maximum Numbers n (bits) 2 n - 1 n (bits) 2 n - 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
13
Copyright © 2002 Jade Lindquist13 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.
14
Copyright © 2002 W. A. Tucker14 Whole Numbers (Integers) Integers are represented internally in the computer exactly This means that a 12 is stored in binary as a 12 (1100 2 ) 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
15
Copyright © 2002 W. A. Tucker15 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
16
Copyright © 2002 W. A. Tucker16 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
17
Copyright © 2002 W. A. Tucker17 Some ASCII Examples Character ASCII Binary Letter A 65 10 01000001 2 Letter a 97 10 01100001 2 Number 1 49 10 00110001 2
18
Copyright © 2002 W. A. Tucker18 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
19
Copyright © 2002 W. A. Tucker19 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
20
Copyright © 2002 W. A. Tucker20 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;
21
Copyright © 2002 W. A. Tucker21 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;
22
Copyright © 2002 W. A. Tucker22 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 places EX: int result; double a, b; result = int (a / b);
23
Copyright © 2002 W. A. Tucker23 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);
24
Copyright © 2002 W. A. Tucker24 Character Built In Functions There are many built-in functions for character operations that are available by merely adding another compiler directive #include 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
25
Copyright © 2002 W. A. Tucker25 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();
26
Copyright © 2002 W. A. Tucker26 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
27
Copyright © 2002 W. A. Tucker27 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; }
28
Copyright © 2002 W. A. Tucker28 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’) }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.