# day02 - Data, expressions, and variables 1 How the computer...

This preview shows pages 1–12. Sign up to view the full content.

1 Data, expressions, and  variables

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

View Full Document
2 How the computer sees the world Internally, the computer stores everything in terms of 1’s and 0’s Example: h 0110100 "hi" 01101000110101 104 0110100 How can the computer tell the difference between an h and 104 ?
3 Data types type : A category of data values Example: integer, real number, character C has many types (below is only a partial list) integers: char , short , int , long real numbers: float , double

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

View Full Document
4 Data types 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 Numbers with a decimal point are treated as real numbers. Question: Isn’t every integer a real number? Why bother?
5 Integer or real number? Which category is more appropriate? credit: Kate Deibel, http://www.cs.washington.edu/homes/deibel/CATs/ integer ( int ) real number ( double ) 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

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

View Full Document
6 Manipulating data via expressions expression : A data value or a set of operations that produces a value. Examples: 1 + 4 * 3 3 "CSE142" (1 + 2) % 3 * 4
7 The operators Arithmetic operators we will use: + addition - subtraction or negation * multiplication / division % modulus, a.k.a. remainder

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

View Full Document
8 Evaluating expressions When C executes a program and encounters an expression, the expression is evaluated (i.e., computed). Example: 3 * 4 evaluates to 12
9 Evaluating expressions: Integer division When dividing integers, the result is also an integer. Example: 14 / 4 evaluates to 3 , not 3.5 (truncate the number) 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 Examples: 1425 / 27 is 52 35 / 5 is 7 84 / 10 is 8 156 / 100 is 1 24 / 0 is illegal

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

View Full Document
10 Evaluating expressions: The modulus ( % ) The modulus computes the remainder from a division of integers. Example: 14 % 4 is 2 1425 % 27 is 21 3 52 4 ) 14 27 ) 1425 12 135 2 75 54 21 What are the results of the following expressions? 45 % 6 2 % 2 8 % 20 11 % 0
11 Applying the modulus What expression obtains… the last digit (unit’s place) of a number? Example: From

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## day02 - Data, expressions, and variables 1 How the computer...

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

View Full Document
Ask a homework question - tutors are online