Upload
others
View
20
Download
0
Embed Size (px)
Citation preview
Variables, Data Types, and Arrays
Session 2, COMP 11
Variable: What is it?
f(x) = x + 17In Mathematics (what you’re used to):
In Computer Science:
x
42
Name (label)
Value (contents)
Type (size)
Variables: “Declaration”
● Defines the name (label) and type (size) of a new variable (box).● General syntax (what the code looks like): type name;● Example:
int fahrenheit;
TypeName Semicolon
Variables: Naming Conventions
Examples of valid variable names:
● x● sum● AVERAGE● myName● todays_date● text2num● Area51● tHiSabOmiNaBLeThiNG
Examples of invalid variable names:
● 5names● money$ign● email.address● int● Suzie’s_birthday● my variable● X-mas● .sysvar
1. Names may contain letters, numbers, and underscores.2. First character must be a letter.
Our first type: int
● Integer number (no fraction/decimal)● Examples: 0, -343, 2819● Range is limited!
○ Calculator -- 8 decimal (0-9) digits○ int -- (usually) 32 binary (0-1) digits○ bit = “binary digit”
● Declaring an integer variable:
int name; /* comment */
Variables: Assignment
int number; /* a new integer */
number
?
“Uninitialized Variable”- A declared variable holding an
unknown (“garbage”) value.
/* An assignment statement */
number = (1 + 2) * 2;
number
6
“Assignment” -- putting a new value in a variable“Initialize” -- put a value in a variable for the first time
Variables: Assignment
● General syntax: name = expression ;1. “Variable ‘name’ gets the value of ‘expression’.”2. Compute the expression on the RIGHT of the = to get a value3. Assign that value to the variable named on the LEFT of the =
WARNING: The right-hand side is always evaluated first to get a value, which is then stored in the box called “name”.
Variables: Using them
int x;
int y;
x = 42;
y = x + 3;
x = x + 1;
After the last line…1. What value is stored in x?2. What value is stored in y?
x y ? N/A ? ? 42 ? 42 45 43 45
Example: temperature.cpp
Our second type: float (and double)
● “Floating-point” number (has a decimal point)● Examples: 0.0, -343.25, 0.2819● Range is still limited!
○ (usually) 32 bits
● double: 64-bit float ○ More Precision: 3.1415927 vs. 3.1415927410125732○ Important for physical simulations
Our third type: char
● Strings like “Hello World” are comprised of characters○ Character: “a symbol representing a letter or number” (Google’s 3rd definition)
● The char type: a value representing a single character● Some char constants: 'a' 'A' '0' ' ' '\n' '\''● WARNING: Use single quotes to denote a char constant● Range is STILL limited: 1 byte (8 bits)
H e l l o01001000 01100101 01101100 01101100 01101111
<space>00100000
W o r l d01010111 01101111 01110010 01101100 01100100
ASCII Encodings (subset of “Unicode”)
From “ASCII” Wikipedia article http://cs.wellesley.edu/~cs110/reading/information-representation.html
Our fourth type: bool
● Boolean → True or False● Only 2 bool constants: true and false
● Named after George Boole → invented “boolean algebra”
Strings
● A “string” is a sequence of characters in double quotes● Some string constants: "hello" "h" " " "***l33t h4x0rz***"● Different from other types:
○ Not primitive → defined in a library (like <iostream>)○ Has observable properties → size, emptiness○ Individual characters are accessible
strings.cpp
Array: General Concept
● Data structure: organize collections of data efficiently● Array: collection of elements of the same type● Fixed size
27 42 222 -382 0
● Access/modify elements via “array index”
0 1 2 3 4WARNING: Arrays are always 0-indexed.
The last element of an array of size n is at index n - 1.
“Elements”
Arrays in C++
int seats[20];
Declaration1. Type of elements2. Name of array3. Size of array in [ ]
float fractions[3];
char alphabet[26];
int nums[3] = {1,2,3};
float decimals[2];
decimals[0] = -38.253;
decimals[1] = 0.25;
int sum = nums[0] + nums[1] + nums[2];
nums[0] = sum;
nums[sum - 6] = 1;
InitializationSpecify 1 or more values in { }.
Accessingarr[index] (index can be any integer expression)
Array Dimensions
int nums[5];
int nums2D[3][5];
int rubiks[3][3][3];
1-Dimensional (1D)
2-Dimensional (2D)
3-Dimensional (3D)
Summary
● What is a variable?● Difference between “assigning”, “initializing”, and “declaring” a variable?● What are the different types we’ve learned about?
○ What are their ranges?
● What’s weird about division in C++?● What is an array? What are some warnings about how to use them?