EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Advertisements

1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
Lecture 18:Topic: I/O Formatting and Files Dale/Weems/Headington
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 3: Input/Output.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
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:
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Chapter 3: Input/Output
Expressions and Interactivity. 3.1 The cin Object.
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.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Week 13 - Friday.  What did we talk about last time?  Server communications on a socket  Function pointers.
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.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Lecture 3 Expressions, Type Conversion, Math and String
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
C++ Basic Input and Output (I/O)
What Actions Do We Have Part 1
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
Chapter 3: Expressions and Interactivity.
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
Expressions and Interactivity
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
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
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
Formatted Input, Output & File Input, Output
EECE.2160 ECE Application Programming
What Actions Do We Have Part 1
EECE.2160 ECE Application Programming
Chapter 2 part #3 C++ Input / Output
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Algorithmic complexity
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
EECE.2160 ECE Application Programming
Lecture 3 Expressions, Type Conversion, and string
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programming Fundamental-1
Presentation transcript:

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 5: Output manipulators Other input functions

Announcements/reminders Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Additional instructor support for course Grader: Shubham Tikare Office hours: Tues 9-11 AM, Ball 301E (ECE Conf Rm) Tutor: Felipe Loera Hours available on CLASS site (link also on home page) 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Lecture outline Program 1 basics Review Functions in C++ Output manipulators Other input methods Reading space character(s) Ignoring single character 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Program 1 basics Refresher on structures Gain familiarity with basic C++ I/O Will use two structures Point: simple point in 2D plane Polygon: collection of Point structures + Number of points Bounding box (min/max X & Y values) (optional) Will write functions to Read contents of Point Add Points to a Polygon Display Point and Polygon structures 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Review: Functions All examples below are function prototypes Contain information about how to call function Return type, name, and argument list Only arg types required, but good practice to list names No details on operation of function (definition) int f1(); double f2(int x, int y); void f3(int *p1, int *p2); void f4(int &r1, int &r2); f3() arguments passed by address Explicit pointer—call requires addresses: f3(&x, &y); f4()arguments passed by reference Aliases—call does not require addresses: f4(x, y); f4() does have ability to modify input arguments 6/1/2019 Data Structures: Lecture 5

Review: Function example #include <iostream> using namespace std; double f1(int v1, int v2); void f2(int *ptr1, int *ptr2); void f3(int &ref1, int &ref2); int main() { int foo = 10; int bar = 57; double baz; baz = f1(foo, bar); cout << "After f1(), foo = " << foo << ", bar = " << bar << ", baz = " << baz << "\n"; f2(&foo, &bar); cout << "After f2(), foo = " << foo << ", bar = " << bar << "\n"; f3(foo, bar); cout << "After f3(), foo = " << foo return 0; } double f1(int v1, int v2) { return (v1 + v2) / 2.0; } void f2(int *ptr1, int *ptr2) { while (*ptr1 > 5) { *ptr2 -= 3; (*ptr1)--; void f3(int &ref1, int &ref2) { if (ref1 == 5 && ref2 >= 45) { ref1++; ref2--; else if (ref1 == 5) { ref1--; ref2++; else { ref1 = ref2 - 10; ref2 = ref1 + 10; 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Example output After f1(), foo = 10, bar = 57, baz = 33.5 After f2(), foo = 5, bar = 42 After f3(), foo = 4, bar = 43 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Formatted output Recall earlier example: int i, j; double x, y; ... cin >> x >> y >> i >> j; // Assume input 2 3 4 5 cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; x and y are of type double … ... but second cout prints 2 & 3 for x & y What if we want to Always print decimal point? Always show certain number of places after point? Use stream manipulators: objects affecting output stream Already seen one of these: endl To use others, must add #include <iomanip> May also use stream functions: functions associated with cin/cout 6/1/2019 Data Structures: Lecture 5

FP Precision (setprecision) Precision of floating-point numbers Number of digits displayed to the right of the decimal point setprecision parameterized stream manipulator Precision settings are sticky Do not change until you’ve explicitly changed them 6/1/2019 Data Structures: Lecture 5

Example: setprecision #include <iostream> using std::cout; using std::endl; using std::fixed; #include <iomanip> using std::setprecision; #include <cmath> using std::sqrt; // sqrt prototype 6/1/2019 Data Structures: Lecture 5

Example: setprecision (cont.) int main() { double root2 = sqrt( 2.0 ); // calc square root of 2 int places; // precision, vary from 0-9 cout << "Square root of 2 with precisions 0-9." << endl; cout << fixed; // use fixed point format (not sci. not) // set precision for each digit, then show square root for ( places = 0; places <= 9; places++ ) cout << setprecision( places ) << root2 << endl; return 0; } // end main 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Example output Square root of 2 with precisions 0-9. 1 1.4 1.41 1.414 1.4142 1.41421 1.414214 1.4142136 1.41421356 1.414213562 6/1/2019 Data Structures: Lecture 5

Trailing Zeros and Decimal Points (showpoint) Stream manipulator showpoint Floating-point numbers are output with decimal point and trailing zeros Example 79.0 prints as 79.000000 instead of 79 Reset showpoint setting with noshowpoint Implies a default precision of 6 Can override with setprecision 6/1/2019 Data Structures: Lecture 5

Example: showpoint, setprecision #include <iostream> #include <iomanip> using namespace std; int main() { double i, j, x, y; cin >> i >> j >> x >> y; // Input: 1 2 3.4 5 cout << fixed << showpoint; cout << "First output " << endl; cout << i << ',' << j << ',' << setprecision(3) << x << ',' << y << endl; return 0; } OUTPUT: First output 1.000000,2.000000,3.400,5.000 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Characters and input >> discards leading whitespace get() method used to input whitespace characters Optional second argument allows you to input multiple characters Default is 1 cin.get(buffer, 10) reads 10 characters from input Example: int x; char ch; cin >> x >> ch; cin >> x; cin.get(ch); x ch 45 39 ‘\n ’ ‘c’ Input stream: 45 c 39 b 6/1/2019 Data Structures: Lecture 5

Characters and input (cont.) Reading an entire line: getline(char[], num) Reads up to num characters on a line Stops at newline character Example: cin.getline(buffer, 10); Must be careful if input is read using stream extraction operator ( >> ) as well as getline() 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 getline example int numR; char name[20]; cin >> numR; cin.getline(name, 20); If input is: 6 Room 12 what values do numR and name hold? numR = 6 name = “\n”  why? cin >> numR stops at any whitespace character  \n cin.getline(name,20) starts with next char, ends at newline 6/1/2019 Data Structures: Lecture 5

Fixing getline example Skipping whitespace characters: ignore(num) Discards num characters from input stream without storing them To fix previous example: int numR; char name[20]; cin >> numR; cin.ignore(1); cin.getline(name, 20); 6/1/2019 Data Structures: Lecture 5

Data Structures: Lecture 5 Final notes Next time Finish input functions, C++ strings Reminders: Program 1 due Friday, 2/8 All programs to be submitted via Blackboard Submit a single .zip file containing all files for this assignment Additional instructor support for course Grader: Shubham Tikare Office hours: Tues 9-11 AM, Ball 301E (ECE Conf Rm) Tutor: Felipe Loera Hours available on CLASS site (link also on home page) 6/1/2019 Data Structures: Lecture 5