lecture5slides

lecture5slides - Todays Topics 1. 2. 3. 4. 5. Project #3 Q...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Today’s Topics 1. 2. Formatting numbers with cout 3. “clock” arithmetic 4. Characters and ASCII 5. Functions, functions, functions Wednesday 1. More functions 2. Implicit variable promotion 3. Enum 4. Reference parameters 5. Simple recursion
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Formatting Numbers with cout To format the width of ONLY the next item printed: cout << setw(#) << item; To specify the number of digits printed for ALL SUBSEQUENT items: cout << setprecision (##) << item; cout << anotheritem; To specify the number of digits AFTER the decimal for ALL SUBSEQUENT items: cout << setiosflags(ios::fixed) << setprecision (##) << item; To left justify ALL SUBSEQUENT items: cout << setiosflags(ios::left); cout << setw(10) << item; To right justify ALL SUBSEQUENT items: cout << setiosflags(ios::right); cout << setw(10) << item;
Background image of page 2
Formatting Numbers with cout #include <iostream> #include <iomanip> using namespace std; using std::ios; void main(void) { cout << setw(10) << "hello" << "|"; cout << setprecision( 1) << setw(10) << 3.14159 << "|"; cout << setprecision( 2) << setw(10) << 3.14159 << "|"; cout << setprecision( 5) << 30.14159 << "|"; cout << 30.14159 << "|"; cout << endl; cout << setw(10) << "hello" << "|"; cout << setiosflags(ios::fixed) << setprecision( 1) << setw(10) << 3.14159 << "|"; cout << setprecision( 2) << setw(10) << 3.14159 << "|"; cout << setprecision( 5) << 30.14159 << "|"; cout << 30.14159 << "|"; cout << endl; } Results: hello| 3| 3.1|30.142|30.142| hello| 3.1| 3.14|30.14159|30.14159|
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Wrapping Arithmetic Sometimes we want to constrain a number between certain values when doing adding/subtracting: Examples: clocks: 0 to 23 hours, 0 to 60 mins cards: 0 to 51 etc. Q: How can we do this easily? A: With the modulo operator.
Background image of page 4
Clocks and the Modulo Operator: FORWARD #include <iostream> #include <iomanip> using namespace std; using std::ios; void main(void) { int time, wait; cout << "Enter start time (0-23): "; cin >> time; do { cout << "# hours to wait? "; cin >> wait; if (wait != -1) { time = time + wait; time = time % 24; // OR: time = (time + wait) %24; } cout << "cur time: " << time; cout << endl; } while (wait != -1); }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Clocks and the Modulo Operator: BACK! #include <iostream>
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 20

lecture5slides - Todays Topics 1. 2. 3. 4. 5. Project #3 Q...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online