1 CS161 Introduction to Computer Science Topic #4.

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
1 String Library and Stream I/O Ying Wu Electrical Engineering & Computer Science Northwestern University ECE230 Lectures Series.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Computer Science 1620 Programming & Problem Solving.
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:
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
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.
CSE202: Lecture 8The Ohio State University1 Formatting Numbers for Output.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
CSE1222: Lecture 9The Ohio State University1. Formatting Numbers for Output  Number formatters are to be used in conjunction with cout  For example,
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
Writing Your First Programs Chapter 2 Mr. Dave Clausen La Cañada High School.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
CSC 270 – Survey of Programming Languages C++ Lecture 1 : C++ As A Better C.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
1 CS161 Introduction to Computer Science Topic #3.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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:
Chapter 3: Assignment, Formatting, and Interactive Input.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CPS120: Introduction to Computer Science Formatted I/O.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
Chapter 3: Input/Output
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
1 CS162 Introduction to Computer Science II Welcome ! CS162 Topic #1.
Program #2 Algorithm for Parking at PSU. Understanding the Assignment You will be writing a program to find out how much someone at PSU might be spending.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
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.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Chapter 05 (Part II) Control Statements: Part II.
Math Operators and Output Formatting. Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter +
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
C++ Basic Input and Output (I/O)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Output Stream Formatting
Chapter 2 part #3 C++ Input / Output
Formatting Screen Input/Output
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.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Let’s all Repeat Together
Chapter 2 part #3 C++ Input / Output
C++ for Engineers and Scientists Second Edition
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #4

CS161 Topic #42 Today in CS161 Assignments/Questions –What is needed for Program #2? –Using the Math Library –Input/Output and Formatting –Sample Algorithm

CS161 Topic #43 To Raise to Power of... In C++ there is no operator that will raise some number by another For example, for x 3 you can’t type: –x**3ILLEGAL! Instead, you must use the pow function float answer; answer = pow(x, 3);

CS161 Topic #44 To Raise to Power of... To use the power function, we must do two very important things... –include the math library: #include –compile our programs using a -lm flag g++ -lm prog2.cpp

CS161 Topic #45 Next, to Format our Output We must learn about precision By default, real numbers are displayed with no more than 6 digits, plus the decimal point This means that 6 significant digits are displayed in addition to the decimal point and a sign if the number is negative

CS161 Topic #46 Default Precision -- Examples float test; cout << “Please enter a real number”; cin >> test; cout << test; InputResulting Output

CS161 Topic #47 To Change Precision float test; cout << “Please enter a real number”; cout.precision(3);//3 instead of 6!! cin >> test;cout << test << endl; InputResulting Output e+04 (Exponential notation)

CS161 Topic #48 Another way to do this... #include float test; cout << “Please enter a real number”; cin >> test; cout <<setprecision(3) << test << endl; setprecision is a manipulator To use it, we must include the iomanip.h header file There is no difference between cout.precision(3)andcout <<setprecision(3)

CS161 Topic #49 What is “width”? The width of a field can be set with: cout.width(size); If what you are displaying cannot fit, a larger width is used –to prevent the loss of information Important –Width is only in effect for the next output

CS161 Topic #410 How does width work... float test; cout.precision(4);cout.width(10); cin >>test;cout << test; cout <<endl <<test; InputResulting Output

CS161 Topic #411 Another way to do this... #include float test; cout.precision(4); cin >>test; cout <<setw(10) << test; cout <<endl <<test; InputResulting Output

CS161 Topic #412 Trailing Zeros For real numbers, trailing zeros are discarded when displayed To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output (for an precision of 3 or greater)

CS161 Topic #413 Displaying Trailing Zeros float test; cout.precision(4); cout.setf(ios::showpoint); cin >>test;cout << test <<endl; cout.unsetf(ios::showpoint); //reset... cout <<test; InputResulting Output

CS161 Topic #414 Displaying Dollars and Cents! There is another meaning to precision... –if we put in our programs: cout.setf(ios::fixed,ios::floatfield); –then, subsequent precision applies to the number of digits after the decimal point! cout.precision(2);cout <<test; To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output

CS161 Topic #415 Displaying Dollars and Cents! Since we ALSO want trailing zero displayed...do all three: cout.setf(ios::fixed,ios::floatfield); cout.precision(2); cout.setf(ios::showpoint); cout <<test; To display trailing zeros we use: cout.setf(ios::showpoint); InputResulting Output

CS161 Topic #416 Now... the Algorithm... Your algorithm is not: –I sat down at the terminal –I got into the editor –I entered my program –I tried to compile it but got errors I DON”T WANT TO SEE THIS!!!!

CS161 Topic #417 Your Algorithm... First define the major tasks Then break down these into subtasks For example, the major tasks might be: –Welcome the user –Get the loan amount, interest rate, duration –Calculate the monthly payment –Display the results –Sign off Message Not Detailed Enough! But...a good start...