CSE 100 <math.h> Input: cin type casting.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Computer Programming w/ Eng. Applications
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
Chapter 3 Assignment and Interactive Input
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Data Types, Expressions and Functions (part I)
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
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. * * *
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
M.T.Stanhope Oct Title : C++ Basics Bolton Institute - Faculty of Technology (Engineering) 1 C++ Basics u Data types. u Variables and Constants.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 3, Part 2 s Input: cin s Example programs.
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Chapter 3: Assignment, Formatting, and Interactive Input.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 3.1 & 3.2 s Programming s Assignment Statements s Incrementing & Decrementing s Math Library Functions.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Recap……Last Time [Variables, Data Types and Constants]
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
C++ Data Types Check sample values of? ‘4’
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
arithmetic operator & cin I.Mona Alshehri The output formatting functions setw(width) setw(n) - output the value of the next expression in n columns.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
Chapter 3 Assignment and Interactive Input.
Programming Fundamentals
Computing Fundamentals
Chapter 3. Expressions and Interactivity
Chapter 2 Assignment and Interactive Input
BASIC ELEMENTS OF A COMPUTER PROGRAM
Enum ,Char Functions& Math Library Functions I.Mona Alshehri
Chapter 2 Elementary Programming
Expressions and Interactivity
Starting Out with C++: From Control Structures through Objects
A First Book of ANSI C Fourth Edition
Screen output // Definition and use of variables
Chapter 3: Input/Output
Wednesday 09/23/13.
Chapter 3 Input output.
Common Programming Errors Assignment Statements
Elementary Programming (C++)
C++ Programming Basics
C++ for Engineers and Scientists Second Edition
Basic Elements of Computer Program
Chapter 3 The New Math.
Presentation transcript:

CSE 100 <math.h> Input: cin type casting

Math Library Functions math.h sqrt(n) fabs(n) cos(n) log10(n) log(n) pow(b, n) etc. * * *

Math Library Functions name of the function what it does data type of argument data type of returned value

Math Library Functions Ex. sqrt(49) pow(2.1, 3) abs(-34.5) cos(30) abs(34.5) Syntax: function_name (argument); *

Math Library Functions sqrt( pow ( fabs (-4), 3) ) = sqrt( pow ( 4.0 , 3) ) = sqrt( 64.0 ) = 8.0 nested functions * * *

Type Casting The explicit conversion of a value from one data type to another. Syntax: data_type (expression) int (5.34 * 1.68) int (8.9712) This returns a value of 8. * *

Type Casting someInt = someDouble - 8.2; someInt = int(someDouble - 8.2); These are identical statements. * *

Type Coercion The implicit (automatic) conversion of a value from one data type to another. someDouble = 42; is stored as 42.0 someInt = 11.9; is stored as 11 *

Math Function Example #include <math.h> // needed for the exp function int year; long int population; year = 1995; population = 5.5 * exp(0.02*(year-1990)); cout << "The estimated population for " << year << " is "<<population << "."; year = 2012; cout << "The estimated population for " << year << " is "<< population <<".";

Math Function Example 5.5 e .02(year-1900) 5.5 * exp(0.02*(year-1990))

Math Function Example Output: The estimated population for 1995 is 6.

cin cin >> my_num; The keyboard entry is stored into a local variable called my_num. Read as: “get from my_num”.

cin Syntax: cin >> var1; Examples: cin >> last_name; cin >> ch; cin >> my_id#; *

Syntax: cin >> var1 >> var2 >> ... cin (concatenation) Syntax: cin >> var1 >> var2 >> ... cin >> first >> last >> my_num; cin >> qz1 >> qz2 >> qz3 >> qz4; *

cin & cout main() { cout << cin >> } standard device standard device (keyboard) (screen)

cin & cout cout cin << >> insertion extraction << >> insertion extraction “put to” “get from” whitespace characters ignored

cin Example 1 int num1, num2, num3; double average; cout << "Enter three integer numbers: "; cin >> num1 >> num2 >> num3; average = (num1 + num2 + num3) / 3.0; cout << "The average is " << average; cout << '\n';

cin Example 1 Output: 3 5 5 The average is 4.33333

cin Example 2 double radius, circumference; double pi = 3.1416; cout << "Enter the radius of a circle: "; cin >> radius; circumference = 2 * pi * radius; cout << "The circumference of a circle of radius " << radius << " is " << circumference <<‘\n’;

cin Example 2 Output: Enter the radius of a circle: 14 The circum ... circle of radius 14 is 87.9648

cin Example 3 double celsius, faren; cout <<"Enter the temperature in degrees Fahrenheit: "; cin >> faren; celsius = 5.0/9.0 * (faren - 32.0); cout << faren <<" degrees Fahrenheit equals "<< celsius << " degrees celsius.\n";

cin Example 3 Output: Enter the temperature in degrees Farenheit: 82 82 degrees Farenheit equals 27.7777 degrees celsius.

const Qualifier Syntax: const DataType Name = Literal Value; Examples const double PI = 3.1416; const double OT_RATE = 1.5; *

const Qualifier double radius, area; const double PI = 3.1416 cout << “Enter the radius: “; cin >> radius; cout << “The area is your circle is “ << PI * radius * radius; *

Common Programming Errors not initializing variables before use forgetting >> to separate variables in cin applying ++ or -- to an expression (x-y)++

Why isn’t phonetic spelled as it sounds? Why do they put Braille dots on the keypad of a drive-up ATM? How many Microsoft technicians does it take to change a light bulb? Three: two holding the ladder and one to screw the bulb into a faucet. “Lack of brains hinders research” The Columbus Dispatch, 16 April 1996