ICOM 4015 Advanced Programming Lecture 2 ComputerHuman Interaction

  • Slides: 11
Download presentation
ICOM 4015 Advanced Programming Lecture 2 Computer/Human Interaction (Part II) Reading: Chapter 11 (Up

ICOM 4015 Advanced Programming Lecture 2 Computer/Human Interaction (Part II) Reading: Chapter 11 (Up to I/O manipulators) Prof. Bienvenido Velez 2/6/2022 ICOM 4015 - Spring 2001 1

Computer/Human Interaction Outline • Administrivia • Formatted I/O Manipulators • Input/Output redirection 2/6/2022 ICOM

Computer/Human Interaction Outline • Administrivia • Formatted I/O Manipulators • Input/Output redirection 2/6/2022 ICOM 4015 - Spring 2001 2

Administrivia • Read Chapters 1 -2 Deitel&Deitel • Lab meeting times changed: – Please

Administrivia • Read Chapters 1 -2 Deitel&Deitel • Lab meeting times changed: – Please sign up for a lab • No labs this week • Ta’s email addresses – Jose Torres (jet@amadeus. uprm. edu) – Jairo Valiente (jvaliente@amadeus. uprm. edu) • Accounts @ amadeus ready – USER Ids: derived from student #’s – Visit the lab to unlock your account 2/6/2022 ICOM 4015 - Spring 2001 3

Output Stream insertion operator returns an output stream cout output stream 2/6/2022 << stream

Output Stream insertion operator returns an output stream cout output stream 2/6/2022 << stream insertion operator expression argument expression ICOM 4015 - Spring 2001 4

Input Stream extraction operator returns an input stream cin input stream 2/6/2022 >> stream

Input Stream extraction operator returns an input stream cin input stream 2/6/2022 >> stream extraction operator variable argument expression ICOM 4015 - Spring 2001 5

Example 5 I/O Manipulators extern "C"{ #include <stdlib. h> } #include <iostream> #include <iomanip>

Example 5 I/O Manipulators extern "C"{ #include <stdlib. h> } #include <iostream> #include <iomanip> main() { int rows = 0; cout << "Please enter number of rows: "; cin >> rows; int columns = 0; cout << "Please enter number of columns: "; cin >> columns; unsigned int seed = 511; srand (seed); for (int i=0; i<rows; i++) { for (int j=0; j<columns; j++) { cout << setprecision(4) // prints only 4 decimal digits << setw(10) // prints each number is field of 10 spaces << setfill('*') // fills empty spaces with '*' << rand()/float(RAND_MAX) << " "; } cout << endl; } } example 5. cc [bvelez@amadeus] ~/icom 4015/lec 2 >>. /example 5 Please enter number of rows: 5 Please enter number of columns: 5 ****0. 6858 ****0. 4712 ***0. 08415 ****0. 7637 ****0. 5106 ****0. 2954 *****0. 756 ****0. 9517 ****0. 9653 ****0. 1655 ****0. 7404 ****0. 7084 ****0. 2605 ****0. 9858 ****0. 8554 ****0. 5025 *****0. 583 ****0. 5728 ***0. 03958 ***0. 02455 ****0. 6029 ****0. 7031 ****0. 2055 ****0. 6303 ****0. 5802 [bvelez@amadeus] ~/icom 4015/lec 02 >> shell 2/6/2022 ICOM 4015 - Spring 2001 6

Example 6 Computing Statistics in One Pass #include <iostream> #include <iomanip> main() { float

Example 6 Computing Statistics in One Pass #include <iostream> #include <iomanip> main() { float next. Number; float sum = 0; float count = 0; float max; float min; bool first. Time = true; while (true) { cout << "Please enter a number (-1 to end): "; cin >> next. Number; if (next. Number == -1) { break; } sum += next. Number; count++; if (first. Time) { max = next. Number; min = next. Number; first. Time = false; } else { max = (max < next. Number) ? next. Number : max; min = (min > next. Number) ? next. Number : min; } } cout << "Statistics: " << endl; cout << "Average: " << sum / count << endl; cout << "Count: "<< count << endl; cout << "Max: " << max << endl; cout << "Min: " << min << endl; } example 6. cc [bvelez@amadeus] ~/icom 4015/lec 02 >> example 6 Please enter a number (-1 to end): 5 Please enter a number (-1 to end): 6 Please enter a number (-1 to end): 7 Please enter a number (-1 to end): 8 Please enter a number (-1 to end): -1 Statistics: Average: 6. 5 Count: 4 Max: 8 Min: 5 [bvelez@amadeus] ~/icom 4015/lec 02 2/6/2022 ICOM 4015 >> - Spring 2001 7 shell

Example 6 With Input redirection 5 6 7 8 -1 example 6. txt [bvelez@amadeus]

Example 6 With Input redirection 5 6 7 8 -1 example 6. txt [bvelez@amadeus] ~/icom 4015/lec 02 >> example 6 < example 6. txt Please enter a number (-1 to end): Please en ter a number (-1 to end): Please enter a num ber (-1 to end): Statistics: Average: 6. 5 Count: 4 Max: 8 Min: 5 [bvelez@amadeus] ~/icom 4015/lec 02 >> shell 2/6/2022 ICOM 4015 - Spring 2001 8

Example 6 With Input/Output redirection 5 6 7 8 -1 example 6. txt [bvelez@amadeus]

Example 6 With Input/Output redirection 5 6 7 8 -1 example 6. txt [bvelez@amadeus] ~/icom 4015/lec 02 >> example 6 < example 6. txt >example 6. out [bvelez@amadeus] ~/icom 4015/lec 02 >> shell Please enter a number (-1 to end): Please en ter a number (-1 to end): Please enter a num ber (-1 to end): Statistics: Average: 6. 5 Count: 4 Max: 8 Min: 5 example 1. out 2/6/2022 ICOM 4015 - Spring 2001 9

Summary of Concepts Fundamentals • Computer-Human Interaction is an area of study in its

Summary of Concepts Fundamentals • Computer-Human Interaction is an area of study in its own right within Computer Science • One pass computing - Process input as you read it. Less memory required to store data. Limited algorithmic capacity. • Input redirection allows me to store input in a file. Helpful for debugging because don’t need to re-enter test input every time. • Output redirection allows me to dump the output of a program to a file. A file can be examined, saved, emailed, … 2/6/2022 ICOM 4015 - Spring 2001 10

Summary of Concepts Language Issues • Language issues – Must #include <iostream> – Stream

Summary of Concepts Language Issues • Language issues – Must #include <iostream> – Stream insertion operators can be chained. Example: (cout << expr 1) << expr 2 – Stream extraction operator can be chained. Example: (cin >> var 1) >> var 2 – I/O manipulators provide fine-grain control over format of output • setprecision – decimal digits • setw – width of output field • setfill – filler for empty space 2/6/2022 ICOM 4015 - Spring 2001 11