Chapter 3 – Variables and Arithmetic Operations Variable Rules Must declare all variable names – List name and type Keep length to 31 characters – Older compiler restriction Give numeric values to variables with assignment statement variable_name = value; assignment operator Lesson 3.1 Naming Identifiers First character must be letter – a-z, A-Z or _ Other characters – letters (a-z, A-Z, _ ) or digits 0-9 Cannot use C++ keywords (reserved words) Cannot have blank within identifies Lesson 3.1 Keywords Words with special meaning to C++ Also includes alternative representations of certain operators and punctuators Full listing in Table 3.1 Examples: – auto, bool, float, inline, union, delete – namespace, private, void Lesson 3.1 Declaring Variables Variables MUST be declared List name and data type Variables of same type may be declared in same statement (separate by comma) Causes C++ compiler to know size of space to be reserved for storing variable’s value data type double radius, diameter; variable names separator Lesson 3.1 Assignment Statements Causes value to be stored in variable’s memory cell variable_name = value; Variable name MUST appear on left Equal sign is assignment operator Note: be careful = does NOT mean equal temperature = 78; Example: Lesson 3.1 Constant Qualified Variables Use const qualifier const double PI = 3.14159; Cannot modify later in program Style tip: use all uppercase characters to name constant – Makes constants easy to identify Lesson 3.2 Formatting Output Insert I/O manipulators (parameterized) into cout statements for printing – declared in header iomanip #include <iomanip> Basic form cout << manipulator(parameter); Listed in Table what manipulator uses3.2 to modify output Lesson 3.2 setw( ) Sets field width Right justifies contents C++ automatically expands if set width too small cout<<“number =“<<setw(7)<<num<<endl; number = 5 ******* Field size Lesson 3.2 setprecision( ) Sets number of digits after decimal point All digits retained in memory Once set may or may not be used until another statement changes it (compiler) num = 5.3415; cout<<“num = “<<setprecision(2)<<num; num = 5.34 Lesson 3.2 setfill( ) Specifies character for blank space in field Single quotes required around character enclosed in parentheses num = 5.34; cout<<setw(10)<<setfill(‘*’)<<num; ******5.34 Lesson 3.2 setiosflags(ios:: ) Perform number of different actions based on flag that is set Table 3.3 Example: left justifies in field num = 5.34; cout<<setiosflags(ios::left) << setfill(‘*’)<<setw(10)<<num; 5.34****** Lesson 3.2 Printing “dollar” Format Necessary to use I/O manipulators cout<<setprecision(2) <<setiosflags(ios::fixed|ios::showpoint) <<“Income = $” <<income; Income = $7842.00 Lesson 3.2 Character Data Lowercase and uppercase characters Also graphic characters (!, #, ^) and “space” Escape characters (\n) regarded as single characters Numbers 0-9 can also be characters Declare character variable: char c1,c2; Assign value: c1 = ‘g’; Can hold ONEincharacter Enclose single quotes Lesson 3.3 Assigning Characters to int C++ assigns ASCII code value for the char Does not use numeric value if assign 0-9 character Table 3.5 gives characters and ASCII code/values Lesson 3.3 Arithmetic Operations Look like algebraic expressions Expression consists of sequence of operand(s) and operator(s) – Operand (variable, constant, any value) – Operators (+, - , * , / , % ) Represent operation to be done Increment (++) and decrement (--) Lesson 3.4 Problems Uninitialized variables – C++ assigns own value – Does not trigger as an error Exceeding integer range – int type range –32768 to 32767 – Due to limit of two bytes of memory – Overflow error Division Lesson 3.4 by zero Pre- and Post- Operators ++ or - Place in front, incrementing or decrementing occurs BEFORE value assigned i = 2 and k = 1 k = ++i; i = i + 1; 3 k = i; 3 Place i = i - 1; 1 k = i; 1 in back, occurs AFTER value assigned i = 2 and k = 1 k = i; 1 k = i++; i = i + 1; 3 Lesson 3.5 k =--i; k = i--; k = i; 2 i = i - 1; 1 Mixed Type Arithmetic Assign real to int – Cut off fractional part of real Assign int value to real – Put decimal point at end, converts method of storage to exponential binary form Modify with cast operators – Change type of expression – Keyword static_cast – Old form: (type) expression Lesson 3.5 static_cast Operator General form static_cast <type> (expression) Keyword requires underscore Expression for which temporary copy is made of type type Type can be any valid C++ data type Lesson 3.5 Operator Precedence ( ) ++, -++, -+ static_cast %, *, / +, +=, -=, *= /=, %= = Lesson 3.5 parentheses post-(in/de)crement pre-(in/de)crement positive sign negative sign cast remainder/multi/div add/subtract math & assignment math & assignment assignment unary unary unary unary unary unary binary binary binary binary binary prefix postfix prefix prefix prefix prefix infix infix infix infix infix L to R L to R R to L R to L R to L R to L L to R L to R R to L R to L R to L 1 2 3 3 3 4 5 6 7 7 7 Real data types Decimal numbers float – 4 bytes of memory, 6 digit precision double – 8 bytes of memory, 15 digits precision long double – 10 bytes of memory, 19 digits precision Lesson 3.6 Integer data types Whole numbers int, signed int, short int, signed short int – 2 bytes, range: -32768 to 32767 unsigned int, unsigned short int – 2 bytes, range: 0 to 65535 long int, signed long int – 4 bytes, range: -2147483648 to 2147483645 unsigned long int – 4 bytes, range: 0 to 4294967295 Lesson 3.6 Math Functions Need cmath or cstlib headers #include <cmath> or #include <cstlib> General form: name(parameters) Note what type and form parameters take – Trig functions use radian measure not angle Table Lesson 3.6 3.11 lists math library functions Summary Learned how to: Declare variables and constants Format output Work with character data Create mathematical expressions Work with mixed data types and casting Use different data types for precision Utilize available math functions Chapter 3