C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side of + -Subtractionx = 6 - 2;Subtract right value from left value * Multiplicationx = 6 * 2;Subtract right hand side values /Divisionx = 6/2;Divide left value by right value %Modulusx = 6 % 2;Remainder of left divided by right What about exponentiation??? Either x = 6 ^ 2 OR x = 6 ** 2 ??? NOTE: Dividing 2 real numbers yields EXACT results Dividing 2 Integers yields TRUNCATED results No Such Thing: You must write your own function (Or use an available one) Why??
C/C++ Operators Previously, we talked about how integers were stored. For example, we said the declaration short s1 = 56, s2 = 17; Asks for two 16-bit integer locations (s1 and s2) and stores the values 56 and 17 in them (respectively) Var/Loc s1: Var/Loc s2: We know that if we divide 56/17 (on a calculator) you would get the value But, how do you store a mantissa as an integer??(You don’t) OK – But what if I wanted to get the real answer above?? All Integers can be rewritten (as floats (casting)) The opposite is NOT true Decimal to Binary Converter
C/C++ Operators Consider (i.e., enter, build and run) the code below: #include "stdafx.h" #include using namespace std; void main() { int s1 = 56, s2 = 17; cout << s1 << " divided by " << s2 << " is " << s1/s2 << endl; cout << "Casting " << s1 << " and " << s2 << " and then dividing yields: " << (float)s1/(float)s2 << endl; } The output should appear as:
C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator cout << "The remainder of 9/2 is " << 9 % 2 << endl; Add the following line to your program, compile and run: How is this useful?? There are a number of uses. We are going to take a look at one example: Converting decimal values to binary
C/C++ Operators There is one additional binary Operator we need to discuss in more detail: The Modulus operator cout << "The remainder of 9/2 is " << 9 % 2 << endl; Add the following line to your program, compile and run: How is this useful?? There are a number of uses. We are going to take a look at one example: Converting decimal values to binary
Converting from decimal to binary Dividing any integer by 2 means that the remainder must be either 0 or 1 A few slides ago, we noted that = How can we verify this?? When you first learned division, you probably were taught to do it as follows: goes into 56, 28 times. 2 times 28 is 56. Subtracting -56 from 56 leaves a remainder of goes into 28, 14 times. 2 times 14 is 28. Subtracting -28 from 28 leaves a remainder of goes into 14, 7 times. 2 times 7 is 14. Subtracting -14 from 14 leaves a remainder of goes into 7, 3 times. 2 times 3 is 6. Subtracting -6 from 7 leaves a remainder of goes into 3, 1 times. 1 times 3 is 3. Subtracting -3 from 7 leaves a remainder of goes into 1, 0 times. 0 times 1 is 0. Subtracting 0 from 1 leaves a remainder of 1
Converting from decimal to binary Let’s rewrite this procedure in a simpler form Pass remainder 1 quotient Gathering the remainders from last to first: Stop when the quotient becomes =56 How do we do that?? We’re going to have to go over your first abstract data type: An Array
Arrays Let’s create an array of short integers An array is an arrangement of similar basic data types short binary[15], temp[15], decval = 56, reverse = 0, index = 0, i; while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; Next, we’ll create the array as we did before Now we need to reverse the order of the array i = index; while (reverse <= index) { binary[reverse] = temp[i]; reverse++; i--; } The complete programming (including the printing of the result) is given on the next slide
Arrays #include "stdafx.h" void main() { short binary[15], temp[15], decval = 177, reverse = 0, index = 0, i; while (decval > 0) { temp[index] = decval % 2; index++; decval = decval/2; } index--; for (i = 0; i <= index; i++) printf("%d",temp[i]); printf("\n\n"); i = index; while (reverse <= index) { binary[reverse] = temp[i]; printf("%d %d %d\n",i, reverse, binary[reverse]); reverse++; } i--; for (i = 0; i <= index; i++) printf("%d", binary[i]); printf("\n"); } As usual, you should enter the code, compile it, and run it
Assignment Operators Assignment Operators: Assigning Values to RAM: Operator = Example x = 6 + 2; Definition. Location x will get the value ( = 8) +=x += 3 IF Same as x = x + 3: IF x contained the value 4, it now contains the value 7 IF Same as x = x - 3: IF x contained the value 4, it now contains the value 1 =x = 3 *=x *= 3 IF Same as x = x * 3: IF x contained the value 4, it now contains the value 12 /=x /= 3 IF Same as x = x / 3: IF x contained the value 4: IF IF x is a float (real), it now contains 1.33 IF IF x is an integer, it now contains the value 1 %=x %= 3 IF ONE Same as x = x % 3: IF x contained the value 4, it now contains the value 1 (3 goes into 4 Once with ONE left over)
Unary Operators Unary Operators: Operators on a single Operand: Operator - Meaning Minus Sign Example x = -2; Definition. Assign a negative value +Plus Signx = +2;Assign a Positive value ++Increment x = ++y; Prefix Notation OR OR x = y++; Postfix Notation -- Decrement x = --y; Prefix Notation OR OR x = y--; Postfix Notation Prefix and Postfix Notation?? What’s that all About ??? int x,y,a,b; a = b = 1;// Initialize the variables (locations) with the value 1 x = 2 * ++a;// PREFIX Notation: First increment a by 1 (a = 2) // Then Multiply by 2 & assign to x (x=2*2) y = 2 * b++;// POSTFIX Notation: First multiply b by 2 & assign to y (y=1*2) // Then increment b by 1 (b = 2) 4 2 printf("%d %d\n",x,y); // The Output would appear as: 4 2 Consider the following section of c code:
There are two (2) Additional Unary Operators: sizeofReturn the size of the operand (in bytes) int i = 12; float f = ; printf("%d sizeof = %d\n",i,sizeof i); // would produce: 12 sizeof = 2 printf("%7.2f sizeof = %d\n",f,sizeof f); // would produce: sizeof = 4 Consider the following section of c code: (type) Convert a value to the specified data type int a = 4; float b = ; printf("%d %ld\n",a,(long) a);// would produce: 4 4 printf("%d %f\n",a,(float) a);// would produce: printf("%f %d\n",b,(int) b); }// would produce: Consider the following section of c code:
Relational Operators Relational Operators: Operators on two Operands Yielding a T/F Answer: OperatorMeaningExampleDefinition. < Less Than x < 2;False if x contains the value 2 <= Less Than OR Equal To x <= 2;True if x contains the value 2 = Equal To x = = 2;True if x contains the value 2 ! = Not Equal To x ! = 2;False if x contains the value 2 > Greater than x > 2;False if x contains the value 2 >= Greater than OR Equal To x >= 2;True if x contains the value 2 && And x > 2 && x < 4 False if x contains the value 2 | Or x = = 2 | | x = = 4 True if x contains the value 2 ! Not! (x = = 2) False if x contains the value 2
Order of Operations: Order 1 Operator ( ) (unary) ! (Rel) 3 * / % (binary) 5 < > <= >= 6 == != 9 = Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; What is the value of the statement: a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1; | | 8 && 7
Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; = 56 * 7 = = 485 % 4 = 1 1 * 1 = 1 1 / 5 = 0 * 8 = 384 / 4 = = = - 95
Example 2: Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; Evaluate: ++a * b-- + c % d / e - f-- * g + 6 * h++ / b + h; 2 2 * 2 = 43 % 4 = 3 3 / 5 = 0 6 * 7 = 426 * 8 = / 2 = = = = = - 6 Note that now: a = 2, b = 1, f = 5, h = 9
Example 3: Given: int a = 1, b = 2, c = 3, d = 4; Consider the statement: a > b || b < c && c == 3 || d < 4 && b != c FalseTrueFalse True False True
scanfprintf Conversion specifiers for scanf and printf: Specifier(s)Output %cSingle Character %d %iSigned decimal Integer %fSigned floating-point, decimal notation Example B %e %ESigned floating-point, e or E notation -4.5e3 2.1E-2 %g %GUse shorter or %f or %e (%f or %E) E4 %oUnsigned octal notation4271 %u7832Unsigned decimal Integer %ld %luSigned/Unsigned long Integer %Lf %Le Long double floating-pt (also %LE) e %x %XUnsigned hexadecimal notation4d2a F6B %sCharacter stringHello %pPointer (address)4FF0: 8BC1 %Print a % sign% Additional information about printf/scanf specifiers can be found in the Supplementary Materials Link
Additional Precompiler Directives #define PI = User Defined constants: #define SQUARE (x) x * x User Defined constants: // macro example.cpp : Defines the entry point for the console application. #include "stdafxh" #include using namespace std; #define PI #define SQUARE(X) X*X void main() { float area, r = SQUARE(3.5); area = PI * r; cout << "The area is:" << area << endl; }