cs110 lec 5

Cards (23)

  • Named constant
    A location in memory that we can refer to by an identifier, and in which a data value that cannot be changed is stored
  • Named constants
    • const string STARS = "****"
    • const float NORMAL_TEMP = 37.0
    • const char BLANK = ' '
    • const int VOTING_AGE = 18
    • const float MAX_HOURS = 40.0
  • Activity: Variables or Constants?
    • Pi
    • Coefficient of Friction of a Material
    • Number of power plants in Saskatchewan
    • Viscosity of a fluid
    • Days in a Week
    • Sales tax rate in Saskatchewan
    • Sales tax rate in Saskatchewan over the last 100 years
  • Where do declarations go (for now)?
    1. #include <iostream>
    2. using namespace std;
    3. // constants go here
    4. const float PI = 3.14159;
    5. int main()
    6. {
    7. // variables go here
    8. int age;
    9. char gender;
    10. return 0;
    11. }
  • Special Characters
    • #
    • <>
    • ()
    • []
    • {}
    • <<
    • ><
    • \\
    • "
    • ;
  • Common Error 1: Missing Braces
  • Common Error 2: Missing Semicolons
  • Common Error 3: Missing Quotation Marks
  • Common Error 4: Misspelling Identifiers
  • Literals
    Numeric values that are directly included in the code
  • Expression
    A valid arrangement of variables, constants, and operators that can be evaluated to compute a value
  • Assignment statements with expressions
    First, the expression (right) is evaluated, then the resulting value is stored in the memory location of the variable (left)
  • Data Types
    • boolean
    • char
    • int
    • float
    • double
  • Characters & Strings in C++
    A character is any character in the ASCII data set. A string is a sequence of characters enclosed in double quotes.
  • The sizeof function can be used to find the size (in memory) of a type
  • Numerical Data Types
    • short int
    • long int
    • long long int
    • long double
  • Floating point numbers
    Stored according to the IEEE 754 standard, using something like scientific notation with a sign, exponent, and mantissa
  • Doubles are more precise than floats
  • Signed vs. Unsigned int
    Unsigned numeric datatypes range from 0 to m, signed numeric datatypes range from -n to +(n-1)
  • Many datatypes have synonyms you can use
  • Overflow is unpredictable and can cause unexpected results
  • Numeric Operators
    • =
    • +
    • -
    • *
    • /
    • %
    • pow(a, b)
  • Example 1: Computing the Area of a Circle
    1. Read in radius
    2. Compute area
    3. Display the area