1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.

Slides:



Advertisements
Similar presentations
More Review, with Some New Chapter 3. Review C++ simple data types – Integral and float – Arithmetic operators Expressions and expression evaluation –
Advertisements

CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
1 Chapter 3 Topics Constants of Type int and float l Evaluating Arithmetic Expressions l Implicit Type Coercion and Explicit Type Conversion l Calling.
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.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 Lecture 7 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
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. * * *
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
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.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Chapter 3 Arithmetic Expressions, Function Calls, and Output
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
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: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
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.
Expressions and Interactivity. 3.1 The cin Object.
Formatting Output  Escape Sequences  iomanip.h Objects  setw()  setiosflags(…)  setprecision()
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
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.
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Lecture 3 Expressions, Type Conversion, Math and String
Formatting Output iomanip.h Objects Escape Sequences setw()
C++ Basic Input and Output (I/O)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Chapter 3. Expressions and Interactivity
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity
A First Book of ANSI C Fourth Edition
CSE 100 Input: cin type casting.
C++ for Engineers and Scientists Second Edition
Programming Fundamental-1
Presentation transcript:

1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting

2 Formatting Output Escape Sequences \t, \n +others iomanip.h  setw()  setiosflags(…)  setprecision()

3 Escape Sequences A simple table cout << “Name\tTotal\tGrade\n”; cout << “Miyo\t186\t B\n”; cout << “\n Jake\t211\t A\n”; cout << “Syd\t203\t A\n”; OUTPUT NameTotalGrade Miyo186 B Jake211 A Syd203 A _ \n gives a blank line

4 Escape Sequences \ Changes the meaning of the character that follows it. \” means take quotes literally cout << “Al \”Scarface\” Capone”; displays Al ”Scarface” Capone

5 Escape Sequence Combinations cout << “\n Al\n\”Scarface\”\nCapone” displays Al “Scarface” Capone

6 Formatting Output iomanip.h contains the objects which have special effects on the iostream. * #include #include

7 setw(n) how many columns for data setprecision(n) sets number of decimals setiosflags(ios::fixed) displays 6 digits after the decimal Formatting Output iomanip.h contains the objects which have special effects on the iostream...

8 Formatting Output setw(n)how many columns for data (ans = 33, num = 7132) cout << setw(4) << ans cout << setw(1) << ans << setw(5) << num << setw(3) << num << setw(4) << “Hi”; << setw(3) << “Hi”; fields expand  33  7132  Hi  Hi

9 Formatting Output setprecision(n)sets number of decimals x format result 314.0setw(10) setprecision(2)  point counts 314.0setw(10) setprecision(5)  columns added 314.0setw(7) setprecision(5)

10 Formatting Output example #include int main() { double v = ; double w = ; double x = ; double y = ; double z = ; cout <<v<< “\n”<<w<< “\n”<<x<< “\n”<<y<< “\n”<<z<<”\n\n”; cout << setiosflags(ios::fixed); cout <<v<< “\n”<<w<< “\n”<<x<< “\n”<<y<< “\n”<<z<<”\n\n”; cout << setprecision(2); cout <<v<< “\n”<<w<< “\n”<<x<< “\n”<<y<< “\n”<<z<<”\n\n”; return 0; } setiosflags(ios::fixed) displays 6 digits after the decimal

Formatting Output default ios::fixed setprecision(2)

Formatting Output For decimal alignment use: sitcky ios::fixed and setprecision( ) sitcky needed each time setw( )needed each time cout << setiosflags(ios::fixed) << setprecision(n);  cout << “- -” << setw(n) << var1 << setw(n) << “- -”; cout << “- -\t” << setw(n) << var1 << setw(n) <<var2;

13 Overflow and underflow Overflow means that the r esult is too large to be represented in an objects type. Underflow Result is too small to be represented by object. Demonstrate with example Chris using debugger.

14 #include int main() { int x = 1000; cout << x << endl; x = x*x; cout << x << endl; x = x*x; cout << x << endl; x = x*x; cout << x << endl; x = x*x; cout << x << endl; x = x*x; return 0; } Example of integer overflow

15 #include int main() { float x = 1.0; cout << x << endl; x = x/1e10; cout << x << endl; x = x/1e10; cout << x << endl; x = x/1e10; cout << x << endl; x = x/1e10; cout << x << endl; x = x/1e10; cout << x << endl; return 0; } Example of integer underflow

16 Math Library Functions Pre-written functions for math's Portability Needs #include FunctionName(param list) You need to know What their identifiers (names) are? What is their purpose? Power, sqrt, sin, cos, exp etc Data type of return value and parameters How they blow up? Use help F1 for usage guide

17 double sqrt(double n) Calculates and returns square root of n. Warning! n must be positive when in doubt use with fabs(n) -> sqrt(fabs(n)); Can use any number data (will promote) Normal usage : double question = 45.35, double answer; answer = sqrt( question ); :

18 double pow(double n, double b) Calculates and returns n to the power b. Warning! Can overflow or underflow! Can blow up (if n = 0.0 and b < 0) Can use any number data (will promote) Normal usage : double question = 3.0, double answer; answer = pow(question, 4.0); //raise to power 4 :

19 double fabs(double n) Calculates the absolute value of the floating-point argument. Will promote integers Normal usage : double question = -3.0, double answer; answer = fabs(question ); //answer = 3.0 :

20 Trigonometric functions double cos(double n), double sin(double n), double tan(double n) n is in radians 360 degrees equals 2  radians Need to convert angles 90 degrees =  /2 radians 45 degrees =  /4 radians n degrees = n*  /180 radians

21 Inverse Trigonometric functions double acos(double n) double asin(double n) double atan(double n) n is strictly between –1 and 1 Returns angle in radians If you need to convert angles to degrees  radians = 180 degrees  /2 radians = 90 degrees n radians = n*180/  degrees

22 double log10(double n) double log(double n) double exp(double n) log10(n) returns the logarithm to base 10 Log10(20) = Pow(10,1.3013) = 20 log(1) = 0; natural logarithm (statistics, radioactive decay population) exp(0) = 1; inverse (e x where e = )

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

24 Math Function Example Mathematical notation 5.5 e.02(year-1900) C++ notation 5.5 * exp(0.02*(year-1990))

25 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 *

26 Type Casting Automatic typecasts mentioned before In arithmetic expressions In passing parameters to library functions Can be made explicit Stop some of those nagging warning messages To deliberately remove fractional information Syntax:data_type (expression) int (5.34 * 1.68) int (8.9712) This returns a value of 8.

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

28 Pitfalls of getting information into the computer cin >> my_num; The keyboard entry is stored into variable called my_num.

29 cin chains Syntax: cin >> var1 >> var2 >>... White space or illegible character is used as a terminator. White space can be an newline. Input continues from point of termination cin >> first >> last >> my_num;

30 cin chain example 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'; Needed to force floating Point calculation

31 cin Example Output: The average is OR Output: 3 5 The average is

32 cin Example 2 automatic promotion double radius, circumference; const double pi = ; 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’;

33 cin promotion Automatic promotion Output: Enter the radius of a circle: 14 The circum... circle of radius 14 is

34 cin cautionary example int num1, double num2 num3; cout <<"Enter three numbers: "; cin >> num1 >> num2 >> num3; cout << num1 << ‘\t’<< num2 << ‘\t’ << num3 << endl;

35 cin Example 3 Output: Enter three numbers : is still waiting to be processed! Do next example Chris

36 #include int main() { int i=1; int num1; float num2, num3, num4; cout << "enter three numbers : "; cin >> num1 >> num2 >> num3; cout << num1 << '\t' << num2 << '\t'<< num3 << endl; cin >> num4; cout << num4 << endl; return 0; }

37 Measures of program quality Minimum criteria Program should work. Besides simply working, a program quality can be measured by how whether it is, Clear Robust Efficient Programming in the large Reusable Extensible

38 Clarity From programmers point of view (good documentation) From a users point of view, program clearly identifies what the inputs are, and what exactly the outputs are.

39 Robustness Program keeps working even with incorrect data.

40 Efficiency The program produces its result in a time efficient manner. The program produces its result in a memory efficient manner

41 Programming in the large Can the program be written using teams of programmers?

42 Extensibility The program is easy to modify and extend functionality

43 Reusability It is easy to reuse existing code, both within the current project or for a new project.

44 Procedural vs. OO programming Procedural programming capable of Clarity Robustness Efficiency Programming in the large Less well suited for Extensibility Reusability OO programming designed with Extensibility and Reusability in mind

45 Learning Object Oriented Technologies Can take many years to learn all. We will be covering first stage on Object oriented programming if we have time.