150 likes | 163 Views
This lecture covers output and input streams, formatting output, layered software design, GUI, and text-based interfaces.
E N D
ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez ICOM 4015 - Lecture 1
Computer/Human InteractionOutline • Output Streams • Input Streams • Formatting output ICOM 4015 - Lecture 1
Layered Software Design GUI Text-based Batch Interface Text-based Interactive Interface ICOM 4015 Appl Programming Interface (API) Library/Component ICOM 4015 - Lecture 1
#include <iostream> main() { for(int i=0; i<10; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << endl; } } example1.cc Example 1Simple output program * ** *** **** ***** ****** ******* ******** ********* output ICOM 4015 - Lecture 1
OutputStream insertion operator returns an output stream cout << expression stream insertion operator output stream argument expression ICOM 4015 - Lecture 1
#include <iostream> main() { for(int i=0; i<10; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << " " << i << " stars"; cout << endl; } } example2.cc Example 2 0 stars * 1 stars ** 2 stars *** 3 stars **** 4 stars ***** 5 stars ****** 6 stars ******* 7 stars ******** 8 stars ********* 9 stars output ICOM 4015 - Lecture 1
InputStream extraction operator returns an input stream cin >> variable stream extraction operator input stream argument expression ICOM 4015 - Lecture 1
#include <iostream> 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 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] ~ >> shell ICOM 4015 - Lecture 1
#include <iostream> 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 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] ~ >> shell ICOM 4015 - Lecture 1
Example 5I/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; } } example5.cc [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 >> shell ICOM 4015 - Lecture 1
#include <iostream> #include <iomanip> 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 Example 6Computing 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 >> ICOM 4015 - Lecture 1 shell
Example 6With Input redirection 5 6 7 8 -1 example6.txt [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 >> shell ICOM 4015 - Lecture 1
Example 6With Input/Output redirection 5 6 7 8 -1 example6.txt [bvelez@amadeus] ~/icom4015/lec02 >> example6 < example6.txt >example6.out [bvelez@amadeus] ~/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 example1.out ICOM 4015 - Lecture 1
Summary of ConceptsFundamentals • 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, … ICOM 4015 - Lecture 1
Summary of ConceptsLanguage Issues • Language issues • Must #include <iostream> • 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 ICOM 4015 - Lecture 1