Download presentation
Presentation is loading. Please wait.
Published byElinor Sparks Modified over 9 years ago
1
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??
2
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) 00000000 11100000 Var/Loc s1: 56 10 00000000 01000100 Var/Loc s2: 17 10 We know that if we divide 56/17 (on a calculator) you would get the value 3.2941176470588235294117647058824 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
3
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:
4
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
5
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
6
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 = 111000 2 56 10 How can we verify this?? When you first learned division, you probably were taught to do it as follows: 2 56 28 -56 0 2 goes into 56, 28 times. 2 times 28 is 56. Subtracting -56 from 56 leaves a remainder of 0 2 28 14 -28 0 2 goes into 28, 14 times. 2 times 14 is 28. Subtracting -28 from 28 leaves a remainder of 0 2 14 7 -14 0 2 goes into 14, 7 times. 2 times 7 is 14. Subtracting -14 from 14 leaves a remainder of 0 2 7 3 -6 1 2 goes into 7, 3 times. 2 times 3 is 6. Subtracting -6 from 7 leaves a remainder of 1 2 3 1 -2 1 2 goes into 3, 1 times. 1 times 3 is 3. Subtracting -3 from 7 leaves a remainder of 1 2 1 0 -0 1 2 goes into 1, 0 times. 0 times 1 is 0. Subtracting 0 from 1 leaves a remainder of 1
7
Converting from decimal to binary Let’s rewrite this procedure in a simpler form 0 56 2 Pass 0 028 2 remainder 1 quotient 0 2 14 2 1 2 7 3 1 2 3 4 1 2 1 5 0 Gathering the remainders from last to first: Stop when the quotient becomes 0 111000=56 How do we do that?? We’re going to have to go over your first abstract data type: An Array
8
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
9
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
10
Assignment Operators Assignment Operators: Assigning Values to RAM: Operator = Example x = 6 + 2; Definition. Location x will get the value 6 + 2 ( = 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)
11
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:
12
There are two (2) Additional Unary Operators: sizeofReturn the size of the operand (in bytes) int i = 12; float f = -123.45; printf("%d sizeof = %d\n",i,sizeof i); // would produce: 12 sizeof = 2 printf("%7.2f sizeof = %d\n",f,sizeof f); // would produce: -123.45 sizeof = 4 Consider the following section of c code: (type) Convert a value to the specified data type int a = 4; float b = 12.245; printf("%d %ld\n",a,(long) a);// would produce: 4 4 printf("%d %f\n",a,(float) a);// would produce: 4 4.000000 printf("%f %d\n",b,(int) b); }// would produce: 12.245000 12 Consider the following section of c code:
13
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
14
Order of Operations: Order 1 Operator ( ) 2 + - ++ -- (unary) ! (Rel) 3 * / % 4 + - (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
15
Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8; 2 + 3 = 56 * 7 = 42 + 6 = 485 % 4 = 1 1 * 1 = 1 1 / 5 = 0 * 8 = 384 / 4 = 96 0 - = - - + 1 = - 95
16
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 = 48 48 / 2 = 24 4 + 0 = 4 4 - 42 = - 38 - 38 + 24 = - 14 - 6 - 14 + 8 = - 6 Note that now: a = 2, b = 1, f = 5, h = 9
17
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
18
scanfprintf Conversion specifiers for scanf and printf: Specifier(s)Output %cSingle Character %d %iSigned decimal Integer %fSigned floating-point, decimal notation Example B 457 -6.576 %e %ESigned floating-point, e or E notation -4.5e3 2.1E-2 %g %GUse shorter or %f or %e (%f or %E) -2.1 4.56E4 %oUnsigned octal notation4271 %u7832Unsigned decimal Integer %ld %luSigned/Unsigned long Integer %Lf %Le Long double floating-pt (also %LE)7.32 -6.1e4 -345 64 %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
21
Additional Precompiler Directives #define PI = 3.15149 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 3.15149 #define SQUARE(X) X*X void main() { float area, r = SQUARE(3.5); area = PI * r; cout << "The area is:" << area << endl; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.