Exposure C++ Chapter VI Data Type Operations C++ Integer Operations Symbols +Addition -Subtraction...
-
Upload
rachel-hubbard -
Category
Documents
-
view
218 -
download
0
Transcript of Exposure C++ Chapter VI Data Type Operations C++ Integer Operations Symbols +Addition -Subtraction...
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(){ 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 = 130100 - 30 = 70100 * 30 = 3000100 / 30 = 3100 % 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 <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 = 100Nr2 = 30Nr1 = 101Nr2 = 31Nr1 = 100Nr2 = 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 <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 1011 1011 11
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
// 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
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
// 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 ondifferent computers. Also, if you moveint x=10; before the void main(), itcan 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 <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.2999881000 - 3.3 = 996.7000121000 * 3.3 = 33001000 / 3.3 = 303.030304
// 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.31000 - 3.3 = 996.71000 * 3.3 = 33001000 / 3.3 = 303.030303
Mathematical Precedence
Parentheses
Exponents
Multiplication & Division
Addition & Subtraction
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
// 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 = 2750A / B * C = 25A * B / C = 40000
// 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
// 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 = FA + 10 = KChar1++ = 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 <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
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 <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
A653.14159
A becomes 6565 becomes A65/3 without casting becomes 2165/3 with casting becomes 21.6666663.14159 becomes 3
// 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
A653.14159
A becomes 6565 becomes A65/3 without casting becomes 2165/3 with casting becomes 21.6666663.14159 becomes 3
// 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: 20000Number: 30000Number: -25356Number: 32767Number: -32768
The Odometer and Integer Memory
+ 1 =
+ 1 =
32767 + 1 = 32768
0 9 9 9 9 9
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
1 0 0 0 0 0
How Positive Numbers Give Negative Results
The first bit in a number is the sign bitIt 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
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 <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;}
PROG0616.CPP OUTPUT
Number 9.1 = 9.1Number 9.12 = 9.12Number 9.123 = 9.123Number 9.1234 = 9.1234Number 9.12345 = 9.12345Number 9.123456 = 9.123456Number 9.1234567 = 9.123457Number 9.12345678 = 9.123457
// 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);
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; }
PROG0617.CPP OUTPUT
BEFORE USING SETPRECISION(15)
Float 9.123456 = 9.123456Double 9.123456 = 9.123456
Float 9.1234567 = 9.123457Double 9.1234567 = 9.123457
AFTER USING SETPRECISION(15)
Float Number: 9.123456954956055Double Number: 9.1234567
Float 9.12345678 = 9.1234569549566055Double 9.12345678 = 9.12345678Double 9.123456789012 = 9.123456789012Double 9.1234567890123 = 9.1234567890123Double 9.12345678901234 = 9.123456789012341Double 9.123456789012345 = 9.123456789012344
// 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;
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
1272553276765535214748364742949672953.4e381.7e3081.1e4932
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