Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.

Similar presentations


Presentation on theme: "Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy."— Presentation transcript:

1 Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy

2 Outline  The cin object  Expressions  Type casting  Named Constant  More on I/O

3 The cin Object  Standard input object  Used to read input from keyboard  Like cout, requires iostream file  Often used with cout to display a user prompt first  Information retrieved from cin with >>

4 The cin Object  User input goes from keyboard to keyboard buffer  cin converts information to the type that matches the variable: Input a single integer; Input a single integer; short aShort; short aShort; cout << “Input a short-type number:"; cin >> aShort; cout<< “The number =”<<aShort<<endl; cout<< “The number =”<<aShort<<endl;

5 The cin object  Floating-point number input double aDouble; double aDouble; cout << “Input a double type number:"; cin >> aDouble; cout<< “The double number =”<<aDouble<<endl; cout<< “The double number =”<<aDouble<<endl;  Character input char aChar; char aChar; cout << “Input a character:"; cin >> aChar; cout<< “The character is ”<<aChar<<endl; cout<< “The character is ”<<aChar<<endl;

6 The cin Object  Can be used to input > 1 value: int int1,int2, int3; int int1,int2, int3; cout<<“Please input three int-type numbers:” cout<<“Please input three int-type numbers:” cin>>int1>>int2>>int3; cin>>int1>>int2>>int3; cout <<“int1= ”<<int1 <<“ int2= ”<<int2 cout <<“int1= ”<<int1 <<“ int2= ”<<int2 <<“ int3= ”<<int3<<endl; <<“ int3= ”<<int3<<endl;  Multiple values from keyboard must be separated by spaces  Order is important: first value entered goes to first variable, etc.

7 The cin Object  Gather multiple values with different data types int aInt; int aInt; double aDouble; double aDouble; char aChar; char aChar; cout<<“Please input three different data types values:” cout<<“Please input three different data types values:” cin>>aInt>>aDouble>>aChar; cin>>aInt>>aDouble>>aChar; cout <<“Int is ”<<aInt <<“ Double is ”<<aDouble cout <<“Int is ”<<aInt <<“ Double is ”<<aDouble <<“ char is ”<<aChar<<endl; <<“ char is ”<<aChar<<endl;

8 Exercise //This program calculates Hui Liu's part-time pay #include using namespace std; void main() { float workPayRate,pay; int workHour; int seeResults; cout << "How many hours do you work every work? "<<endl; cin >> workHour; cout<<"How much do you get paid per hour?"<<endl; cin>>workPayRate; pay=workPayRate*workHour; cout<<"Hui Liu: You will get "<<pay <<" dollars every work for the part time job!"<<endl; cin>>seeResults; }

9 The cin Object  Character string input #include #include using namespace std; int main() { char name[21]; cout<<“What is your name? ”; cin>>name; cout<<“Good Morning ”<<name<<endl; return 0; }  The cin object will let the user enter a string larger than the array can hold. If this happens, the string will overflow the array’s boundaries and destroy other data in memory.

10 The cin object  Multiple strings #include #include using namespace std; int main() { char first[21], last[21]; cout<<“What is your first and last name? ”; cin>>first>>last; cout<<“Good Morning ”<<last<<“,”<<first<<endl; return 0; }

11 The cin Object  Boolean data input bool aBool=true; bool aBool=true; cout<<“Please input a boolean data: ” cout<<“Please input a boolean data: ” cin>>aBool; cin>>aBool; cout <<“bool data is ”<<aBool<<endl; cout <<“bool data is ”<<aBool<<endl;

12 Mathematical Expressions  A math formula x(a+b) 3+6b x(a+b) 3+6b  Can create complex expressions using multiple mathematical operators  An expression is a programming statement that has a value. sum = 10+1; sum = 10+1;  can be a constant, a variable, or a mathematical combination of constants and variables  Can be used in assignment, cout, other statements.

13 Expressions  In assignment statement result = x; result = 4; result = 15/3; result = 22*number; result = sizeof(int); result = a+b+c;

14 Expression  In output statement //This program calculates Hui Liu's part-time pay #include using namespace std; void main() { float workPayRate; int workHour; int seeResults; cout << "How many hours do you work every work? "<<endl; cin >> workHour; cout<<"How much do you get paid per hour?"<<endl; cin>>workPayRate; //pay=workPayRate*workHour; cout<<"Hui Liu: You will get "<<(workPayRate*workHour) <<" dollars every work for the part time job!"<<endl; cin>>seeResults; }

15 Order of Operations In an expression with > 1 operator, evaluate in this order: - (unary negation), in order, left to right * / %, in order, left to right + -, in order, left to right In the expression 2 + 2 * 2 – 2, evaluate first evaluate second evaluate third

16 Associativity of Operators  - (unary negation) associates right to left  *, /, %, +, - associate left to right  parentheses ( ) can be used to override the order of operations: 2 + 2 * 2 – 2 = ? 2 + 2 * 2 – 2 = ? (2 + 2) * 2 – 2 = ? 2 + 2 * (2 – 2) = ? 2 + 2 * (2 – 2) = ? (2 + 2) * (2 – 2) = ?

17 Algebraic Expressions  Multiplication requires an operator: Area=lw is written as Area = l * w;  There is no exponentiation operator: Area=s 2 is written as Area = pow(s, 2);  Parentheses may be needed to maintain order of operations: is written as m = (y2-y1) /(x2-x1);

18 Library Functions  A collection of specialized functions.  A “library function” as a “routine” that performs a specific operation.  area = pow(4, 2)  Some Examples:

19 pow function //This program calculates the volume of a sphere #include using namespace std; void main() { float radius, volume,pi=3.14155926; cout << “What is the radius of the sphere? "<<endl; cin >> radius; volume= cout<<“The volume of the sphere is “ << <<volume<<endl; }

20 More math library functions  Require cmath header file  Take double as input, return a double  Commonly used functions: sin Sine cos Cosine tan Tangent sqrt Square root log Natural (e) log Log10 base-10 logarithm exp exponential function abs Absolute value (takes and returns an int) fmod remainder

21 Random number generator  #include  #include  y = rand();  Psuedorandom For example: cout<<rand()<<endl;cout<<rand()<<endl;cout<<rand()<<endl;  In order to randomize the results of rand()  srand (unsigned int)

22 rand() and srand() //This program demonstrates random numbers. #include using namespace std; void main() { unsigned int seed; cout << “Enter a seed value : "; cin >> seed; srand(seed); cout<<rand()<<endl; }

23 Simplified twenty one points game


Download ppt "Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy."

Similar presentations


Ads by Google