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 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 = = * 30 = / 30 = % 30 = 10
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
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
Flashback To Elementary School Long Division
// PROG0602.CPP // This program demonstrates incrementers and decrementers. #include 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
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;
// PROG0603.CPP // This program demonstrates the difference between // the X++ and ++X incrementers. #include void main() { int X1,X2; X1 = X2 = 10; cout << X1 << " " << X2 << endl; cout << ++X1 << " " << X2++ << endl; cout << X1 << " " << X2 << endl; } PROG0603.CPP OUTPUT
Basic Operation Shortcuts No Shortcut Notation Shortcut Notation K = K + 5K += 5 K = K - 5K -= 5 K = K * 5K *= 5 K = K / 5K /= 5 K = K % 51K %= 5
// PROG0604.CPP // This program demonstrates using shortcut notation with // each one of the 5 basic operations. #include // 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 += Nr -= Nr *= Nr /= 5 36
The Danger of Combining Shortcuts // PROG0605.CPP // This program demonstrates complexity caused // by combining incrementers. #include void main() { int X = 10; ++X += X++; cout << "X = " << X << endl << endl; } PROG0605.CPP OUTPUT X = 23
// PROG0606.CPP // This program shows complexity with combining // incrementers. #include 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.
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;
C++ Real Number Operations +Addition -Subtraction *Multiplication /Real number division (no remainder)
// PROG0607.CPP // This program demonstrates real number operations implemented // with the float type. #include 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 = = * 3.3 = / 3.3 =
// PROG0608.CPP // This program demonstrates real number operations // implemented with the double type. #include 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 = = * 3.3 = / 3.3 =
Mathematical Precedence Parentheses Exponents Multiplication & Division Addition & Subtraction
Hidden Operators in Mathematics MathematicsC++ Source Code 5XY5*X*Y 4X + 3Y4*X + 3*Y 6(A + B)6 * (A + B) A + B(A + B) / (A - B) A - B
// PROG0609.CPP // This program demonstrates math precedence in C++. #include 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
// PROG0610.cpp // This program demonstrates that characters are stored as numbers. #include 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
// PROG0611.cpp // This program demonstrates operations with character variables. #include 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
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.
// PROG0612.cpp // This program demonstrates concatenation with strings. #include #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
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
// PROG0613.CPP // This program demonstrates type casting in C++. #include void main() { char Var1; int Var2; float Var3; Var1 = 'A'; Var2 = 65; Var3 = ; 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 << " becomes " << (int) Var3 << endl; } PROG0613.CPP OUTPUT A A becomes becomes A 65/3 without casting becomes 21 65/3 with casting becomes becomes 3
// PROG0614.CPP // This program demonstrates a second syntax style for type casting. #include void main() { char Var1; int Var2; double Var3; Var1 = 'A'; Var2 = 65; Var3 = ; 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 << " becomes " << int (Var3) << endl; } PROG0614.CPP OUTPUT A A becomes becomes A 65/3 without casting becomes 21 65/3 with casting becomes becomes 3
// PROG0615.CPP // This program demonstrates integer overflow problems. #include 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: Number: Number: Number: Number:
The Odometer and Integer Memory + 1 = =
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 =
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.
// PROG0616.CPP // This program demonstrates precision problems with float. #include 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 = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; Number = ; cout << "Number = " << Number << endl; }
PROG0616.CPP OUTPUT Number 9.1 = 9.1 Number 9.12 = 9.12 Number = Number = Number = Number = Number = Number =
// PROG0617.CPP // This program demonstrates precision with float and double. #include #include // required for setprecision function void main() { float FNr; double DNr; cout << "BEFORE USING SETPRECISION(15)" << endl << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl << endl; cout << setprecision(15);
PROG0617.CPP Continued cout << "AFTER USING SETPRECISION(15)" << endl << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl; cout << endl; FNr = ; cout << "Float = " << FNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; DNr = ; cout << "Double = " << DNr << endl; }
PROG0617.CPP OUTPUT BEFORE USING SETPRECISION(15) Float = Double = Float = Double = AFTER USING SETPRECISION(15) Float Number: Double Number: Float = Double = Double = Double = Double = Double =
// PROG0618.CPP // This program demonstrates the various integer and real C++ data types #include void main() { char Int1;// 1 byte unsigned char Int2; // 1 byte int Int3; // 2 bytes unsigned int Int4; // 2 bytes 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 e38 double Float2; // 8 bytes 1.7e e308 long double Float3; // 10 bytes 3.4e e4932 Int1 = 127; Int2 = 255; Int3 = 32767; Int4 = 65535; Int5 = ; Int6 = ; Float1 = 3.4e38; Float2 = 1.7e308; Float3 = 1.1e4932;
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 e38 1.7e e4932
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. APCS Examination Alert
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. C++ Warning