day02 - How the computer sees the world Data, expressions,...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 1 Data, expressions, and variables 2 How the computer sees the world s Internally, the computer stores everything in terms of 1’s and 0’s b Example: h B 0110100 "hi" B 01101000110101 104 B 0110100 s How can the computer tell the difference between an h and 104 ? 3 Data types s type : A category of data values b Example: integer, real number, character s C has many types (below is only a partial list) b integers: char , short , int , long b real numbers: float , double 4 Data types s Though the book uses many different types in the example programs, we will only concern ourselves with int and double for now. Name Description Examples int integers 42 , -3 , 0 , 926394 double real numbers 3.4 , -2.53 , 91.4e3 s Numbers with a decimal point are treated as real numbers. s Question: Isn’t every integer a real number? Why bother? 5 Integer or real number? s Which category is more appropriate? b credit: Kate Deibel, http://www.cs.washington.edu/homes/deibel/CATs/ real number ( double ) integer ( int ) 1. Temperature in degrees Celsius 2. The population of lemmings 3. Your grade point average 4. A person's age in years 5. A person's weight in kilograms 6. A person's height in meters 7. Number of kilometers traveled 8. Number of dry days in the past month 9. Your locker number 10. Number of seconds left in a game 11. The sum of a group of integers 12. The average of a group of integers 6 Manipulating data via expressions s expression : A data value or a set of operations that produces a value. b Examples: 1 + 4 * 3 3 "CSE142" (1 + 2) % 3 * 4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 7 The operators s Arithmetic operators we will use: b + addition b - subtraction or negation b * multiplication b / division b % modulus, a.k.a. remainder 8 Evaluating expressions s When C executes a program and encounters an expression, the expression is evaluated (i.e., computed). b Example: 3 * 4 evaluates to 12 9 Evaluating expressions: Integer division s When dividing integers, the result is also an integer. b Example: 14 / 4 evaluates to 3 , not 3.5 (truncate the number) 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 b Examples: s 1425 / 27 is 52 s 35 / 5 is 7 s 84 / 10 is 8 s 156 / 100 is 1 s 24 / 0 is illegal 10 Evaluating expressions: The modulus ( % ) s The modulus computes the remainder from a division of integers. b Example: 14 % 4 is 2 1425 % 27 is 21 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 s What are the results of the following expressions? b 45 % 6 b 2 % 2 b 8 % 20 b 11 % 0 11 Applying the modulus s What expression obtains… b the last digit (unit’s place) of a number? s Example: From 230857 , obtain the 7 . b the last 4 digits of a Social Security Number? s Example: From 658236489 , obtain 6489 . b
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/05/2010 for the course CSE cse142 taught by Professor Benson during the Spring '10 term at Punjab Engineering College.

Page1 / 8

day02 - How the computer sees the world Data, expressions,...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online