230 likes | 241 Views
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. Storage Capacity.
E N D
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
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
Numbering Systems cosc175/data
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
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 ---> 1 ----- 2 | 4 ---> 1 ----- 2 | 2 ---> 0 ----- 2 | 1 ---> 0 ----- 0 ---> 1 2. Reverse:. 100112 = 1910 cosc175/data
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
Octal to Binary • Start with the rightmost digit • Look up the binary equivalent (3 digits) • 5 38 • 101 0112 cosc175/data
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
Hexadecimal to Binary Perform the grouping procedure in reverse. B 316 1011 00112 cosc175/data
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
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
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
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
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; intnumStudents; cosc175/data
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
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
Declarations of numeric variables in C++ int test1; int test2; float avg; cosc175/data
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
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
Logical Data • Boolean • TRUE - yes • FALSE - no • used for decisions or tests bool isEmpty; cosc175/data
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