c/c++ operators binary operators: operators between two operands: operator + meaningexample...

23
C/C++ Operators Binary Operators: Operators Between Two Operands: Operato r + Meaning Example Definition . Additi on x = 6 + 2; Add the values on either side of + - Subtractio n x = 6 - 2; Subtract right value from left value * Multiplica tion x = 6 * 2; Subtract right hand side values / Division x = 6/2; Divide left value by right value % Modulus x = 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??

Upload: elinor-sparks

Post on 02-Jan-2016

239 views

Category:

Documents


0 download

TRANSCRIPT

C/C++ Operators

Binary Operators: Operators Between Two Operands:

Operator+

Meaning Example Definition.Addition x = 6 + 2; Add the values on either side of +

- Subtraction x = 6 - 2; Subtract right value from left value

* Multiplication x = 6 * 2; Subtract right hand side values/ Division x = 6/2; Divide left value by right value

% Modulus x = 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 resultsDividing 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)

0 0 0 0 0 000 1 1 1 0 0 000Var/Loc s1: 5610

0 0 0 0 0 000 0 1 0 0 0 100Var/Loc s2: 1710

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

C/C++ OperatorsConsider (i.e., enter, build and run) the code below:

#include "stdafx.h"#include <iostream>

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++ OperatorsThere 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++ OperatorsThere 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 binaryDividing any integer by 2 means that the remainder must be either 0 or 1A few slides ago, we noted that = 1110002 5610

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

Converting from decimal to binaryLet’s rewrite this procedure in a simpler form

0 56 2

Pass

0

028 2

remainder

1

quotient

02 14 2

12 7 3

12 3 4

12 1 5

0

Gathering the remainders from last to first:

Stop when the quotient becomes 0

1 1 1 0 0 0 = 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 integersAn 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: Assigning Values to RAM:

Operator

=

Example

x = 6 + 2;

Definition.

Location x will get the value 6 + 2 ( = 8)+= x += 3 Same as x = x + 3: IF x contained the

value 4, it now contains the value 7

Same as x = x - 3: IF x contained the value 4, it now contains the value 1

= x = 3

*= x *= 3 Same as x = x * 3: IF x contained the value 4, it now contains the value 12

/= x /= 3 Same as x = x / 3: IF x contained the value 4: IF x is a float (real), it now contains 1.33 IF x is an integer, it now contains the value 1

%= x %= 3 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: Operators on a single Operand:Operator

-

Meaning

Minus Sign

Example

x = -2;

Definition.

Assign a negative value+ Plus Sign x = +2; Assign a Positive value

++ Increment x = ++y; Prefix Notation OR x = y++; Postfix Notation

-- Decrement x = --y; Prefix Notation 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) 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:

sizeof Return 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 = 2printf("%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 4printf("%d %f\n",a,(float) a); // would produce: 4 4.000000printf("%f %d\n",b,(int) b); } // would produce: 12.245000 12

Consider the following section of c code:

Relational Operators: Operators on two Operands Yielding a T/F Answer:Operator Meaning Example Definition.

< 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

1Operator( )

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

a = (a * ((b + c) % d) / e) - (f * g + 6) * h / 4 + 1;

Given: int a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8;

2 + 3 = 5 6 * 7 = 42

42 + 6 = 485 % 4 = 1

1 * 1 = 1

1 / 5 = 0 48 * 8 = 384

384 / 4 = 96

0 - 96 = - 96

- 96 + 1 = - 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 = 4 3 % 4 = 3

3 / 5 = 0 6 * 7 = 42 6 * 8 = 48

48 / 2 = 24

4 + 0 = 4

4 - 42 = - 38

- 38 + 24 = - 14

- 14 + 8 = - 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

False True FalseTrue True

True False

True

True

Conversion specifiers for scanf and printf:

Specifier(s) Output %c Single Character%d %i Signed decimal Integer

%f Signed floating-point, decimal notation

Example B457

-6.576%e %E Signed floating-point, e or E notation -4.5e3 2.1E-2%g %G Use shorter or %f or %e (%f or %E) -2.1 4.56E4

%o Unsigned octal notation 4271

%u 7832Unsigned decimal Integer%ld %lu Signed/Unsigned long Integer

%Lf %Le Long double floating-pt (also %LE) 7.32 -6.1e4

-345 64

%x %X Unsigned hexadecimal notation 4d2a F6B%s Character string Hello%p Pointer (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 = 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 <iostream>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;}