Скачать презентацию CSIS 113 A Introduction To C Glenn Stevenson Скачать презентацию CSIS 113 A Introduction To C Glenn Stevenson

efc71d592beb0cc54b901d521da4426f.ppt

  • Количество слайдов: 72

CSIS 113 A Introduction To C++ Glenn Stevenson CSIS 113 A MSJC CSIS 113 A Introduction To C++ Glenn Stevenson CSIS 113 A MSJC

Overview Of A Computer The Hardware Disk CPU Memory Glenn Stevenson CSIS 113 A Overview Of A Computer The Hardware Disk CPU Memory Glenn Stevenson CSIS 113 A MSJC I/O Devices

Memory The Memory 10010110 00110010 10111101 11000110 01010000 11011110 10100101 11011111 00110101 … 00101101 Memory The Memory 10010110 00110010 10111101 11000110 01010000 11011110 10100101 11011111 00110101 … 00101101 000 001 002 003 004 005 006 007 008 Max • Memory is addressed by byte • In our Windows machines 1 byte = 8 bits • Each bit can be 0 or 1 • Memory can hold • Program instructions • Data Glenn Stevenson CSIS 113 A MSJC

Memory II The Memory Program Instructions Data • Memory is split into space for Memory II The Memory Program Instructions Data • Memory is split into space for program instructions and for data • Data space is usually further subdivided • Static/consta nt • Stack Glenn Stevenson CSIS 113 A MSJC • Heap

CPU And Memory The CPU Program Counter Instruction register Other Registers . . . CPU And Memory The CPU Program Counter Instruction register Other Registers . . . Other Registers ALU Glenn Stevenson CSIS 113 A MSJC Memor y • In our Windows machines these registers are 32 bits wide

The OS Applications APIs Operating System Drivers Hardware • Operating System • Windows • The OS Applications APIs Operating System Drivers Hardware • Operating System • Windows • Linux • Applications • Editor • Compiler • The programs we will write = Software Glenn Stevenson CSIS 113 A MSJC

Developing C++ Programs • Steps to develop a program – Write C++ instructions, store Developing C++ Programs • Steps to develop a program – Write C++ instructions, store in source-code file – The following two steps are usually automated • Use compiler to turn source into object code • Use linker to create executable – Use a debugger to examine and test program • Standalone tools vs. an IDE – Integrated Development Environment combines tools – You should know how to use individual tools Glenn Stevenson CSIS 113 A MSJC

Compile, Link, Execute Source Code Preprocessor Compiler Comments (discarded) Object Code Linker . exe Compile, Link, Execute Source Code Preprocessor Compiler Comments (discarded) Object Code Linker . exe Glenn Stevenson CSIS 113 A MSJC

Comments • Way to leave notes in your code – Ignored by compiler • Comments • Way to leave notes in your code – Ignored by compiler • You should use them liberally • 2 types – Single line // • Everything after the double slash is a comment up to the end of the line – Multi-line /* */ • /* everything between • Is a comment */ Glenn Stevenson CSIS 113 A MSJC

Required for homework • All assignments must have comments at the top of each Required for homework • All assignments must have comments at the top of each page! /* Name: Glenn Stevenson SID#: 1234567 Date: todays date Lab #: 1 Description: you can simply copy and paste from the assignment descript itself */ • Failure to add comments to your assignments can result in a 0 grade Glenn Stevenson CSIS 113 A MSJC

Functions & main • C++ is typically a cooperating group of functions – Functions Functions & main • C++ is typically a cooperating group of functions – Functions • Like a small program within a large program – Used to perform a specific task • main – function where your program begins – Your program must have a main function • Programs need a place to start • Program will not link without a main function Glenn Stevenson CSIS 113 A MSJC

Anatomy of main • C++ is a case Sensitive language • Function Header – Anatomy of main • C++ is a case Sensitive language • Function Header – First line of function • Function Body – Code between opening and closing braces • All blocks have an opening and a closing brace • Return Statement – What this function returns • Where does in return to? Glenn Stevenson CSIS 113 A MSJC

Statements / Blocks • Statements – Any valid line of C++ code • Statements Statements / Blocks • Statements – Any valid line of C++ code • Statements are terminated by a semicolon – Can span multiple lines compiler looks for ; as a marker » Compiler generates an error when a statement is not terminated by a semicolon • Blocks – Any code that has an opening & closing brace • Not terminated by a semicolon – Function main has opening and closing brace » Function body then is a block of code Glenn Stevenson CSIS 113 A MSJC

Our First Program! // myfirst. cpp - A simple C++ program #include <iostream> using Our First Program! // myfirst. cpp - A simple C++ program #include using namespace std; int main() { cout << "Hi, my name cout << "n"; return 0; } // Preprocessor statement // Make cout visible // Function header is Glenn Stevenson"; Glenn Stevenson CSIS 113 A MSJC

Ananlyizing my. First. cpp • #include <iostream> – Has definitions input and output streams Ananlyizing my. First. cpp • #include – Has definitions input and output streams • C++ has much functionality included • using namespace std; – Use built functionality from standard library • int main() – Function header of main followed by the body of main (code between the baces) • cout << “Hi my name is Glenn Stevenson”; – cout puts information to screen • cout << “n”; – Moves the cursor to the next line on the console window • Called an escape character, more about that later Glenn Stevenson CSIS 113 A MSJC

What is wrong with this code? // myfirst. cpp - A simple C++ program What is wrong with this code? // myfirst. cpp - A simple C++ program #include // Preprocessor statement using namespace std; // Make cout visible int Main(); // Function header { cout << "Hi, my name is Glenn Stevenson"; cout << "n"; return 0; }; Glenn Stevenson CSIS 113 A MSJC

The preprocessor • Separate program called CPP [C Pre-Processor] – Massages source code before The preprocessor • Separate program called CPP [C Pre-Processor] – Massages source code before giving it to compiler – Preprocessor instructions are called directives – Preprocessor directives all begin with # • The #include directive says – "Send the declarations and definitions in the iostream header to the compiler at this point" – What is a "header"? • "Family" of declarations and definitions • Normally stored in a file (or translation unit as it is called in C++), but doesn't have to be Glenn Stevenson CSIS 113 A MSJC

The Preprocessor II #include <iostream> Source Code // Preprocessor statement Preprocessor Compiler Header Glenn The Preprocessor II #include Source Code // Preprocessor statement Preprocessor Compiler Header Glenn Stevenson CSIS 113 A MSJC Object Code

The Preprocessor III • Traditional [C-style] headers all end in. h • Standard C++ The Preprocessor III • Traditional [C-style] headers all end in. h • Standard C++ headers: – Don't have ". h" suffix – Don't necessarily refer to file names – Older [pre-IS 0] compilers won't support • Under Turbo C++, for instance, you write: – #include – And don’t get namespace functionality • Do not use the C-style headers – C++ Style Guide 7. 1. 1 Glenn Stevenson CSIS 113 A MSJC

The Standard Headers • 51 headers available in Standard C++ – 18 ANSI C The Standard Headers • 51 headers available in Standard C++ – 18 ANSI C Standard library headers • Available in traditional and new styles • and – 13 Standard Template Library [STL] headers • Collections and algorithms [generic programming] – 9 Input/Output [I/0] stream headers – String, exception, memory management, and various data structure classes not in the STL Glenn Stevenson CSIS 113 A MSJC

Namespaces using namespace std; // Make cout visible • A namespace defines a region Namespaces using namespace std; // Make cout visible • A namespace defines a region where names are valid – In C, all function names are "global" – Headers combine functions into "families" – Namespaces combine families into "neighborhoods" • All Standard C++ library functions and objects are in the std namespace – C library functions also in std with new headers • Some older compilers have not fully implemented this Glenn Stevenson CSIS 113 A MSJC

Namespaces II • To use a function in a namespace – Prefix it with Namespaces II • To use a function in a namespace – Prefix it with namespace and : : [scope resolution] • Example: using the standard sqrt() function – #include // In std namespace double ans = std: : sqrt(5. 25); • In this class, you'll often import entire std namespace – "Import" with using namespace declaration Glenn Stevenson CSIS 113 A MSJC

Literals • Called literals because they are values that are literally stated – – Literals • Called literals because they are values that are literally stated – – – 1 // integer literal – whole number 2. 2 // floating point literal. 3 // floating point literal 'h‘ // single character literal "Hello“ // string literal • Don’t confuse single characters for strings! – This is not legal ‘hello’ and – ‘h’ is different from “h” Glenn Stevenson CSIS 113 A MSJC

C++ Output cout << C++ Output cout << "Hi, my name is Glenn Stevenson"; • I/O is supplied as part of the standard library – Definitions found in the header – Standard I/O is represented as a stream – cout uses the stream extraction operator • << notice that it points at cout! – Can use many insertion operators to separate types • Cout << “hello my lucky number is “ << 7 << endl; – endl does the same thing as “n” » Moves cursor to next line on console window Glenn Stevenson CSIS 113 A MSJC

Changing Numbers With cout • You can output numbers in many formats – Send Changing Numbers With cout • You can output numbers in many formats – Send format type to cout – Hexadecimal – Base 16 • cout << hex << 16 << endl; – Outputs 10 because 10 is 16 in hex – Octal – Base 8 • cout << oct << 9 << endl; – Outputs 10 because 9 is 10 in octal – Decimal – Base 10 • cout << dec << 10 << endl; – Outputs 10 – Will output desired format until changed or program ends Glenn Stevenson CSIS 113 A MSJC

What is a stream? • Streams are abstract information flows – Data flows into What is a stream? • Streams are abstract information flows – Data flows into your program from a source – Your program processes the data – Information flows out of your program to a sink • Sending info to a sink is called writing • Getting info from a source is called reading Glenn Stevenson CSIS 113 A MSJC

Standard streams • C++ initially creates at least 3 stream objects – – Also Standard streams • C++ initially creates at least 3 stream objects – – Also known as standard input, output, and error cin : input connected to the keyboard cout : output connected to the monitor cerr : output connected to the monitor Glenn Stevenson CSIS 113 A MSJC

Formatting your code • C++ is a free-form language – Interchangably use spaces, tabs Formatting your code • C++ is a free-form language – Interchangably use spaces, tabs and newlines A. int main() { cout << "Hi"; return 0; } B. int main ( ) { cout << "Hi" ; return 0 ; } Glenn Stevenson CSIS 113 A MSJC

Formatting II • Cannot use whitespace between parts of a token – A token Formatting II • Cannot use whitespace between parts of a token – A token is one or more characters with a single meaning • /*, */, <<, // are all tokens • Keywords like int and return are tokens – Here are some uses of illegal whitespace with tokens in t main( ) { cout < < "Hi" ; ret urn 0; } – You cannot put a literal newline in a string cout << “This is illegal”; cout << “But this ” “is legal!”; Glenn Stevenson CSIS 113 A MSJC

Code Formatting III • Must use whitespace to separate some tokens – cout<< Code Formatting III • Must use whitespace to separate some tokens – cout<<"Hello there"; // Not required – intmain(){return 0; } // Spaces needed int main(){return 0; } // OK int main(){return(0); } // OK Glenn Stevenson CSIS 113 A MSJC

Values & Variables • Values are discrete quantities of data – Here are some Values & Variables • Values are discrete quantities of data – Here are some values: • 1, 3. 1459, “Glenn”, D, true – Values represent different kinds of things • In programming, this is called a value's type • integers, real, text [strings], characters, Boolean • Variables are: – Named storage locations that hold values – Every variable holds a value of a certain type • The type defines the legal operations and the required memory to store it (size) Glenn Stevenson CSIS 113 A MSJC

Declaring Variables • C++ has two statements devoted to variables – Declaration statements and Declaring Variables • C++ has two statements devoted to variables – Declaration statements and assignment statements • A declaration statement specifies: – The name of memory location – The kind of thing (type) stored at the location int fleas; – This creates an integer variable named fleas • The compiler sets aside memory to store an int – The amount of memory for an int is implementation dependent • Makes sure that you use variable appropriately • Cannot store a string in fleas, for instance Glenn Stevenson CSIS 113 A MSJC

Variable Definition I • I like to think of the compiler taking a box Variable Definition I • I like to think of the compiler taking a box of the correct size for the type of variable (a variable can be from 1 to n bytes in size), and writing the name, type of variable and its address on the outside. – The box will hold the value of the variable when it’s used. • The boxes are then stacked away in storage Name: i Type: int Address: 5000 Name: ch Type: char Address: 5004 Name: r Type: double Address: 5005 4 byte box 1 byte box 8 byte box Glenn Stevenson CSIS 113 A MSJC

Variable Definition II int 5000 char 5004 double 5005 Glenn Stevenson CSIS 113 A Variable Definition II int 5000 char 5004 double 5005 Glenn Stevenson CSIS 113 A MSJC … … … x 5005 … 5006 … 5007 … … x ch r 5000 5001 5002 5003 5004 r ch

LValues • An LValue is nothing more than something that is to the left LValues • An LValue is nothing more than something that is to the left of the equal sign. – Since values get assigned from right to left, the lvalue must alway be a variable. • This means that you cannot do something like this: int x = 30; 25 = x; //Can’t Do this Glenn Stevenson CSIS 113 A MSJC

Variable Naming Rules • Variable names can consist of letters, numbers, and the underscore Variable Naming Rules • Variable names can consist of letters, numbers, and the underscore but cannot begin with a number. • They can also not be a C++ keyword like if, else, for, etc. • Variable names cannot contain a space character. int my Var; // This is not valid int my. Var; // This is valid Glenn Stevenson CSIS 113 A MSJC

Naming Conventions • Naming conventions have changed over the years it used to be Naming Conventions • Naming conventions have changed over the years it used to be that if you had a variable name that had two words in it you would separate the words with an underscore. – int my_int_variable; • Later people starting capitalizing every word. – int My. Int. Variable; • Currently, the convention is to capitalize from the second word on: – int my. Int. Variable; Glenn Stevenson CSIS 113 A MSJC

Number Types • C++ has several number types in two categories – Integral • Number Types • C++ has several number types in two categories – Integral • Counting numbers – Signed – Unsigned – Floating-point • Real numbers (numbers with a fractional part) Glenn Stevenson CSIS 113 A MSJC

Numeric Type Sizes Name Bytes Range Digits short (also called 2 short int) -32, Numeric Type Sizes Name Bytes Range Digits short (also called 2 short int) -32, 767 to +32, 767 N/A int 4 -2, 147, 483, 647 to +2, 147, 483, 647 N/A long (also called long int) 4 -2, 147, 483, 647 to +2, 147, 483, 647 N/A float 4 10 -38 to 10+38 7 double 8 10 -308 to 10+308 Approximately 15 long double 10 10 -4932 to 10+4932 Approximately 19 char 1 All ASCII Characters N/A bool 1 true, false N/A Glenn Stevenson CSIS 113 A MSJC

Losing Precision • Copying values from one type of a variable to another type Losing Precision • Copying values from one type of a variable to another type can force a loss of data. double d = 33. 456; int x; x = d; • x holds an integer value and the code is attempting to store double in it. – These are different size buckets! • the double will get truncated to an integer – The value stored in x is 33. Glenn Stevenson CSIS 113 A MSJC

Losing Precision II • You can put smaller values into bigger buckets because they Losing Precision II • You can put smaller values into bigger buckets because they will fit: . double d; int x = 30; d = x; //This is fine, no loss of data • You may actually want to store a large value in a small bucket. – You can do this without the compiler complaining by casting the value. – Future topic Glenn Stevenson CSIS 113 A MSJC

Binary Numbers • Unsigned (positive) binary integers – Converting a binary number to decimal Binary Numbers • Unsigned (positive) binary integers – Converting a binary number to decimal • Each binary digit represents a power of 2 • Just like decimal digits represent power of 10 – So what is 10110101 binary in decimal? • • 27 128 25 32 24 16 23 8 22 4 2120 21 1 128 26 64 0 1 1 0 10 0 32 16 0 4 01 • = 181 Glenn Stevenson CSIS 113 A MSJC 1

Binary Numbers II • So what’s 0 x 608 bfa 05 in decimal? • Binary Numbers II • So what’s 0 x 608 bfa 05 in decimal? • 5 x 160 = 5 x 1 = 5 0 x 161 = 0 x 16 = 0 2 = 10 x 256 A x 16 = 2, 560 F x 163 = 15 x 4096 = 61, 440 B x 164 = 11 x 65536 = 720, 896 8 x 165 = 8 x 1048576 = 8, 388, 608 0 x 166 = 0 x 16777216 = 0 6 x 167 = 6 x 268435456 =1, 610, 612, 736 1, 619, 786, 245 • So 0110000010111111101000000101 = 014042775005 = 0 x 608 bfa 05 = 1, 619, 786, 245 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers III • Converting a decimal number to binary – Find largest power Binary Numbers III • Converting a decimal number to binary – Find largest power of 2 and subtract; repeat – Example: Decimal 89 as binary • • Largest power of 2 is 64 : 0100 -0000 Subtract 64 from 89 = 25 Find next power of 2 [16] : 0101 -0000 Subtract 16 from 25 = 9 Find next power of 2 [8] : 0101 -1000 Subtract 8 from 9 = 1 Find next power of 2 [1] : 0101 -1001 = 0 x 59 Glenn Stevenson CSIS 113 A MSJC

Binary Number IV • A way with a lot less work – convert to Binary Number IV • A way with a lot less work – convert to hex first – Find the largest power of 16 and subtract the multiples – Example: Decimal 89 as binary • • • Largest power of 16 is 16: 0 x 50 Subtract 5 * 16 from 89 = 9 Next smallest power of 16 is 1: 0 x 59 Subtract 9 * 1 from 9 = 0 Convert hex to binary by inspection – = 0101 1001 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers V • An easier way to convert – In Windows there is Binary Numbers V • An easier way to convert – In Windows there is a calculator application (usually in the accessories group) • Make certain that the scientific option is selected under the view menu • Use the Hex, Oct, Dec and Bin radio buttons to convert numbers • You’re probably now wondering why do I need to know this stuff? ? ? – C++, like it predecessor C, can do low-level programming • You can “bit-twiddle” which is frequently necessary when dealing with hardware and systems programming – When examining memory the addresses and values are normally displayed in hex Glenn Stevenson CSIS 113 A MSJC

Binary Numbers VI • How can signed binary integers be stored? – One scheme Binary Numbers VI • How can signed binary integers be stored? – One scheme is called sign-magnitude method – Give up a bit to represent sign (0 = positive) – Remaining bits represent magnitude – 1011 - 0101 = - (32 + 16 + 4 + 1) = -53 – Not generally used because of the two-zeros problem – 0000 -0000 = +0 1000 -0000 = -0 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers VII • One’s complement – MSB [most-significant-bit] is used for sign bit Binary Numbers VII • One’s complement – MSB [most-significant-bit] is used for sign bit – If it is set, then one's complement is used to calculate the magnitude of negative number – One’s complement: • Reverse every bit (0 1 and 1 0) – Also called complement • Interpret result as an unsigned number • Place minus sign in front of answer – Still results in two zeros • • 0000 – 0000 = +0 1111 – 1111 = -0 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers VII • Two's complement results in one zero – MSB [most-significant-bit] is Binary Numbers VII • Two's complement results in one zero – MSB [most-significant-bit] is used for sign bit – If it is set, then two's complement is used to calculate the magnitude of negative number – Two's complement: • • Reverse every bit [simple, or one's complement] Add one to the result Interpret result as an unsigned number Place minus sign in front of answer Glenn Stevenson CSIS 113 A MSJC

Binary Numbers VIII • Wait a minute, how do I add 1 in binary? Binary Numbers VIII • Wait a minute, how do I add 1 in binary? – 0 01 +0 +1+1 0 110 – 1 + 1 = 0 carry 1 – So – 1011 +1 1100 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers IX • Wait a minute, how do I add 1 in binary? Binary Numbers IX • Wait a minute, how do I add 1 in binary? – 0 01 +0 +1+1 0 110 • 1 + 1 = 0 carry 1 – So – 1011 +1 1100 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers X • Signed binary to decimal examples – 0000 -0011 = + Binary Numbers X • Signed binary to decimal examples – 0000 -0011 = + 3 – MSB is clear - treat as positive unsigned – 1000 -0011 = ? ? ? – MSB set - negative-perform two's complement • • 1. Reverse all of the bits : 0111 -1100 2. Add one to result : 0111 -1101 3. Interpret result : 64+32+16+8+4+1 = 125 4. Add minus sign : -125 Glenn Stevenson CSIS 113 A MSJC

Binary Numbers XI • How to convert negative decimal numbers – 1. Convert absolute Binary Numbers XI • How to convert negative decimal numbers – 1. Convert absolute value to binary – 2. Perform two's complement on result • Example: convert -59 to binary – 1. Binary magnitude (59) – 0011 -1011 – 2. Perform two's complement – 1100 -0100 +1 1100 -0101 Glenn Stevenson CSIS 113 A MSJC

Integral Number Types • C++ Built-in Integral types – – char, unsigned char, and Integral Number Types • C++ Built-in Integral types – – char, unsigned char, and signed char short and unsigned short int and unsigned int (or unsigned) long, unsigned long • In this course we will use int primarily – On our 32 -bit machines it takes 32 -bits or 4 bytes to store an int – Range • -2, 147, 483, 648 to 2, 147, 483, 647 – climits header defines limits Glenn Stevenson CSIS 113 A MSJC

The int data type I • The int data type I • "Basic" integer is a signed whole number – Size depends upon underlying platform – On 16 -bit systems - 16 -bit int – Today, almost all systems are 32 bit, tomorrow 64? • Integer modifiers include: – – long [at least 32 bits, never smaller than int] short [at least 16 bits, never larger than int] unsigned [only positive numbers] Use of modifiers implicitly assumes type int e. g. long x; is equivalent to long int x; – char one byte – may be signed do or unsigned Glenn Stevenson CSIS 113 A MSJC

int data type II • How is an int stored in my computer – int data type II • How is an int stored in my computer – It depends on your processor • Intel processors use “Little Endian” – Two byte words stored least significant word first with each word stored least significant byte first – So 0 x 608 bfa 05 would be stored in memory as 05 fa 8 b 60 • Many other processors use “Big Endian” – Words and bytes are stored most significant word and byte first – So 0 x 608 bfa 05 would be stored in memory as 60 8 b fa 05 – If you use a memory dump to debug, this is what you will see Glenn Stevenson CSIS 113 A MSJC

The sizeof operator • C++ mandates size The sizeof operator • C++ mandates size "relationships" • How can you find actual size of object? – Use the sizeof operator (note operator - not a function) – Returns size in "bytes" [usually 8 or 16 bits per byte – implementation dependent] • cout << "sizeof 123 = " << sizeof 123; cout << "sizeof a = " << sizeof a; – Parentheses required when using a type-name • cout << "sizeof (int) = " << sizeof (int); Glenn Stevenson CSIS 113 A MSJC

Floating Point Number Types • C++ Built-in Floating-point types – float – double – Floating Point Number Types • C++ Built-in Floating-point types – float – double – long double • In this course we will primarily use double – On our 32 -bit machines it takes 128 bits or 8 bytes to store a double – Range • 2. 2250738585072014 e-308 to 1. 7976931348623158 e+308 – Precision • 15 digits – cfloat header defines limits Glenn Stevenson CSIS 113 A MSJC

Scientific Notation • Also sometimes called Exponential Notation • Usually used to express very Scientific Notation • Also sometimes called Exponential Notation • Usually used to express very large or very small numbers – – – speed of light in a vacuum = 2. 99 x 108 meters/sec Distance to Alpha Centauri = 5. 863 x 1012 miles Size of a hydrogen atom ≈ 10 -10 meters Mass of an electron = 9. 11 x 10 -31 kg Number of atoms in the entire universe ≈ 1080 • Expressed by a sign followed by a single digit followed by a decimal fraction followed by a power of ten – The single digit plus the fraction is called the mantissa – The power of ten is called the exponent Glenn Stevenson CSIS 113 A MSJC

Scientific Notation II • So 7. 3125 (7 5/16) is – 111. 0101 – Scientific Notation II • So 7. 3125 (7 5/16) is – 111. 0101 – We can write this as – 1. 110101 x 22 – In this case • the sign is positive • the mantissa is 1. 110101 • the exponent is 2 Glenn Stevenson CSIS 113 A MSJC

Scientific Notation III • 0. 1875 (3/16) is – 0. 0011 or 1. 1 Scientific Notation III • 0. 1875 (3/16) is – 0. 0011 or 1. 1 x 2 -3 – In this case • the sign is positive • the mantissa is 1. 1 • the exponent is -3 Glenn Stevenson CSIS 113 A MSJC

Scientific Notation IV • And -55. 25 is – -110111. 01 or -1. 1011101 Scientific Notation IV • And -55. 25 is – -110111. 01 or -1. 1011101 x 25 – In this case • the sign is negative • the mantissa is 1. 1011101 • the exponent is 5 Glenn Stevenson CSIS 113 A MSJC

Scientific Notation V • What about 1. 1? – 1. 000110011001100… – 0. 1 Scientific Notation V • What about 1. 1? – 1. 000110011001100… – 0. 1 cannot be exactly represented as a binary fraction • This is similar to 1/3 not being able to be exactly represented as a decimal fraction: 0. 333333… • This is referred to as representational error Glenn Stevenson CSIS 113 A MSJC

Scientific Notation VI • So how do we store these numbers in our computer? Scientific Notation VI • So how do we store these numbers in our computer? – There are several conventions • Most common is IEEE (Institute of Electrical and Electronics Engineers) standard – Real*4 – Real*8 – Real*10 sign bit + 8 -bit exponent + 23 bit mantissa sign bit + 11 -bit exponent + 52 bit mantissa sign bit + 15 -bit exponent + 64 bit mantissa Real*4 Precision (min) Real*8 Real*10 6 digits 15 digits 18 digits Max Exponent 38 308 4932 Min Exponent -37 -307 -4931 Glenn Stevenson CSIS 113 A MSJC

Arithmetic Operators Operator + * / % Meaning • • Add Subtract • • Arithmetic Operators Operator + * / % Meaning • • Add Subtract • • Multiply • Divide Modulous • int x = 3, y = 0; y = x + 6; y = x – 2; y=x*2 y = 6 / x; Y = 6 % 3; 10 % 3 results in 1 Because 10 / 3 = 3 with a remainder of 1 12 % 7 results in 5 Because 12 / 7 = 1 with a remainder of 5 20% 5 results in 0 Because 20 / 5 = 4 with a remainder of 0 Glenn Stevenson CSIS 113 A MSJC

Shortcut Notation int x = 4; Equivalent Value x+=5; x=x+5 9 x-=1; x = Shortcut Notation int x = 4; Equivalent Value x+=5; x=x+5 9 x-=1; x = x -1; 3 x*=5; x = x * 5; 20 x/=2 x=x/2 4 x%=3 x=x%3 1 Glenn Stevenson CSIS 113 A MSJC

Escape Characters • A character pair that you can put into a string to Escape Characters • A character pair that you can put into a string to perform an action. n Newline cout<<“hellon”; r Move the cursor back to the beginning of the line cout<<“hello” << endl; t Insert a tab \ Insert a single backslash ’ Single quote /” Double quote Below are not commonly used v Vertical tab b backspace f Form feed ? Questions mark Glenn Stevenson CSIS 113 A MSJC

Constants • A read only variable – It cannot be changed programmatically • Example: Constants • A read only variable – It cannot be changed programmatically • Example: PI • Use const keyword – const double PI = 3. 14159265 ; • By convention – const variables should be ALL CAPS Glenn Stevenson CSIS 113 A MSJC

C Typecasting • Used to intentionally convert numbers – From one type to another C Typecasting • Used to intentionally convert numbers – From one type to another • Can store small value in a larger bucket • Cannot store larger value in smaller bucket – Possible loss of data int x = 0; double z = 33. 345; x = z; // x now holds 33. Value is truncated & compiler // warning possible compiler error x = (int) z; //Temporarily converts z to an int. Still truncates but // no error or warning x = static_cast z ; // c++ cast covered later Glenn Stevenson CSIS 113 A MSJC

Shorthand operators • Called unary operators – They operate on a single variable! – Shorthand operators • Called unary operators – They operate on a single variable! – ++ // increments a variable by one – -- // decrements a variable by one int x = 3; int y = 2; x++; // x is now 4 y--; // y is now 1 Glenn Stevenson CSIS 113 A MSJC

Post & Pre Increment/ Decrement • Can apply shorthand operators 2 ways – Before Post & Pre Increment/ Decrement • Can apply shorthand operators 2 ways – Before variable ++x – After Variable x++ – Whats the difference? • Consider: int x = 2; int y = 0; y = 1 * x++; // What does y equal here? y = 1 * ++x; // Now what does y equal Glenn Stevenson CSIS 113 A MSJC

Coding Style • You should adopt a coding style that makes your code readable. Coding Style • You should adopt a coding style that makes your code readable. – Typically, you should indent whenever a block of code starts. and move it back when the block ends. • How far you indent depends on your preference. – I prefer 3 spaces. Some people like to do a tab, and still others like 4 spaces. Whatever you decide, be consistent. Int main() { cout << “Hello World” << endl; // Notice indent } Glenn Stevenson CSIS 113 A MSJC