Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.

Slides:



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

Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++
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.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
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.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
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.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CPS120: Introduction to Computer Science Operations Lecture 9.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
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.
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
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.
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.
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.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
Chapter Topics The Basics of a C++ Program Data Types
CS102 Introduction to Computer Programming
Chapter 3 Assignment and Interactive Input.
Chapter 3. Expressions and Interactivity
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity
Starting Out with C++: From Control Structures through Objects
Expressions and Interactivity
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Standard Version of Starting Out with C++, 4th Edition
Lecture 3 Expressions, Type Conversion, and string
Presentation transcript:

Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

The cin Object The cout object’s counterpart for input is cin Standard console input object Prompt the user for input >> stream extraction operator (remember <<, the stream insertion operator Gather input is a two step process use cout object to display a prompt use cin object to read a value from the keyboard

// This program asks the user to enter the length and width of // a rectangle. It calculates the rectangle's area and displays // the value on the screen. #include using namespace std; int main() { int length, width, area; cout << "This program calculates the area of a "; cout << "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; }

Entering multiple values // This program asks the user to enter the length and width of // a rectangle. It calculates the rectangle's area and displays // the value on the screen. #include using namespace std; int main() { int length, width, area; cout << "This program calculates the area of a "; cout << "rectangle.\n"; cout << "Enter the length and width of the rectangle "; cout << "separated by a space.\n"; cin >> length >> width; area = length * width; cout << "The area of the rectangle is " << area << endl; return 0; }

Entering different data types // This program demonstrates how cin can read multiple values // of different data types. #include using namespace std; int main() { int whole; double fractional; char letter; cout << "Enter an integer, a double, and a character: "; cin >> whole >> fractional >> letter; cout << "Whole: " << whole << endl; cout << "Fractional: " << fractional << endl; cout << "Letter: " << letter << endl; return 0; }

Reading Strings cin can read more than one character and store it in memory as a character array, or C-string char company[12] Note: A larger string entered will overflow the array’s boundaries and destroy data in memory! Char Type Name Number indicates the size of the array

// 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; }

// 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; }

Mathematical Expressions C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols Expression: statement that has a value Example: sum = ; Num = 4; Result = x; Result = 15 / 3; Result = a + b + c;

// 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() { double 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) << endl; return 0; }

Operator Precedence PMDAMS! 1. Parentheses 2. Multiplication 3. Division 4. Addition 5. Modulus 6. Subtraction Examples * 4 = ? 10 / 2 -3 = ? * 2 – 4 = ? % 2 – 1 = ? 6 – 3 * – 1 = ?

Associativity and Grouping Either left to right or right to left If two operators sharing an operand have the same precedence, they work according to their associativity * / % + - (left to right) Unary negation – (right to left) You can use () to group operations and force their precedence Result = (a + b) / 4;

Converting Algebraic Expressions to Programming Statements Examples 6B → 6 * B (3)(12) → 3 * 12 4xy → 4 * x * y; Insert parentheses where necessary → x = (a + b) / c; Using exponents → y = pow(x,2); This statement calls pow, with arguments x and 2 and the result of the power is returned

// 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 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.0); cout << "The area is " << area << endl; return 0; }

When you mix apples and oranges: Type Conversion When an operator’s operands are different data types, C++ will automatically convert to the same data type, affecting the results Depends on Ranking and Rules

Rules 1. chars, shorts, and unsigned shorts are automatically converted to int 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 3. when the final value of an expression is assigned to a variable, it will be converted to the data type of the variable

Overflow and Underflow When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows

// This program demonstrates integer overflow and underflow. #include using namespace std; int main() { // testVar is initialized with the maximum value for a short. short testVar = 32767; // Display testVar. cout << testVar << endl; // Add 1 to testVar to make it overflow. testVar = testVar + 1; cout << testVar << endl; // Subtract 1 from testVar to make it underflow. testVar = testVar - 1; cout << testVar << endl; return 0; }

// This program can be used to see how your system handles // floating point overflow and underflow. #include using namespace std; int main() { float test; test = 2.0e38 * 1000; // Should overflow test. cout << test << endl; test = 2.0e-38 / 2.0e38; // Should underflow test. cout << test << endl; return 0; }

Type Casting Type casting allows for manual data type conversion General format static_cast (Value) Example Double number = 3.7; int val; val = static_cast (number);

// This program uses a type cast to avoid integer division. #include using namespace std; int main() { int books; // Number of books to read int months; // Number of months spent reading double perMonth; // Average number of books per month 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 = static_cast (books) / months; cout << "That is " << perMonth << " books per month.\n"; return 0; }

// This program uses a type cast expression to print a character // from a number. #include using namespace std; int main() { int number = 65; // Display the value of the number variable. cout << number << endl; // Display the value of number converted to // the char data type. cout (number) << endl; return 0; }

Named Constants Literals may be given names that symbolically represent them in a program Example: PI const double PI = ;

// 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 pow function using namespace std; int main() { const double 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.0); cout << "The area is " << area << endl; return 0; }

Multiple Assignment and Combined Assignment

Formatting Output

Formatted Input

Introduction to File Input and Output

More Math Library Functions

Problem Solving: Case Study