FALL 2001ICOM Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez
FALL 2001ICOM Lecture 12 Computer/Human Interaction Outline Output Streams Input Streams Formatting output
FALL 2001ICOM Lecture 13 Layered Software Design Library/Component Appl Programming Interface (API) GUI Text-based Interactive Interface Text-based Batch Interface ICOM 4015
FALL 2001ICOM Lecture 14 Example 1 Simple output program * ** *** **** ***** ****** ******* ******** ********* #include main() { for(int i=0; i<10; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << endl; } example1.cc output
FALL 2001ICOM Lecture 15 Output Stream insertion operator cout << expression output stream insertion operator argument expression returns an output stream
FALL 2001ICOM Lecture 16 Example 2 0 stars * 1 stars ** 2 stars *** 3 stars **** 4 stars ***** 5 stars ****** 6 stars ******* 7 stars ******** 8 stars ********* 9 stars #include main() { for(int i=0; i<10; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << " " << i << " stars"; cout << endl; } example2.cc output
FALL 2001ICOM Lecture 17 Input Stream extraction operator cin >> variable input stream extraction operator argument expression returns an input stream
FALL 2001ICOM Lecture 18 Example 3 ~ >> example3 Enter number of rows: 7 0 stars * 1 stars ** 2 stars *** 3 stars **** 4 stars ***** 5 stars ****** 6 stars ~ >> #include main() { int entered; cout << “Enter number of rows: "; cin >> entered; for(int i=0; i<entered; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << " " << i << " stars"; cout << endl; } example3.cc shell
FALL 2001ICOM Lecture 19 Example 4 ~ >> example4 Enter number of rows(-1 to end): 5 0 stars * 1 stars ** 2 stars *** 3 stars **** 4 stars Please enter an integer (-1 to end): -1 ~ >> #include main() { while (true) { cout << " Enter number of rows (-1 to end): "; int entered; cin >> entered; if (entered == -1) { break; } for(int i=0; i<entered; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << " " << i << " stars"; cout << endl; } example4.cc shell
FALL 2001ICOM Lecture 110 Example 5 I/O Manipulators extern "C"{ #include } #include 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; } ~/icom4015/lec2 >>./example5 Please enter number of rows: 5 Please enter number of columns: 5 **** **** *** **** **** **** *****0.756 **** **** **** **** **** **** **** **** **** *****0.583 **** *** *** **** **** **** **** **** ~/icom4015/lec02 >> example5.cc shell
FALL 2001ICOM Lecture 111 Example 6 Computing Statistics in One Pass ~/icom4015/lec02 >> example6 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 ~/icom4015/lec02 >> #include main() { float nextNumber; float sum = 0; float count = 0; float max; float min; bool firstTime = true; while (true) { cout << "Please enter a number (-1 to end): "; cin >> nextNumber; if (nextNumber == -1) { break; } sum += nextNumber; count++; if (firstTime) { max = nextNumber; min = nextNumber; firstTime = false; } else { max = (max < nextNumber) ? nextNumber : max; min = (min > nextNumber) ? nextNumber : min; } cout << "Statistics:" << endl; cout << "Average: " << sum / count << endl; cout << "Count: "<< count << endl; cout << "Max: " << max << endl; cout << "Min: " << min << endl; } example6.cc shell
FALL 2001ICOM Lecture 112 Example 6 With Input redirection ~/icom4015/lec02 >> example6 < example6.txt Please enter a number (-1 to end): Please enter a number (-1 to end): Please en\ ter a number (-1 to end): Please enter a number (-1 to end): Please enter a num\ ber (-1 to end): Statistics: Average: 6.5 Count: 4 Max: 8 Min: 5 ~/icom4015/lec02 >> example6.txt shell
FALL 2001ICOM Lecture 113 Example 6 With Input/Output redirection ~/icom4015/lec02 >> example6 example6.out ~/icom4015/lec02 >> shell Please enter a number (-1 to end): Please enter a number (-1 to end): Please en\ ter a number (-1 to end): Please enter a number (-1 to end): Please enter a num\ ber (-1 to end): Statistics: Average: 6.5 Count: 4 Max: 8 Min: 5 example6.txt example1.out
FALL 2001ICOM Lecture 114 Summary of Concepts Fundamentals Computer-Human Interaction is a subarea 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 expressiveness. Input redirection allows me to store input 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, ed, …
FALL 2001ICOM Lecture 115 Summary of Concepts Language Issues Language issues –Must #include –Stream insertion operators can be chained. Example: (cout << expr1) << expr2 –Stream extraction operator can be chained. Example: (cin >> var1) >> var2 –I/O manipulators provide fine-grain control over format of output setprecision – decimal digits setw – width of output field setfill – filler for empty space