intro_C_II

advertisement
Alina Vincent, CS 201 Spring 2004
1/28/04
Introduction to C++ (II)
Data types

Integers (int): zero, any positive or negative number without a decimal point.
Examples: 0
+11
456 -3
-2345
-10
10
Other data types for integers are long, short, and unsigned – the difference is in the
amount of space the compiler allocates to store them.
Declarations:

int value_1;
int num1, num2, num3;
int total = 0;
Floating-point numbers (float): real numbers, any signed or unsigned number with a
decimal point.
Examples: +11.23
-5.6 .7 -0.0001 6. 0.0 +100.
You can also use double and long double to store floating-point numbers – the matter
of precision/ amount of storage used per number.
Declarations:

float value_2;
float num4, num5, num6;
float sum = 0;
Character values (char): characters – letters of the alphabet (uppercase and lowercase),
the ten digits and special symbols (!, @, $…).
NOTE: until we get to arrays, char represents a single character – only one letter, one
digit, etc..
Examples: ‘V’
‘+’
‘6’
‘&’
‘d’
Character values are typically stored in the computer using ASCII code (see page 53)
Example: ‘A’ = 01000001
Declarations:
char character_1;
char char_1, char_2, char_3;
char MyLetter = ‘A’;
1
Alina Vincent, CS 201 Spring 2004
1/28/04
Arithmetic Operations:
Addition
Subtraction
Multiplication
Division
Modulus
Negation
+
*
/
%
-
An integer expression is an expression that contains only integer operands.
A floating-point expression is an expression that contains only floating-point values.
A mixed-mode expression is the one containing both integer and floating point operands.
Rules:
1. If both operands are integers, the result of the operation will be an integer.
2. If one operand is a floating-point value, the result of the operation is a doubleprecision value.
Warning: The division of two integers will yield an integer result ( the fractional part will
be truncated/dropped).
Example:
7/3=2
(!!!!)
Modulus Operator: calculates the remainder of an integer division.
Example: 7 % 2 = 1
13 / 3
=
20 / 2
=
13 % 3 =
20 % 5 =
(5+10)/4 =
5/3+8%3 =
Operator Precedence

Operators in C++ have the following precedence
1. Unary + and – (highest)
2. *, /, %
3. +, (lowest)

Parentheses should be used to specify logical grouping of operands.
2
Alina Vincent, CS 201 Spring 2004
1/28/04
Math Library Functions

The math library, like other C++ libraries, is a group of functions that you can include
in your program to save yourself the trouble of writing the code to do standard
mathematical computations

In order to use math library, you need to include the following preprocessor directive:
# include <cmath>

In order to access the individual functions, you need to know the same four things
that are required to use any other function:
1. The name of the function
2. Exactly what it does
3. What arguments the function is expecting you to provide
4. What data type result will be returned to you


Some of the common math library functions are:

absolute value (of a): abs (a)

power (a raised to the power b): pow (a,b)

square root (of a): sqrt (a)

sin (or cos, or tan): sin (a)

log (natural log of a): log (a)

base 10 log of a: log10 (a)
Since each of these functions returns a value, it may be used as part of an
assignment statement
Example: square_root = sqrt (a);

Some of the functions are present in multiple versions, each of which
expects a different data type as an argument. This is an example of a C++
feature called function overloading. For now, you don't need to worry about
what this means. All you need to know is that you can use the same math
library function with data of multiple types. (The compiler is ok with this,
because it can tell by the data type you use to call the function which
version to invoke.)
3
Download