23
cosc175/data 1 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/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

Embed Size (px)

Citation preview

Page 1: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 1

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

Page 2: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 2

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

Page 3: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 3

Numbering Systems

Numbering system BASE Digits

Decimal 10 0 - 9

Octal 8 0 - 7

Hexadecimal 16 0 – F

Binary 2 0 - 1

Page 4: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

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

Page 5: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 5

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

Binary to Decimal

Page 6: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 6

Converting From Decimal to Binaryuse the division/remainder technique

 

1. Divide the number repeatedly by 2 and record the remainder of each division.

exa.

2 | 19

-----

2 | 9 ---> 1

-----

2 | 4 ---> 1

-----

2 | 2 ---> 0

-----

2 | 1 ---> 0

-----

0 ---> 1

2. Reverse:. 100112 = 1910

Page 7: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 7

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

 

Page 8: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 8

Octal to Binary

•Start with the rightmost digit• Look up the binary equivalent (3 digits)  5 38

101 0112

Page 9: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 9

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

Page 10: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 10

Hexadecimal to Binary

Perform the grouping procedure in reverse.   B 316

1011 00112

Page 11: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 11

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

Page 12: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 12

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

payRatehrsWorked

Page 13: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 13

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

Page 14: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 14

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, numbers, and special symbols

'A', 'a', '&','8' single quotes

String Combination of more than one character

"21093",

"Mrs Smith" Double quotes

Boolean TRUE, FALSE done, valid

Page 15: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 15

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

Page 16: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

integers

• whole numbers– positive or negative– counters - number of people or inventory

• What is the largest number stored in 32 bits?• Sign bit

• intint num1;int num2;int numStudents;

cosc175/data 16

Page 17: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 17

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

Page 18: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 18

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

Page 19: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

Declarations of numeric variables in C++

int test1;int test2;float avg;

cosc175/data 19

Page 20: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 20

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

Page 21: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 21

string

• set of characters

• + - concatenation - joins strings together– "4" + "4" = "44"

•  use string for zip code - allows you to hold leading 0'sDeclare string zipCode

zipCode = “21093”

Page 22: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 22

Logical Data

•  Boolean

• TRUE - yes

•  FALSE - no

•  used for decisions or tests

bool isEmpty;

Page 23: Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a

cosc175/data 23

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;