1 / 27

Chapter 3 Numeric Types, Expressions, and Output

Chapter 3 Numeric Types, Expressions, and Output. 3.1 Numeric Data Types 3.2 Arithmetic Expressions 3.3 Function Calls and Library Functions 3.4 Formatting the Output 3.5 Additional string Operations. Chapter 3. Knowledge Goals:.

kaelem
Download Presentation

Chapter 3 Numeric Types, Expressions, and Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Numeric Types, Expressions, and Output 3.1 Numeric Data Types 3.2 Arithmetic Expressions 3.3 Function Calls and Library Functions 3.4 Formatting the Output 3.5 Additional string Operations

  2. Chapter 3 Knowledge Goals: 1.To understand implicit type coercion and explicit type conversion; 2.To recognize and understand the purpose of function arguments; 3.To learn and use additional operations associated with the string type; 4.To learn how to format program statements in a clear and readable fashion.

  3. Skill Goals: To be able to: 1.Declare named constants and variables of type int and float. 2.Construct simple arithmetic expressions. 3.Evaluate simple arithmetic expressions. 4.Construct and evaluate expressions that contain multiple arithmetic operations. 5.Call a value-returning function and a void function. 6.Use C++ manipulators to format output.

  4. 3.1 Numeric Data Types 1. Integral Types char short int long bool integral eg: 22 0 -912

  5. 2. Floating-Point Types float double long double Floating Floating-point numbers have an integer part and a fractional part, with a decimal point in between. Either the integer part or the fractional part, but not both, may be missing. Floating-point values also can have an exponent, as in scientific notation. eg: 18.6 0.57 4. 16.6786 .8 1.6836E-12 7E26

  6. 3. Declarations for Numeric Types Named Constant Declarations const float PI=3.14159; const int MAX_SCORE=100; Variable Declarations int sumofscores; float average; char grade; string stuName; sumofscores=275; average=91.7; grade=‘A’; stuName=“Mary”;

  7. 3.2 Arithmetic Expressions 1. 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) eg: 6 / 2 =3 7 / 2 =3 7.0 / 2.0 =3.5 7 % 2 =1

  8. 2. Increment and Decrement Operators ++ Increment -- Decrement eg: num++ num = num + 1; a++ ++a a- - - -a aa = a++; means: aa = a; a = a + 1; aa = ++a; means: a = a + 1; aa = a;

  9. int main( ) { int v1=0,v2=7,v3=16; cout<<“v2++:”<<v2++<<“ v2:”<<v2<<endl; cout<<“++v3:”<<++v3<<“ v3:”<<v3<<endl; v1=v2++; cout<<“v1:”<<v1<<“ v2:”<<v2<<endl; v1=++v3; cout<<“v1:”<<v1<<“ v3:”<<v3<<endl; cout<<“now,v1=”<<v1<<“ v2=”<<v2<<“ v3=”<<v3; return 0; } v1 v2 v3 8 17 8 9 18 18

  10. Key: v2++: 7 v2:7 ++v3: 17 v3:16 V1: 8 v2: 9 V1: 18 v3:18 Now,v1=18 v2=9 v3=18

  11. C++ provides several built-in numeric data types, of which the most commonly used areintandfloat. The integral types are based on the mathematical integers, but the computer limits the range of integer values that can be represented.

  12. The floating-point types are based on the mathematical notion of real numbers. As with integers, the computer limits the range of floating-point numbers that can be represented. We can write literals of type floatin several forms, including scientific (E) notation.

  13. eg: 1. Here are some examples of floating-point numbers, which of the show is invalid? (A) .E15 (B) .35 (C) 3E5 (D) 3E-5 2. Assume that a and b areintvariables with a containing 2 and b containing 2. ++a+b=___________; a=____________; b=____________; 5 3 2

  14. Much of the computation of a program is performed in arithmetic expressions. Expressions can contain more than one operator. The order in which the operations are performed is determined by precedence rules. 3. Precedence Rules

  15. Highest precedence level:Unary + Unary – Middle level:* / % Lowest level:+ -

  16. 4. Type Coercion and Type Casting eg: int someInt; float someFloat; 4 someInt=4.8; someFloat=12; Type Coercion 12.0 someInt=someFloat+8.2; someInt=int(someFloat+8.2); Type Casting

  17. someInt * someFloat 4.6 + someInt - 3 float(someInt) * someFloat 4.6 + float(someInt - 3) int sum=60; int count=80; float average; × average = sum / count; √ average = float(sum) / float(count);

  18. 3.3 Function Calls and Library Functions 1. Value-Returning Functions eg: int Square (int n) { return n * n; } int Cube (int n) { return n * n * n; }

  19. int main( ) { cout<<“The square of 27 is ”<<Square(27)<<endl; cout<<“and the cube of 27 is ”<<Cube(27)<<endl; return 0; } function call Function Name (Argument List) Here are several facts about value-returning functions:

  20. ▲ The function call is used within an expression; it does not appear as a separate statement. someInt = Cube(2) *10; alpha = Cube(int1*int1 + int2*int2); alpha = Cube(Square(int1) + Square(int2)); ▲ The function computes a value (result) that is then available for use in the expression. ▲ The function returns exactly one result-no more, no less.

  21. 2. Library Functions header file #include <cstdlib> using namespace std; eg: #include <iostream> #include <cmath> //For sqrt( ) and fabs( ) using namespace std; ︰ float alpha; float beta; ︰ alpha=sqrt(7.3 + fabs(beta));

  22. 3. Void Functions void printline( ) { cout<<“******”<<endl; } Function main would call the printline function like this: printline ( );

  23. 3.4 Formatting the Output endl #include <iostream> fixed showpoint setw #include <iomanip> setprecision Integers and Strings Floating-Point Numbers

  24. ▲ large floating-point values are printed in scientific (E) notation. fixed 123456789.5 1.23457E+08 ▲ If the number is a whole number, C++ doesn’t print a decimal point. showpoint 95.0 95 ▲ use the setprecision manipulator as follows: 4.827 cout<<setw(6)<<setprecision(1)<<x; □□□4.8

  25. 3.5 Additional string Operations In this section, we introduce four functions that operate on strings:length, size, find, and substr. 1. The length and size Functions string name; string::size_type len; name = “Edison”; len = name.length( ); cout<<name.length( )<<endl; //Prints 6 cout<<len<<endl; //Prints 6

  26. 2. The find Function Given the code segment string phrase; string::size_type position; phrase = “The dog and the cat”; The statement position = phrase.find(“the”); 12 position = phrase.find(“rat”); string::npos

  27. 3. The substr Function myString = “Programming and Problem Solving”; function call myString.substr(0,7) “Program” myString.substr(7,8) “ming and” myString.substr(24,40) “Solving”

More Related