C++ Numeric Types, Expressions, and Output

Overview of Chapter 3, Programming and Problem Solving with C++, 5th edition (2010)

C++ Data Types
C++ Data Types

* Types CHAR, SHORT, INT, and LONG are unsigned. An unsigned integer value is assumed to be only positive or zero.

Examples of Named Constant Declarations for Numberic Types:
const float PI = 3.14159;
const float E = 2.71828;
const float int = MAX_SCORE = 100;
const float int MIN_SCORE = -100;
const char = LETTER = ‘W’;
const string NAME = “Elizabeth”;

Examples of Variable Declarations:
int studentCount;
int sumOfScores;
float average;
char grade;
string stuName;

Given the following declarations:
int num;
int alpha;
float rate:
char ch;
the following are appropriate assignment statements:
alpha = 2856;
rate = 0.36;
ch = ‘B’;
num = alpha;

+ Unary plus
– Unary minus
+ Addition
– Substraction
* Multipication
/ Division (Integer dvision is without a fractional part)
% Modulus (Remainder from integer divsion)

The modules sign % is used to find the remainder of an integer division. Integer division is without floating point numbers therefore 7/2 is equal to 3. With use of modulus 7%2 is equal to 1, as this is the remainder when you divide 7/2. (p. 95)

Increment ++
Decrement —

Type Coercion
The implicit (automatic) conversion of a value from one data type to another.
For example:
float myFloat;
myFloat = 12;
Coercion changes the integer value of 12 in the above statement to the float value of 12.0.
Another example:
int myInt;
myInt = 4.852;
Coercion changes the float 4.852 into an integer 4, as the the floating point was removed.

Type Casting
The explicit conversion of a value from one data type to another; also know as type conversion.
For example:
myInt = myFloat + 8.2;
myInt = int(myFloat + 8.2);
In both examples, the type int is cast into type float.

Mixed type expression
An expression that contains operands of different data types; also called mixed mode expression.
For example:
myInt * myFloat
4.8 + myInt – 3

Sample Program to Clarify Function Calls:

  1. int main()
  2. {
  3. count << “The square of 10 is ” << Square(10) << endl:
  4. return 0;
  5. }
  6. int Square ( int n )
  7. {
  8. return n * n;
  9. }

The statement in line 3, the (main) causes the servant (Square) to compute the square of 10 and return the result back to the main.
In line 3 Square(10) is a function call or function invocation. The computer temporarily puts the main function on hold and starts the Square function running. When Square has finished, the computer goes back to main and picks up where it left off.

Function call (function invocation)
The mechanism that translates control to a function.

Argument list
A mechanism by which functions communicate with each other.

In line 3, the number 10 is known as an argument (or actual parameter). Arguments make it possible for the same function to work on many different values.
For example:
cout << Square(4);
cout << Square(125);

Syntax Template for a Function Call:
FunctionName( ArgumentList)

Some functions have two, three, four, or more arguments in the list, all separated by commas.

Library Functions
Programming languages include a large collegction of prewritten functions, data types, and other items that any programmer may use. In C++, the functions in the library are placed into separate files known as header files.
Header file: <cmath>
Function: sqrt(x)
Argument type: float
Result type: float
Result (Value Returned): Square root of x.

To use a library function, add the include statment at the top of your program.
For example:
#include <cmath>
The above example adds the library <cmath> that includes the sqrt function (Square root).

Void function (procedure)
A function that does not return a function to its caller and is invoked as a separate statement.

Value-returning function
A function that returns a single value to its caller and is invoked from within an expression.

Manipulators are used only in input and ouput statements.
For example, the the line:
cout << myInt << endl << myFloat;
uses the “End Line” manipulator (endl).
Syntax Template:
cout << ExpressionOrManipulator << ExpressionOrManiuplator … ;

Some other examples of manipulators:
Set Width: setw
Fix large floating point numbers: fixed
(Large values floating point numbers may be written as scientific numbers; the fixed manipulator causes a decimal to be displayed).
Set desired number of decimal points: setprecision
* Set Width applies only to the very next item printed. Set Precision applies to all subsequent values.

Dale, N. and Weems, C. (2010). Programming and Problem Solving with C++, 5th Edition, Jones and Bartlett Publishers, LLC, Sudbury, MA.

  Related Posts