Data Representation

Download Report

Transcript Data Representation

Data
•
•
•
•
•
comprised of constants and variables
information stored in memory
Each memory location has an address
Address - number identifying a location
can do 2 things to data
– read - doesn't change contents
– write - replaces contents
cosc175/data
1
Storage Capacity
• bit - 0 or 1, electrical states - on and off
– nibble - 4 bits
• byte - 8 bits, holds 1 character
• word - number of bits handled as a unit for a
particular computer system
• K,Kb - kilobyte
– 1024 bytes - approximately 1000 locations
– 640K - 640*1024 = 655,360 bytes
– 640K - approximately 640,000 locations
• Mb - Megabyte - 1024*1024, millions of bytes
• Gb - Gigabyte - 1 billion bytes
cosc175/data
2
Numbering Systems
Numbering system
Decimal
Octal
Hexadecimal
BASE
10
8
16
Digits
0-9
0-7
0–F
Binary
2
0-1
cosc175/data
3
Binary
Octal
Hex
Decimal
0000
0
0
0
0001
1
1
1
0010
2
2
2
0011
3
3
3
0100
4
4
4
0101
5
5
5
0110
6
6
6
0111
7
7
7
1000
10
8
8
1001
11
9
9
1010
12
A
10
1011
13
B
11
1100
14
C
12
1101
15
D
13
1110
16
E
14
1111
17
F
15
10000
20
10
16
Binary to Decimal
Multiply the 1's in a binary number by their position
values, then sum the products.
Exa.
1 1 0 1 02
0 x 2^0 = 0
1 x 2^1 = 2
0 x 2^2 = 0
1 x 2^3 = 8
1 x 2^4 = 16
---2610
cosc175/data
5
Converting From Decimal to Binary
use the division/remainder technique
1. Divide the number repeatedly by 2 and record the remainder of each division.
exa.
2 | 19
----2 | 9 --->
----2 | 4 --->
----2 | 2 --->
----2 | 1 --->
----0 --->
2. Reverse:.
1
1
0
0
1
100112 = 1910
cosc175/data
6
Binary to Octal
•Start with the rightmost digit
• group by 3 digits
•Look up the octal equivalent
1 0 0 0 0 1 1 1 02
------ ------ ------4
1
68
cosc175/data
7
Octal to Binary
•Start with the rightmost digit
• Look up the binary equivalent (3 digits)
5 38
101 0112
cosc175/data
8
Binary to Hex
•Start with the rightmost digit
• group by 4 digits
•Look up the hex equivalent
1 1 0 0 0 1 0 12
-------- --------C
516
cosc175/data
9
Hexadecimal to Binary
Perform the grouping procedure in reverse.
B
316
1011 00112
cosc175/data
10
Variables and Constants
• constant
– alphabetic or numeric value that never changes during
processing
– can be any data type
– can also be used for something that may change at a
later date
– need only be changed in one place
– Aids readability
– const float PI = 3.14;
– const float MD_TAX_RATE = .06;
– Note, use caps for constants
• variable - value does change during process
cosc175/data
11
variable names
• Corresponds to memory location or address
• Naming convention varies varies from language to
language
• Alphanumeric,begin with alphabetic characters
• no spaces in variable names
• any number of characters
• meaningful names
• exa
payRate
hrsWorked
cosc175/data
12
example
• solution that calculates payroll for a
company:
const string COMP_NAME = “COMPANY1”;
const float OT_RATE = 1.5;
string empName;
float hoursWorked;
float payRate;
• values change for each employee
• can use one program and modify each
variable to process 1000 employees
cosc175/data
13
Data Types
Data set
Examples
Integer
All whole numbers
3580, -90, 6
Float (or real)
All real numbers
37.92, -8.3, 100.0
Character
All letters, symbols, 'A', 'a', '&','8'
numbers, and
single quotes
special symbols
String
Combination of
more than one
character
"21093",
"Mrs Smith"
TRUE, FALSE
done, valid
Boolean
cosc175/data
Double quotes
14
Numerical Data
•
•
•
•
•
•
all types of numbers
used in calculations
use numerical data for values such as salary, hours
not zip-code or social security number
int
float or double
cosc175/data
15
integers
• whole numbers
– positive or negative
– counters - number of people or inventory
• What is the largest number stored in 32 bits?
• Sign bit
• int
int num1;
int num2;
int numStudents;
cosc175/data
16
real numbers
• whole numbers plus the decimal part
• float or double
• precision - number of significant digits
– number of digits visible in the number
• magnitude - size of the number measured
by the power of ten
– 3.4598723 x 10.9
– 8 significant digits, magnitude of 9
– 348 million - 3 significant digits
cosc175/data
17
precision
• precision varies with the computer
• greater the number of significant digits -> greater
precision or accuracy
• the computer converts fractions to decimal and
decimal to binary - > round-off errors
• for exa. 1/3 + 1/3 + 1/3
.333333333 + .333333333 + .333333333 =
.999999999
• use smallest precision possible for efficiency
cosc175/data
18
Declarations of numeric variables
in C++
int
int
float
test1;
test2;
avg;
cosc175/data
19
characters
• all letters, numbers, and special characters
Declare character inputChar
• BCD - convert each digit into binary form (uses 4 bits)
• ASCII - American Standard Code for Information
Interchange
– 7 bits
– used for pc's
– http://www.asciitable.com/
• EBCDIC - Extended Binary Coded Decimal Interchange
Code
– 8 bits
– IBM mainframes
cosc175/data
20
string
• set of characters
• + - concatenation - joins strings together
– "4" + "4" = "44"
• use string for zip code - allows you to hold
leading 0's
Declare string zipCode
zipCode = “21093”
cosc175/data
21
Logical Data
•
•
•
•
Boolean
TRUE - yes
FALSE - no
used for decisions or tests
bool isEmpty;
cosc175/data
22
Declaring Data Types
• Variables must be declared before use.
• Declaration defines type, allocates space in
memory.
• variable names - corresponds to address
• type name
string studentName;
float payPerHr;
cosc175/data
23