Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numeric Types, Expressions, and Output ROBERT REAVES.

Similar presentations


Presentation on theme: "Numeric Types, Expressions, and Output ROBERT REAVES."— Presentation transcript:

1 Numeric Types, Expressions, and Output ROBERT REAVES

2 Additional String Operations  Four functions that we will use to operate on strings:  length  size  find  substr

3 Length Function  When applied to a string variable, it returns an unsigned integer value that equals the number of characters currently in the string.  If myName is a string variable, a call looks like this:  myName.length();  Use parentheses to signify an empty argument list.  This is a value-returning function, so the call must appear within an expression.

4 Dot Notation  Why the call to this function so weird?  Certain programmer-defined data types, such as strings, have functions that are associated with them and the dot notation is required in the calls.  myName.length();  Dot notation is the dot (period) between the variable name and the function name.  What is we don’t use the “dot” and say:  length();

5 Size Function  Same as length function.  This was to accommodate both terms, since people say size and length interchangeably.  Example:  myName.size();  Returns unsigned-integer value, thus you must declare results to be of type int.  string firstName;  int len;  firstName = “Robert”;  len = firstName.length(); //len = firstName.size();

6 Find Function  Find function searches a string to find the first occurrence of a particular substring and returns an unsigned integer value.  Takes one argument, a literal string or a string expression.  Ex:  str1.find(“the”);  str1.find(str2);  str1.find(str2 + “abc”);  str1 is searched to see if substring can be found within it.  If so, returns the position in str1 where the match beings. (Position start at 0)  Else returns “npos”, meaning not a position within the string”  Evaluates to something like 4294967295  Has to match exact.

7 Find Function  Find function can also be a char value, in this case it returns the position of the first occurrence of that character within the string else “npos”.  Only returns the FIRST matched position.

8 Substr Function  Substr function returns a particular substring of a string.  Ex:  String myString = “Hello, my name is Robert.”;  myString.substr(5, 20);  First argument is an unsigned integer that specifies a position within the string, and the second is an unsigned integer that specifies the length of the desired substring.  Returns the piece of the string that starts at the specified location and continues for the number of characters given by the second argument.  DOESN’T CHANGE YOUR ORIGINAL STRING

9 Numeric Data Types  Data types:  char, short, int, long  Are known as integral types (or integer types) because they refer to integer values, in other words WHOLE NUMBERS.  ex:  22  16  1  0  Commas are NOT ALLOWED  Minus sign preceding an integer will make it negative.

10 Numeric Data Types  Exception to this is when you add the reserved word unsigned to the data type name.  Ex: unsigned int  Assumed to only be positive or zero.  Used for specialized situations.  Meaning you won’t use these in here.  Each data type is intended to represent different sizes of integers, however this is machine dependent.

11 Numeric Data Types  Wait, why is a char an integer type?  C++ classifies char as an integral type so it can be used to store integer values with a very limited range  -128 to 127

12 Numeric Data Types  Float, or floating type are used to represent real numbers.  Have an integer part and a fractional part, with a decimal between.  Ex:  18.0  127.54 .8  Computer cannot always represent exactly.  Slight inaccuracies in the rightmost fractional digits are to be expected and are not the result of programmer error.  Ex:  4.8 might be 4.79999998

13 Named Constant Declartions  Just to show you can make constant variables of all data types.  const float PI = 3.14159;  const float E = 2.71828;  const int MAX_SCORE = 100;  const int MIN_SCORE = -100;  const char LETTER = ‘W’;  const string NAME = “Elizabeth”;

14 Arithmetic Operators  Operators allowed in an expression depend on the data types of the constants and variables in the expressions.  Arithmetic operators:  + Unary Plus  - Unary minus  + Addition  - Subtraction  * Multiplication  /  Floating-point division (floating-point result)  Integer division (no fractional part)  % Modulus (remainder from integer division.

15 Arithmetic Operators  Unary Operators an operator that has just one operand.  Ex:  -54  +259.65  -rate (can use on variables)  Rarely use unary plus, numeric constant is assumed to be positive.  Binary Operators an operator that has two operands.

16 Arithmetic Operators  % (modulus) is only used with integers.  Gives only the remainder.  10 / 7 = 3  / (division) can be used with integers or float  Integer division yields ONLY the quotient  10 / 7 = 1  Floating point yields a floating point result  10.0 / 7.0 = 1.3

17 Increment and Decrement Operators  C++ provides increment and decrement operators  ++ increment  -- decrement  Unary operators take a single variable as an operand.  For int and float operands, the effect is to add 1 or subtract 1 from the operand.  If num currently contains 8, what would this statement yield?  num++;  and  num--;

18 Increment and Decrement Operators  The ++ and – operators can be either prefix operators:  ++num;  or postfix operators:  num++;

19 Library Functions  C++ system includes a standard library-a large collection of prewritten functions, data types, and other times that any C++ programmer may use.  The functions in library are divided into separate files called header files.  Ex:  #include  This library had this like sqrt() and abs()


Download ppt "Numeric Types, Expressions, and Output ROBERT REAVES."

Similar presentations


Ads by Google