C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
CS150 Introduction to Computer Science 1
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
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:
C++ Numerical Data Input/Output Programming. COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2 Rules for Division l C++ treats integers.
The If/Else Statement, Boolean Flags, and Menus Page 180
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 1 Quiz Questions (CGS-3464) Mahendra Kumar
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSE 100 s s Input: cin s type casting Math Library Functions math.h sqrt(n) fabs(n) cos(n) log10(n) log(n) pow(b, n) etc. * * *
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1.  By the end of this section you should: ◦ Understand what the variables are and why they are used. ◦ Use C++ built in data types to create program.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
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.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming.
Arithmetic You can perform arithmetic with numbers and/or variables. Java follows mathematical order of operations (PEMDAS). Example: / 2  this.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Example 21 #include<iostream.h> int main() { char Letter = 0;
Variables Mr. Crone.
Introduction to C++ October 2, 2017.
C++ Arrays.
Chapter 2 Elementary Programming
Lecture 3 Expressions Richard Gesick.
הרצאה 03 אבני היסוד של תוכנית ב- C
Counting Loops.
Arithmetic Operations
Fundamental Programming
Other types of variables
Data Types and Maths Programming Guides.
Programming Fundamental-1
Data Types and Arithmetic in C
Presentation transcript:

C++ Typecasting. Math Library Functions.

Operator / Operands A = x + y

Typecasting  What is the integral expression?  What is a decimal expression?  What is mixed expression?  What will be the result of those expression?

Typecasting  What is typecasting?  What are the two types of typecasting?  Examples…

Implicit Typecasting 3.4 / 2 = 3.4 / 2.0 = 1.7

Explicit Typecasting int x=3, y=2; double z; z = x/y; //integer division, z=1.0 z = double(x)/y; // z=1.5 What is the datatype of x after this operation?

Explicit Typecasting double(x) is equivalent to (double)x is equivalent to static_cast x

int number; cout << “Enter a number”; //User enters 10 cin >> number; What is stored in variable “number”? 10

int number; cout << “Enter a number”;//User enters 10.3 cin >> number; What is stored in variable “number”? 10

float number; cout << “Enter a number”; //User enters 10.3 cin >> number; What is stored in variable “number”? 10.3

float number; cout << “Enter a number”; //User enters 10 cin >> number; What is stored in variable “number”? 10.0

int num1=3; int num2=5; int product; product = num1 * num2; What is stored in variable “product”? 15

float num1=3.1; int num2=5; int product; product = num1 * num2; What is stored in variable “product”? 15

float num1=3.1; int num2=5; float product; product = num1 * num2; What is stored in variable “product”? 15.5

int num1=3; int num2=5; float product; product = num1 * num2; What is stored in variable “product”? 15.0

Integer Division ( / ) int/int  int 10/3 = 30/5 = 5/6 = 9/8 = 22/5 =

Mod Operator ( %) int % int = int 10%3 = 30%5 = 5%6 = 9%8 = 22%5 =

int num1= 13; int num2= 4; int quotient; quotient = num1 /num2; What is stored in variable “quotient”? 3

int num1= 13; int num2= 4; float quotient; quotient = num1 /num2; What is stored in variable “quotient”? 3.0

int num1=13; int num2=4; float quotient; quotient = float(num1) /num2; What is stored in variable “quotient”? What is stored in variable “num1”?

int num1=13; int num2=4; float quotient; quotient = float(num1/num2); What is stored in variable “quotient”? 3.0

Character Typecasting char letter = ‘a’; cout<< int(letter); // What is displayed? int x=98; cout<<char(x); //What is displayed? 97 b

Character Typecasting char letter='a'; cout<< int(letter) << endl; letter= letter + 1; cout<< letter << endl; cout<< letter + 1 << endl; cout<< char (letter+1) << endl; cout<< int(letter) << endl; 97 b 99 c 98

Find an average of three numbers: int x, y, z; float average; average=(x+y+z)/3; average=(float(x)+y+z)/3.0; average=(x+y+z)/3.0;