Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019"— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

7 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

8 Data Structures: Lecture 5
Formatted output Recall earlier example: int i, j; double x, y; ... cin >> x >> y >> i >> j; // Assume input 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

9 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

10 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

11 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

12 Data Structures: Lecture 5
Example output Square root of 2 with precisions 6/1/2019 Data Structures: Lecture 5

13 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 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

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

15 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

16 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

17 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

18 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

19 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


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

Similar presentations


Ads by Google