1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page 85-87 & Section 3.8.

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Computer Science 1620 Loops.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
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.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Computer Science 1620 Formatting. Suppose you work for the HR dept. of a company you wish to write a program to show their earnings per month Details:
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.
Classes and Objects Objects of class type string Input/Output Objects and Formatted Input/Output 6/30/2015MET CS 563--Fall A. Using Class Type Objects.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
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
© Janice Regan, CMPT 128, Sept CMPT 128: Introduction to Computing Science for Engineering Students C++ Basic Input and output.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
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:
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
1 CS161 Introduction to Computer Science Topic #4.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
Chapter 3 L7.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Starting Out with C++: From Control Structures through Objects
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
Functions Divide and Conquer
Let’s all Repeat Together
Arithmetic Operations
Output Formatting Bina Ramamurthy 4/16/2019 BR.
Reading from and Writing to Files
Functions Divide and Conquer
Reading from and Writing to Files Part 2
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
Output Formatting Bina Ramamurthy 9/25/2019 BR.
Presentation transcript:

1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8

2 9/26/07CS150 Introduction to Computer Science 1 Exponents (page )  The exponent operator was missing from the list!x 2 y n  C++ does not provide an exponent operator as part of the language  Use pow() in the cmath library #include double area; area = pow(4, 2); // area = 4 2

3 9/26/07CS150 Introduction to Computer Science 1 pow()  pow() is not an operator o it is a function o like main() o double pow(double x, double y) o it takes as arguments two doubles  x and y o it produces a double

4 9/26/07CS150 Introduction to Computer Science 1 Practice using exponents! // Calculate the area of a square double lengthOfSide = 4.9; // Calculate the volume of a cube

5 9/26/07CS150 Introduction to Computer Science 1 Advanced Output Section 3.8  How can we force output to look a particular way? o Precision of numbers o Spacing around output Here are some floating point numbers: Here is a table of data: 4 cat

6 9/26/07CS150 Introduction to Computer Science 1 Precision double number = ; cout << number << endl; // default output  What does this output?  Precision cout << setprecision(2) << number; Output:

7 9/26/07CS150 Introduction to Computer Science 1 Precision  Precision can also be used to set the number of digits after the decimal point  Output: double number = ; cout << fixed << setprecision(2) << number;

8 9/26/07CS150 Introduction to Computer Science 1 Precision of numbers #include #include //New Library! using namespace std; int main() { double number = ; cout << number << endl; // default output cout << fixed << setprecision(4) << number << endl; cout << fixed << setprecision(3) << number << endl; cout << fixed << setprecision(2) << number << endl; cout << fixed << setprecision(1) << number << endl; return 0; } These numbers are rounded! Explore on your own what happens if number is an integer.

9 9/26/07CS150 Introduction to Computer Science 1 Precision  Precision and fixed are sticky o remains in effect until changed double number = ; cout << fixed << setprecision(4) << number << endl; cout << setprecision(2) << number << endl; cout << number << endl; // Output?

10 9/26/07CS150 Introduction to Computer Science 1 double  a double has a range of: o ±1.7E-308 to ±1.7E308 o however, only tracks 16 significant digits double bignumber = ; cout << fixed << setprecision(20); cout << bignumber <<endl; bignumber = ; cout << bignumber <<endl;  Output:

11 9/26/07CS150 Introduction to Computer Science 1 Spacing  How can we output data in a table? cs house dog string name = “cs150”; int integer = 42; cout << setw(6) << name;

12 9/26/07CS150 Introduction to Computer Science 1 Spacing around output #include #include //New Library! #include using namespace std; int main() { double number = ; string name = “cs150”; int integer = 42; cout << setw(6) << name << setw(6) << integer << endl; cout << setw(6) << fixed << setprecision(3) << number; cout << setw(4) << integer <<endl; return 0; } cs A represents a blank space

13 9/26/07CS150 Introduction to Computer Science 1 Setw  Setw is not sticky o you must specify it every time double number = ; int integer = 42; cout << setw(6) << integer << integer << endl; cout << integer <<endl; //output?

14 9/26/07CS150 Introduction to Computer Science 1 Practice  Using the variables below, create the output shown: double number = ; string name = “cs150”; string animal = “cat”; string cover = “hat”; int integer = 42; cat hatcs A represents a blank space

15 9/26/07CS150 Introduction to Computer Science 1 Practice  Write a program segment that allows the user to input two integer values into variables num1 and num2. Display both numbers as shown below, always displaying the smaller number first. Please enter two numbers: The numbers are: 9 100