Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fa 2018 Ji Seaman Programming A program is a set of instructions that the computer foows to perform a task It must be transated from a programming anguage (C++) to machine code in order to run on the machine. 1 2 Structure of a C++ Program Heo word: In genera: //This program outputs a message to the screen #incude <iostream> using namespace std; int main() { cout << Heo word! << end; } //This is a comment #incude <incudefie>... using namespace std; int main() { statements... } Variabes, Data Types Variabe: portion of memory that stores a vaue Identifier: name of a program eement Fundamenta data types short int ong ong Variabe Decaration statement datatype identifier; Variabe Initiaization statement: datatype identifier = constant; boo char hours; int count = 0; 3 4
Integer types Integers are whoe numbers such as 12, 7, and -99 Data Type Range short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 ong -2,147,483,648 to 2,147,483,647 char type stores characters such as A, @, and 9 The ascii code vaue (an integer) of the character is stored in memory. 5 Foating-point types (and boo) Foating point types store rea numbers such as 12.45 and -3.8 They are stored using scientific notation. Data Type ong boo type stores vaues that are true or fase fase is 0, true is 1. Range ±3.4E-38 to ±3.4E38 ±1.7E-308 to ±1.7E308 ±1.7E-308 to ±1.7E308 6 Constants Literas (specific vaue of a given type) Assignment statement, expressions To change the vaue of a variabe: 1 75-2 12.45-3.8 6.25e-5 true fase A 2 Named Constants: variabe whose vaue cannot be changed const datatype identifier = constant; const TAX_RATE = 0.0675; 7 The efthand side must be a variabe The righthand side is an expression of the right type What is an expression? variabe = expression; count = 10; an expression has a type and evauates to a vaue itera named constant variabe arithmetic expression etc. 8
Arithmetic Operations arithmetic operators: + addition - subtraction * mutipication / division % moduo (remainder) Integer division: x + 10 7 * 2 8-5 * 10 (3 * 10) / 2 14 3 = 4 r. 2 (because 4*3+2 = 14) 14/3 => 4 in C++ 14%3 => 2 in C++ 14.0/3.0 => 4.6666667 in C++ 9 Operator precedence In an expression with mutipe operators, which one happens first? Use this order for different operators: + - (unary) * / % + - (binary) < > <= >= ==!= && We wi study reationa and ogica operators next week. Use this order for mutipe occurrences of the same operator - (unary negation) associates right to eft *, /, %, +, - associate eft to right 10 Output (cout and <<) Basic Input/Output sends data to the screen (consoe) cout << expression; cout << expr1 << expr2; cout << heo ; cout << Count is: << count << end; Input (cin and >>) receives data typed in from the keyboard (stops at space) cin >> variabe; cin >> var1 >> var2; right hand side must be a variabe! cout << Enter the height and width: ; cin >> height >> width; cout << The height is << height << end; 11 Formatting output Goa: contro how output dispays for numeric data these require #incude<iomanip> setw(x): print next vaue in a fied at east x spaces wide (right justified, padded with spaces). cout << setw(6) << 1234 << setw(6) << 5 << end; cout << setw(6) << 5 << setw(6) << 1234 << end; cout << fixed << setprecision(x); print a ing point vaues using x digits after the decima (put it at the beginning of the program): cout << fixed << setprecision(2); cout << 3.14159 << end; x = 20; cout << x << end; 3.14 20.00 1234 5 5 1234 12
The string cass string iteras: represent sequences of chars, inside of quotes: To define string variabes: string firstname, astname; Operations incude: - = for assignment -.size() function for ength cout << Heo ; string name; name = George ; cout << name.size() << ; cout << name[2] << end; - [n] to access one character in the nth position. 13 Impicit - assignment: - binary operations: Expicit Type conversions int x; d = 3.1415; x = d; cout << x << end; int x = 10; d = 2.3; cout << x + d << end; int x, y;... avg = static_cast<>(x)/y; or avg = x/()y; //c-stye notation the type of expression on the right wi be converted to type of variabe on eft, possiby osing information. the operand with the ower ranking type is converted to the type of the other. Order of types: ong ong int char 14 Math Library functions Comments These require cmath header fie These take argument, return a Commony used functions: pow y = pow(x,d); returns x raised to the power d abs y = abs(x); returns absoute vaue of x sort y = sqrt(x); returns square root of x cei y = cei(x); returns the smaest integer >= x sin y = sin(x); returns the sine of x (in radians) etc. Singe-Line Comments // this text is ignored, to end of ine Muti-Line Comments /* Anything occurring between a sash star and a star sash is ignored. Even when spanning mutipe ines. */ Use comments to expain your code to a human reader who knows C++. 15
Programming Stye The visua organization of the source code Purpose: improve the readabiity of the source code Incudes the use of spaces, tabs, and bank ines Incudes naming of variabes, constants. Incudes where to use comments. Common eements to improve readabiity: Braces { } aigned verticay Indentation of statements within a set of braces Lines shorter than 80 characters. 17