C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.

Slides:



Advertisements
Similar presentations
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 Objectives Understand streamed input and output.
Starting Out with C++, 3 rd Edition 1 Chapter 3. Expressions and Interactivity.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Chapter 3 Expressions and Interactivity. 2 Topics 3.1 The cin Object 3.2 Mathematical Expressions 3.3 When You Mix Apples and Oranges: Type Conversion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Chapter 7 Arrays C++ Programming, Namiq Sultan1 Namiq Sultan University of Duhok Department of Electrical and Computer Engineering Reference: Starting.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
1 CS102 Introduction to Computer Programming Week 3 Chapter 3 Expressions and Interactivity.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Expressions & Interactivity
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
CPS120: Introduction to Computer Science Operations Lecture 9.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Recap……Last Time [Variables, Data Types and Constants]
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Lecture 3 Expressions, Type Conversion, Math and String
Chapter Topics The Basics of a C++ Program Data Types
CS102 Introduction to Computer Programming
INSPIRING CREATIVE AND INNOVATIVE MINDS
Chapter 3. Expressions and Interactivity
Basic Elements of C++.
Type Conversion, Constants, and the String Object
Basic Elements of C++ Chapter 2.
Expressions and Interactivity
Starting Out with C++: From Control Structures through Objects
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference: Starting Out with C++, Tony Gaddis, 2 nd Ed.

C++ Programming, Namiq Sultan2 3.1The cin Object The cin object reads information types at the keyboard. cin is the standard input object Notice the >> and << operators appear to point in the direction information is flowing.

C++ Programming, Namiq Sultan3 Program 3-1 #include using namespace std; int main() { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"What is the length of the rectangle? "; cin>>length; cout <<"What is the width of the rectangle? "; cin>>width; area = length * width; cout <<"The area of the rectangle is " << area << ".\n"; return 0; }

C++ Programming, Namiq Sultan4 Program Output This program calculates the area of a rectangle. What is the length of the rectangle? 10 [Enter] What is the width of the rectangle? 20 [Enter] The area of the rectangle is 200.

C++ Programming, Namiq Sultan5 Entering Multiple Values The cin object may be used to gather multiple values at once. cin >> length>> width;

C++ Programming, Namiq Sultan6 Reading Strings cin can read strings as well as numbers. Strings are stored in character arrays. char Company[12];

C++ Programming, Namiq Sultan7 Program 3-5 // This program demonstrates how cin can read a // string into a character array #include using namespace std; int main() { char name[21]; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; return 0; }

C++ Programming, Namiq Sultan8 Program Output What is your name? Charlie [Enter] Good morning Charlie

C++ Programming, Namiq Sultan9 Program 3-6 //This program reads two strings into two character arrays. #include using namespace std; int main() { char first[16], last[16]; cout << "Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; return 0; }

C++ Programming, Namiq Sultan10 Program Output Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny

C++ Programming, Namiq Sultan11 Notes on strings: If a character array is intended to hold strings, it must be at least one character larger than the largest string that will be stored in it. 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 information in memory. If you wish the user to enter a string that has spaces in it, you cannot use this input method.

C++ Programming, Namiq Sultan Mathematical Expressions C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.

C++ Programming, Namiq Sultan13 Program 3-7 // This program asks the user to enter the numerator // and denominator of a fraction and it displays the decimal value #include using namespace std; int main() { float numerator, denominator; cout << "This program shows the decimal value of "; cout << "a fraction.\n"; cout << “Enter the numerator: “; cin >> numerator; cout << “Enter the denominator: “; cin >> denominator; cout << “The decimal value is “; cout << (numerator / denominator); return 0; }

C++ Programming, Namiq Sultan14 Program Output for Program 3-8 with Example Input This program shows the decimal value of a fraction. Enter the numerator: 3 [Enter] Enter the denominator: 6 [Enter] The decimal value is

C++ Programming, Namiq Sultan15 Table 3-1 Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) - * / % + -

C++ Programming, Namiq Sultan16 Table 3-2 Some Expressions

C++ Programming, Namiq Sultan17 Associativity If two operators sharing an operand have the same precedence, they work according to their associativity, either right to left or left to right

C++ Programming, Namiq Sultan18

C++ Programming, Namiq Sultan19 Converting Algebraic Expressions to Programming Statements

C++ Programming, Namiq Sultan20 No Exponents Please! C++ does not have an exponent operator. Use the pow() library function to raise a number to a power. Will need #include for pow() function. area = pow(4,2) // will store 4 2 in area

C++ Programming, Namiq Sultan21 Program 3-8 // This program calculates the area of a circle. The formula for the area // of a circle is Pi times the radius squared. Pi is #include #include // needed for the pow function using namespace std; int main() { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = * pow(radius,2); cout << "The area is " << area; return 0; }

C++ Programming, Namiq Sultan22 Program 3-8 Output With Example Input This program calculates the area of a circle. What is the radius of the circle? 10[Enter] The area is

C++ Programming, Namiq Sultan Type Conversion When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.

C++ Programming, Namiq Sultan24 Type Conversion Rules: Rule 1: chars, shorts, and unsigned shorts are automatically promoted to int. Rule 2: When an operator works with two values of different data types, the lower-ranking value is promoted to the type of the higher-ranking value. Rule 3: When the final value of an expression is assigned to a variable, it will be converted to the data type of the variable.

C++ Programming, Namiq Sultan The Typecast Operator The typecast operator allows you to perform manual data type conversion. Val = int(number); //If number is a floating //point variable, it will be //truncated to an integer and //stored in the variable Val

C++ Programming, Namiq Sultan26 Program 3-11 #include using namespace std; int main() { int months, books; float perMonth; cout << "How many books do you plan to read? "; cin >> books; cout << "How many months will it take you to read them? "; cin >> months; perMonth = float(books) / months; cout << "That is " << perMonth << " books per month.\n"; return 0; }

C++ Programming, Namiq Sultan27 Program Output How many books do you plan to read? 30 [Enter] How many months will it take you to read them? 7 [Enter] That is books per month.

C++ Programming, Namiq Sultan28 Typecast Warnings In Program 3-11, the following statement would still have resulted in integer division: perMonth = float(books / months); Because the division is performed first and the result is cast to a float.

C++ Programming, Namiq Sultan29 Program 3-12 // This program uses a typecast operator to print // a character from a number. #include using namespace std; int main() { int number = 65; cout << number << endl; cout << char(number) << endl; return 0; }

C++ Programming, Namiq Sultan30 Program Output 65 A

C++ Programming, Namiq Sultan The Power of Constants Constants may be given names that symbolically represent them in a program.

C++ Programming, Namiq Sultan32 Program 3-13 // This program calculates the area of a circle. #include using namespace std; int main() { const float pi = ; double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = pi * pow(radius,2); cout << "The area is " << area; return 0; }

C++ Programming, Namiq Sultan33 The #define Directive The older C-style method of creating named constants is with the #define directive, although it is preferable to use the const modifier. #define PI is roughly the same as const float PI= ;

C++ Programming, Namiq Sultan34 Program 3-14 #include #include // needed for pow function using namespace std; #define PI int main() { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * pow(radius, 2); cout << "The area is " << area; return 0; }

C++ Programming, Namiq Sultan Multiple Assignment and Combined Assignment Multiple assignment means to assign the same value to several variables with one statement. A = B = C = D = 12; Store1 = Store2 = Store3 = BegInv;

C++ Programming, Namiq Sultan36 Combined Assignment

C++ Programming, Namiq Sultan More Mathematical Library Functions

C++ Programming, Namiq Sultan38 Table 3-14

C++ Programming, Namiq Sultan39 Table 3-14 continued

C++ Programming, Namiq Sultan40 Table 3-14 continued

41 Program 3-32 // This program asks for the lengths of the 2 sides of a right triangle. // The length of the hypotenuse is then calculated and displayed. #include #include // For sqrt and pow using namespace std; int main() { float a, b, c; cout << "Enter the length of side A: "; cin >> a; cout << "Enter the length of side B: "; cin >> b; c = sqrt(pow(a, 2.0) + pow(b, 2.0)); cout << "The length of the hypotenuse is "; cout << c << endl; return 0; }

C++ Programming, Namiq Sultan42 Program Output Enter the length of side A: 5.0 [Enter] Enter the length of side B: 12.0 [Enter] The length of the hypotenuse is 13