1 / 40

Exposure C++ Chapter VI Data Type Operations

Exposure C++ Chapter VI Data Type Operations. C++ Integer Operations Symbols. + Addition - Subtraction * Multiplication / Integer division % Modulus or Remainder Division. // PROG0601.CPP // This program demonstrates integer operations. #include <iostream.h> void main() {

Download Presentation

Exposure C++ Chapter VI Data Type Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exposure C++ Chapter VI Data Type Operations

  2. C++ Integer Operations Symbols + Addition - Subtraction * Multiplication / Integer division % Modulus or Remainder Division

  3. // PROG0601.CPP // This program demonstrates integer operations. #include <iostream.h> void main() { int Nr1 = 100; int Nr2 = 30; int Result1, Result2, Result3, Result4, Result5; Result1 = Nr1 + Nr2; Result2 = Nr1 - Nr2; Result3 = Nr1 * Nr2; Result4 = Nr1 / Nr2; Result5 = Nr1 % Nr2; cout << Nr1 << " + " << Nr2 << " = " << Result1 << endl; cout << Nr1 << " - " << Nr2 << " = " << Result2 << endl; cout << Nr1 << " * " << Nr2 << " = " << Result3 << endl; cout << Nr1 << " / " << Nr2 << " = " << Result4 << endl; cout << Nr1 << " % " << Nr2 << " = " << Result5 << endl; } PROG0601.CPP OUTPUT 100 + 30 = 130 100 - 30 = 70 100 * 30 = 3000 100 / 30 = 3 100 % 30 = 10

  4. Integer Division Examples 12 / 3 = 4 12 / 4 = 3 12 / 5 = 2 12 / 8 = 1 12 / 12 = 1 12 / 15 = 0 0 / 12 = 0 12 / 0 = undefined

  5. Modulus Division Examples 12 % 3 = 0 12 % 4 = 0 12 % 5 = 2 12 % 8 = 4 12 % 12 = 0 12 % 15 = 12 0 % 12 = 0 12 % 0 = undefined

  6. Flashback To Elementary School Long Division

  7. // PROG0602.CPP // This program demonstrates incrementers and decrementers. #include <iostream.h> void main() { int Nr1 = 100; int Nr2 = 30; cout << "Nr1 = " << Nr1 << endl; cout << "Nr2 = " << Nr2 << endl; Nr1++; // postfix incrementer ++Nr2; // prefix incrementer cout << "Nr1 = " << Nr1 << endl; cout << "Nr2 = " << Nr2 << endl; Nr1--; // postfix decrementer --Nr2; // prefix decrementer cout << "Nr1 = " << Nr1 << endl; cout << "Nr2 = " << Nr2 << endl; } PROG0602.CPP OUTPUT Nr1 = 100 Nr2 = 30 Nr1 = 101 Nr2 = 31 Nr1 = 100 Nr2 = 30

  8. Incrementer and Decrementer Shortcuts K++ is a shortcut for K = K + 1; ++K is a shortcut for K = K + 1; K-- is a shortcut for K = K - 1; --K is a shortcut for K = K - 1;

  9. // PROG0603.CPP // This program demonstrates the difference between // the X++ and ++X incrementers. #include <iostream.h> void main() { int X1,X2; X1 = X2 = 10; cout << X1 << " " << X2 << endl; cout << ++X1 << " " << X2++ << endl; cout << X1 << " " << X2 << endl; } PROG0603.CPP OUTPUT 10 10 11 10 11 11

  10. Basic Operation Shortcuts No Shortcut Notation Shortcut Notation K = K + 5 K += 5 K = K - 5 K -= 5 K = K * 5 K *= 5 K = K / 5 K /= 5 K = K % 51 K %= 5

  11. // PROG0604.CPP // This program demonstrates using shortcut notation with // each one of the 5 basic operations. #include <iostream.h> // Necessary for program input/output void main() { int Nr = 100; cout << "Nr " << Nr << endl << endl; Nr += 10; cout << "Nr += 10 " << Nr << endl << endl; Nr -= 20; cout << "Nr -= 20 " << Nr << endl << endl; Nr *= 2; cout << "Nr *= 2 " << Nr << endl << endl; Nr /= 5; cout << "Nr /= 5 " << Nr << endl << endl; Nr %= 7; cout << "Nr %= 7 " << Nr << endl << endl; } PROG0604.CPP OUTPUT Nr 100 Nr += 10 110 Nr -= 20 90 Nr *= 2 180 Nr /= 5 36

  12. The Danger of Combining Shortcuts // PROG0605.CPP // This program demonstrates complexity caused // by combining incrementers. #include <iostream.h> void main() { int X = 10; ++X += X++; cout << "X = " << X << endl << endl; } PROG0605.CPP OUTPUT X = 23

  13. // PROG0606.CPP // This program shows complexity with combining // incrementers. #include <iostream.h> void main() { int X = 10; ++X += (X++ + ++X); cout << "X = " << X << endl << endl; } The output is intentionally not shown. This is because it can be different on different computers. Also, if you move int x=10; before the void main(), it can actually change the answer. The point is do not combine shortcuts.

  14. C++ Shortcut Warning C++ shortcut operations should not be combined with any other type of operation or any other type of statement. The results of many combinations is non-standard. It can fluctuate with different compilers and results are unpredictable. Proper Usage: K++; cout << K << endl; Problematic Usage: cout << K++ << endl;

  15. C++ Real Number Operations + Addition - Subtraction * Multiplication / Real number division (no remainder)

  16. // PROG0607.CPP // This program demonstrates real number operations implemented // with the float type. #include <iostream.h> void main() { float Nr1 = 1000; float Nr2 = 3.3; float Result1, Result2, Result3, Result4; Result1 = Nr1 + Nr2; Result2 = Nr1 - Nr2; Result3 = Nr1 * Nr2; Result4 = Nr1 / Nr2; cout << Nr1 << " + " << Nr2 << " = " << Result1 << endl; cout << Nr1 << " - " << Nr2 << " = " << Result2 << endl; cout << Nr1 << " * " << Nr2 << " = " << Result3 << endl; cout << Nr1 << " / " << Nr2 << " = " << Result4 << endl; } PROG0607.CPP OUTPUT 1000 + 3.3 = 1003.299988 1000 - 3.3 = 996.700012 1000 * 3.3 = 3300 1000 / 3.3 = 303.030304

  17. // PROG0608.CPP // This program demonstrates real number operations // implemented with the double type. #include <iostream.h> void main() { double Nr1 = 1000; double Nr2 = 3.3; double Result1, Result2, Result3, Result4; Result1 = Nr1 + Nr2; Result2 = Nr1 - Nr2; Result3 = Nr1 * Nr2; Result4 = Nr1 / Nr2; cout << Nr1 << " + " << Nr2 << " = " << Result1 << endl; cout << Nr1 << " - " << Nr2 << " = " << Result2 << endl; cout << Nr1 << " * " << Nr2 << " = " << Result3 << endl; cout << Nr1 << " / " << Nr2 << " = " << Result4 << endl; } PROG0608.CPP OUTPUT 1000 + 3.3 = 1003.3 1000 - 3.3 = 996.7 1000 * 3.3 = 3300 1000 / 3.3 = 303.030303

  18. Mathematical Precedence Parentheses Exponents Multiplication & Division Addition & Subtraction

  19. Hidden Operators in Mathematics Mathematics C++ Source Code 5XY 5*X*Y 4X + 3Y 4*X + 3*Y 6(A + B) 6 * (A + B) A + B (A + B) / (A - B) A - B

  20. // PROG0609.CPP // This program demonstrates math precedence in C++. #include <iostream.h> void main() { double A,B,C, Result; A = 1000; B = 100; C = 2.5; cout << "A = " << A << " B = " << B << " C = " << C << endl << endl; Result = A + B * C; cout << "A + B * C = " << Result << endl; Result = (A + B) * C; cout << "(A + B) * C = " << Result << endl; Result = A / B * C; cout << "A / B * C = " << Result << endl; Result = A * B / C; cout << "A * B / C = " << Result << endl; } PROG0609.CPP OUTPUT A = 1000 B = 100 C = 2.5 A + B * C = 1250 (A + B) * C = 2750 A / B * C = 25 A * B / C = 40000

  21. // PROG0610.cpp // This program demonstrates that characters are stored as numbers. #include <iostream.h> void main() { cout << "'a' - 'A' = " << 'a' - 'A' << endl; cout << "'b' - 'B' = " << 'b' - 'B' << endl; cout << "'z' - 'Z' = " << 'z' - 'Z' << endl; cout << "'a' + 'A' = " << 'a' + 'A' << endl; cout << "'b' + 'B' = " << 'b' + 'B' << endl; cout << "'z' + 'Z' = " << 'z' + 'Z' << endl; } PROG0610.CPP OUTPUT 'a' - 'A' = 32 'b' - 'B' = 32 'z' - 'Z' = 32 'a' + 'A' = 162 'b' + 'B' = 164 'z' + 'Z' = 212

  22. // PROG0611.cpp // This program demonstrates operations with character variables. #include <iostream.h> void main() { char Char1, Char2; Char1 = 'A'; Char2 = Char1 + 5; cout << Char1 << " + 5 = " << Char2 << endl; Char2 = Char1 + 10; cout << Char1 << " + 10 = " << Char2 << endl; Char1++; cout << "Char1++ = " << Char1 << endl; } PROG0611.CPP OUTPUT A + 5 = F A + 10 = K Char1++ = B

  23. String Concatenation Concatenation is the appending of a second string to a first string. "Hello" + "World" = "HelloWorld" "Hello" + " " + "World" = "Hello World" "100" + "200" = "100200" The plus operator ( + ) is used both for arithmetic addition and string concatenation. The same operator performs two totally different operations. In computer science when an operator is used for more than one operation, the operator is said to be overloaded.

  24. // PROG0612.cpp // This program demonstrates concatenation with strings. #include <iostream.h> #include "APSTRING.H" void main() { apstring String1 = "Good"; apstring String2 = "Morning"; apstring String3; String3 = String1 + ' ' + String2; cout << String1 << " + " << String2 << " = " << String3 << endl; } PROG0612.CPP OUTPUT Good + Morning = Good Morning

  25. Type Casting Type casting is the intentional assignment of one “indicated” data type to another data type. Indication is important. double Result; Result = 5/3; // integer division double Result; Result = (double) 5/3; // real division // due to type casting

  26. // PROG0613.CPP // This program demonstrates type casting in C++. #include <iostream.h> void main() { char Var1; int Var2; float Var3; Var1 = 'A'; Var2 = 65; Var3 = 3.14159; cout << Var1 << endl; cout << Var2 << endl; cout << Var3 << endl; cout << endl; cout << "A becomes " << (int) Var1 << endl; cout << "65 becomes " << (char) Var2 << endl; cout << "65/3 without casting becomes " << Var2/3 << endl; cout << "65/3 with casting becomes " << (float) Var2/3 << endl; cout << "3.14159 becomes " << (int) Var3 << endl; } PROG0613.CPP OUTPUT A 65 3.14159 A becomes 65 65 becomes A 65/3 without casting becomes 21 65/3 with casting becomes 21.666666 3.14159 becomes 3

  27. // PROG0614.CPP // This program demonstrates a second syntax style for type casting. #include <iostream.h> void main() { char Var1; int Var2; double Var3; Var1 = 'A'; Var2 = 65; Var3 = 3.14159; cout << Var1 << endl; cout << Var2 << endl; cout << Var3 << endl; cout << endl; cout << "A becomes " << int (Var1) << endl; cout << "65 becomes " << char (Var2) << endl; cout << "65/3 without casting becomes " << Var2/3 << endl; cout << "65/3 with casting becomes " << float (Var2/3) << endl; cout << "3.14159 becomes " << int (Var3) << endl; } PROG0614.CPP OUTPUT A 65 3.14159 A becomes 65 65 becomes A 65/3 without casting becomes 21 65/3 with casting becomes 21.666666 3.14159 becomes 3

  28. // PROG0615.CPP // This program demonstrates integer overflow problems. #include <iostream.h> void main() { int Number = 20000; cout << "Number: " << Number << endl; Number += 10000; cout << "Number: " << Number << endl; Number += 10000; cout << "Number: " << Number << endl; Number = 32767; cout << "Number: " << Number << endl; Number++; cout << "Number: " << Number << endl; } PROG0615.CPP OUTPUT Number: 20000 Number: 30000 Number: -25356 Number: 32767 Number: -32768

  29. The Odometer and Integer Memory + 1 = + 1 = 32767 + 1 = 32768 0 9 9 9 9 9 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  30. How Positive Numbers Give Negative Results The first bit in a number is the sign bit It determines if a number is positive or negative 0 = Positive 1 = Negative 32767 + 1 = -32768 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

  31. Overflow Problems Overflow is a situation where the assigned value of a variable exceeds the allocated storage space. The resulting value that is stored will be inaccurate and can change from positive to negative or negative to positive. Avoid overflow problems by using a data type that can handle the size of the assigned values. It is important to save computer memory. However, do not be so stingy with memory that overflow problems occur.

  32. // PROG0616.CPP // This program demonstrates precision problems with float. #include <iostream.h> void main() { float Number; Number = 9.1; cout << "Number 9.1 = " << Number << endl; Number = 9.12; cout << "Number 9.12 = " << Number << endl; Number = 9.123; cout << "Number 9.123 = " << Number << endl; Number = 9.1234; cout << "Number 9.1234 = " << Number << endl; Number = 9.12345; cout << "Number 9.12345 = " << Number << endl; Number = 9.123456; cout << "Number 9.123456 = " << Number << endl; Number = 9.1234567; cout << "Number 9.1234567 = " << Number << endl; Number = 9.12345678; cout << "Number 9.12345678 = " << Number << endl; }

  33. PROG0616.CPP OUTPUT Number 9.1 = 9.1 Number 9.12 = 9.12 Number 9.123 = 9.123 Number 9.1234 = 9.1234 Number 9.12345 = 9.12345 Number 9.123456 = 9.123456 Number 9.1234567 = 9.123457 Number 9.12345678 = 9.123457

  34. // PROG0617.CPP // This program demonstrates precision with float and double. #include <iostream.h> #include <iomanip.h> // required for setprecision function void main() { float FNr; double DNr; cout << "BEFORE USING SETPRECISION(15)" << endl << endl; FNr = 9.123456; cout << "Float 9.123456 = " << FNr << endl; DNr = 9.123456; cout << "Double 9.123456 = " << DNr << endl << endl; FNr = 9.1234567; cout << "Float 9.1234567 = " << FNr << endl; DNr = 9.1234567; cout << "Double 9.1234567 = " << DNr << endl << endl; cout << setprecision(15);

  35. PROG0617.CPP Continued cout << "AFTER USING SETPRECISION(15)" << endl << endl; FNr = 9.1234567; cout << "Float 9.1234567 = " << FNr << endl; DNr = 9.1234567; cout << "Double 9.1234567 = " << DNr << endl; cout << endl; FNr = 9.12345678; cout << "Float 9.12345678 = " << FNr << endl; DNr = 9.12345678; cout << "Double 9.12345678 = " << DNr << endl; DNr = 9.123456789012; cout << "Double 9.123456789012 = " << DNr << endl; DNr = 9.1234567890123; cout << "Double 9.1234567890123 = " << DNr << endl; DNr = 9.12345678901234; cout << "Double 9.12345678901234 = " << DNr << endl; DNr = 9.123456789012345; cout << "Double 9.123456789012345 = " << DNr << endl;  }

  36. PROG0617.CPP OUTPUT BEFORE USING SETPRECISION(15) Float 9.123456 = 9.123456 Double 9.123456 = 9.123456 Float 9.1234567 = 9.123457 Double 9.1234567 = 9.123457 AFTER USING SETPRECISION(15) Float Number: 9.123456954956055 Double Number: 9.1234567 Float 9.12345678 = 9.1234569549566055 Double 9.12345678 = 9.12345678 Double 9.123456789012 = 9.123456789012 Double 9.1234567890123 = 9.1234567890123 Double 9.12345678901234 = 9.123456789012341 Double 9.123456789012345 = 9.123456789012344

  37. // PROG0618.CPP // This program demonstrates the various integer and real C++ data types #include <iostream.h> void main() { char Int1; // 1 byte -128..127 unsigned char Int2; // 1 byte 0..255 int Int3; // 2 bytes -32768..32767 unsigned int Int4; // 2 bytes 0..65535 long Int5; // 4 bytes -2,147,483,647..2,147,483,647 unsigned long Int6; // 4 bytes 0..4,294,967,295 float Float1; // 4 bytes 3.4e-38..3.4e38 double Float2; // 8 bytes 1.7e-308..1.7e308 long double Float3; // 10 bytes 3.4e-4932..1.1e4932 Int1 = 127; Int2 = 255; Int3 = 32767; Int4 = 65535; Int5 = 2147483647; Int6 = 4294967295; Float1 = 3.4e38; Float2 = 1.7e308; Float3 = 1.1e4932;

  38. PROG0618.CPP CONTINUED cout << (int) Int1 << endl; // type casting is needed cout << (int) Int2 << endl; // type casting is needed cout << Int3 << endl; cout << Int4 << endl; cout << Int5 << endl; cout << Int6 << endl; cout << endl; cout << Float1 << endl; cout << Float2 << endl; cout << Float3 << endl; } PROG0618.CPP OUTPUT 127 255 32767 65535 2147483647 4294967295 3.4e38 1.7e308 1.1e4932

  39. APCS Examination Alert C++ has many simple (single-value) data types. You are expected to understand and use only the following data types: char // (1 byte character or integer) int // (2 byte integer) double // (8 byte real number) bool // (will be explained later) For an APCS Examination, understand means that you can handle questions that include the specified topics. This is primarily true for the multiple choice examination. The word use means that you can write C++ source code that includes the specified topic.

  40. C++ Warning C++ is an industrial strength language. This language is designed for, and used for, many industry-wide applications. This means that C++ is expected to be used by professional programmers who can handle the many complexities of a major programming language. Computer science students, new to programming, can easily get in trouble because C++ does not provide many guards against student peculiarities. Student may also be using C-style programming that is considered obsolete by modern computer science standards. Even though C++ will compile older, C-style features, you should not use features that have not been introduced. Please check with your teacher about the inclusion of any program keyword, operator or style that you are not sure about. Frequently, there is the temptation to use some shortcut, to use some other method because it was shown in a magazine, presented on the Internet, or provided by a friend. Regardless of the source, you are well advised to follow your teacher’s guidelines closely. Failure to follow suggestions may result in computer lock-ups that can mean very time-consuming cold-boots.

More Related