Upload
betha
View
27
Download
3
Embed Size (px)
DESCRIPTION
ICOM 4015 Advanced Programming. Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3. Prof. Bienvenido Velez. Computer/Human Interaction Outline. Output Streams Input Streams Formatting output. Layered Software Design. GUI. Text-based Batch Interface. Text-based Interactive - PowerPoint PPT Presentation
Citation preview
FALL 2001 ICOM 4015 - Lecture 1 1
ICOM 4015 Advanced Programming
Lecture 1
Computer/Human Interaction
Readings: LMM 2.3 & 3.3
Prof. Bienvenido Velez
FALL 2001 ICOM 4015 - Lecture 1 2
Computer/Human InteractionOutline
• Output Streams
• Input Streams
• Formatting output
FALL 2001 ICOM 4015 - Lecture 1 3
Layered Software Design
Library/Component
Appl Programming Interface (API)
GUI
Text-basedInteractiveInterface
Text-basedBatch
Interface
ICOM 4015
FALL 2001 ICOM 4015 - Lecture 1 4
Example 1Simple output program
*********************************************
#include <iostream>
main() {
for(int i=0; i<10; i++) { for(int j=0; j<i; j++) { cout << "*"; } cout << endl; }}
example1.cc
output
FALL 2001 ICOM 4015 - Lecture 1 5
OutputStream insertion operator
cout << expression
outputstream
streaminsertionoperator
argumentexpression
returns an output stream
FALL 2001 ICOM 4015 - Lecture 1 6
Example 2
0 stars* 1 stars** 2 stars*** 3 stars**** 4 stars***** 5 stars****** 6 stars******* 7 stars******** 8 stars********* 9 stars
#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
output
FALL 2001 ICOM 4015 - Lecture 1 7
InputStream extraction operator
cin >> variable
inputstream
streamextractionoperator
argumentexpression
returns an input stream
FALL 2001 ICOM 4015 - Lecture 1 8
Example 3
[bvelez@amadeus] ~ >> example3Enter number of rows: 7 0 stars* 1 stars** 2 stars*** 3 stars**** 4 stars***** 5 stars****** 6 stars[bvelez@amadeus] ~ >>
#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
shell
FALL 2001 ICOM 4015 - Lecture 1 9
Example 4
[bvelez@amadeus] ~ >> example4Enter number of rows(-1 to end): 5 0 stars* 1 stars** 2 stars*** 3 stars**** 4 starsPlease enter an integer (-1 to end): -1[bvelez@amadeus] ~ >>
#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
shell
FALL 2001 ICOM 4015 - Lecture 1 10
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; }}
[bvelez@amadeus] ~/icom4015/lec2 >> ./example5Please enter number of rows: 5Please 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
FALL 2001 ICOM 4015 - Lecture 1 11
Example 6Computing Statistics in One Pass
[bvelez@amadeus] ~/icom4015/lec02 >> example6Please enter a number (-1 to end): 5Please enter a number (-1 to end): 6Please enter a number (-1 to end): 7Please enter a number (-1 to end): 8Please enter a number (-1 to end): -1Statistics:Average: 6.5Count: 4Max: 8Min: 5[bvelez@amadeus] ~/icom4015/lec02 >>
#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
shell
FALL 2001 ICOM 4015 - Lecture 1 12
Example 6With Input redirection
[bvelez@amadeus] ~/icom4015/lec02 >> example6 < example6.txtPlease 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.5Count: 4Max: 8Min: 5[bvelez@amadeus] ~/icom4015/lec02 >>
5678-1
example6.txt
shell
FALL 2001 ICOM 4015 - Lecture 1 13
Example 6With Input/Output redirection
[bvelez@amadeus] ~/icom4015/lec02 >> example6 < example6.txt >example6.out[bvelez@amadeus] ~/icom4015/lec02 >>
5678-1
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 2001 ICOM 4015 - Lecture 1 14
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, …
FALL 2001 ICOM 4015 - Lecture 1 15
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