Presentation is loading. Please wait.

Presentation is loading. Please wait.

FALL 2001ICOM 4015 - Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez.

Similar presentations


Presentation on theme: "FALL 2001ICOM 4015 - Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez."— Presentation transcript:

1

2 FALL 2001ICOM 4015 - Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez

3 FALL 2001ICOM 4015 - Lecture 12 Computer/Human Interaction Outline Output Streams Input Streams Formatting output

4 FALL 2001ICOM 4015 - Lecture 13 Layered Software Design Library/Component Appl Programming Interface (API) GUI Text-based Interactive Interface Text-based Batch Interface ICOM 4015

5 FALL 2001ICOM 4015 - 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

6 FALL 2001ICOM 4015 - Lecture 15 Output Stream insertion operator cout << expression output stream insertion operator argument expression returns an output stream

7 FALL 2001ICOM 4015 - 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

8 FALL 2001ICOM 4015 - Lecture 17 Input Stream extraction operator cin >> variable input stream extraction operator argument expression returns an input stream

9 FALL 2001ICOM 4015 - Lecture 18 Example 3 [bvelez@amadeus] ~ >> example3 Enter number of rows: 7 0 stars * 1 stars ** 2 stars *** 3 stars **** 4 stars ***** 5 stars ****** 6 stars [bvelez@amadeus] ~ >> #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

10 FALL 2001ICOM 4015 - Lecture 19 Example 4 [bvelez@amadeus] ~ >> 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 [bvelez@amadeus] ~ >> #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

11 FALL 2001ICOM 4015 - 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; } [bvelez@amadeus] ~/icom4015/lec2 >>./example5 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] ~/icom4015/lec02 >> example5.cc shell

12 FALL 2001ICOM 4015 - Lecture 111 Example 6 Computing Statistics in One Pass [bvelez@amadeus] ~/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 [bvelez@amadeus] ~/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

13 FALL 2001ICOM 4015 - Lecture 112 Example 6 With Input redirection [bvelez@amadeus] ~/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 [bvelez@amadeus] ~/icom4015/lec02 >> 5 6 7 8 example6.txt shell

14 FALL 2001ICOM 4015 - Lecture 113 Example 6 With Input/Output redirection [bvelez@amadeus] ~/icom4015/lec02 >> example6 example6.out [bvelez@amadeus] ~/icom4015/lec02 >> 5 6 7 8 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

15 FALL 2001ICOM 4015 - 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, emailed, …

16 FALL 2001ICOM 4015 - 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


Download ppt "FALL 2001ICOM 4015 - Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez."

Similar presentations


Ads by Google