Download presentation
Presentation is loading. Please wait.
1
COMS 261 Computer Science I
Title: C++ Fundamentals Date: September 17, 2004 Lecture Number: 12
2
Announcements Project 1 Exam 1 Due 9/24/04 (next Friday)
9/22/04 (next Wednesday)
3
Review Boolean Algebra Boolean Type Relational Operators
Operator Precedence Revisited Conditional Constructs
4
Outline Conditional Constructs Iterative Constructs
5
Operator Precedence Revisited
Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || (!false) == (5 < 24))
6
Conditional Constructs
Provide Ability to control whether a statement list is executed Two constructs If statement if if-else if-else-if Switch statement Left for reading
7
The Basic If Statement Syntax if (Expression) Action
If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action true false
8
Example if (Value < 0) { Value = -Value; }
9
Sorting Two Numbers cout << "Enter two integers: "; int value1;
cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;
10
Semantics
11
The If-Else Statement Syntax if (Expression) Action1 else Action2
If Expression is true then execute Action1 otherwise execute Action2 if (v == 0) { cout << "v is 0"; } else { cout << "v is not 0"; } Check out the indentation! Expression Action1 Action2 true false
12
Finding the Max cout << "Enter two integers: "; int value1;
cin >> value1 >> value2; int max; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl;
13
Finding the Max
14
Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice if-else-if statement if-else statements “glued” together Switch statement An advanced construct
15
An If-Else-If Statement
if (nbr < 0){ cout << nbr << " is negative" << endl; } else if (nbr > 0) { cout << nbr << " is positive" << endl; } else { cout << nbr << " is zero" << endl; }
16
A Switch Statement switch (ch) { case 'a': case 'A':
case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }
17
Example cout << "Enter simple expression: "; int left;
int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (Operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }
18
Determining Average Magnitude
Calculate the average brightness of a list of five star magnitude values Can we do it? Yes, it would be easy Calculate the average brightness of a list of 8,479 stars visible from earth Can we do it Yes, but it would be tedious without the use of iteration
19
C++ Iterative Constructs
Three constructs while statement for statement do-while statement
20
While Syntax
21
While Semantics
22
Computing an Average int listSize = 4; int numberProcessed = 0;
double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
23
Execution Trace Suppose input contains: 1 5 3 1 6 int listSize = 4;
int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
24
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
25
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl;
26
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum
27
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum
28
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value --
29
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value --
30
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum value 1
31
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
32
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
33
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
34
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
35
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 1
36
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 5
37
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 1 value 5
38
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
39
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 1 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
40
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
41
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
42
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 5
43
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 6 value 3
44
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 2 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
45
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
46
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
47
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 3
48
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 9 value 1
49
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 3 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
50
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
51
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; sum 10 value 1
52
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed; cout << "Average: " << average << endl; sum 10 value 1 average 2.5
53
Execution Trace Suppose input contains: 1 5 3 1 6 listSize 4
numberProcessed 4 int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed; cout << "Average: " << average << endl; sum 10 value 1 average 2.5
54
Stays in stream until extracted
Execution Trace Suppose input contains: Stays in stream until extracted int listSize = 4; int numberProcessed = 0; double sum = 0; while (numberProcessed < listSize) { double value; cin >> value; sum += value; ++numberProcessed; } double average = sum / numberProcessed; cout << "Average: " << average << endl;
55
Even Better Way of Averaging
int numberProcessed = 0; double sum = 0; double value; while (cin >> value) { sum += value; ++numberProcessed; } double average = sum / numberProcessed ; cout << "Average: " << average << endl; The value of the extraction operation corresponds to true only if a successful extraction was made What if list is empty?
56
Even Better Way of Averaging
int numberProcessed = 0; double sum = 0; double value; while (cin >> value) { sum += value; ++numberProcessed; } if (numberProcessed > 0) { double average = sum / numberProcessed ; cout << "Average: " << average << endl; } else { cout << "No list to average" << endl;
57
The For Statement Syntax for (ForInit ; ForExpression; PostExpression)
Action Example for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; }
59
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i
60
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i
61
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i
62
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i
63
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i 1
64
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i 1
65
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 1
66
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 1
67
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 2
68
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i 2
69
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 2
70
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 2
71
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 3
72
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 i 3
73
Execution Trace for (int i = 0; i < 3; ++i) { cout << "i is " << i << endl; } cout << "all done" << endl; i is 0 i is 1 i is 2 all done i 3
74
Table Revisiting const int TableSize = 20; long Entry = 1;
cout << "i" << "\t\t" << "2**i" << endl; for (int i = 0; i <= TableSize; ++i) { cout << i << "\t\t" << Entry << endl; Entry *= 2; }
75
Table Revisiting const int TableSize = 20; long Entry = 1;
cout << "i" << "\t\t" << "2**i" << endl; for (int i = 0; i < TableSize; ++i) { cout << i << "\t\t" << Entry << endl; Entry = 2 * Entry; } cout << "i is" << i << endl; // illegal The scope of i is limited to the loop!
76
Displaying a Diagonal SimpleWindow W("One diagonal", 5.5, 2.25);
W.Open(); for (int j = 1; j <= 3; ++j) { float x = j * ; float y = j * ; float Side = 0.4; RectangleShape S(W, x, y, Blue, Side, Side); S.Draw(); }
77
Sample Display
78
Displaying Three Diagonals
SimpleWindow W("Three diagonals", 6.5, 2.25); W.Open(); for (int i = 1; i <= 3; ++i) { for (int j = 1; j <= 3; ++j) { float x = i j * ; float y = j * ; float Side = 0.4; RectangleShape S(W, x, y, Blue, Side, Side); S.Draw(); } The scope of i includes the inner loop. The scope of j is just the inner loop.
79
Sample Display
80
int Counter1 = 0; int Counter2 = 0; int Counter3 = 0; int Counter4 = 0; int Counter5 = 0; ++Counter1; for (int i = 1; i <= 10; ++i) { ++Counter2; for (int j = 1; j <= 20; ++j) { Counter3; } ++Counter4; } ++Counter5; cout << Counter1 << " " << Counter2 << " " << Counter3 << " " << Counter4 << " " << Counter5 << endl;
81
For Into While Observation The for statement is equivalent to {
ForInit; while (ForExpression) { Action; PostExpression; }
82
Only extracts nonblank characters
Counting Characters int NumberOfNonBlanks = 0; int NumberOfUpperCase = 0; char c; while (cin >> c) { ++NumberOfNonBlanks; if ((c >= 'A') && (c <= 'Z')) { ++NumberOfUpperCase; } cout << "Nonblank characters: " << NumberOfNonBlanks << endl << "Uppercase characters: " << NumberOfUpperCase << endl; Only extracts nonblank characters
83
Counting All Characters
char c; int NumberOfCharacters = 0; int NumberOfLines = 0; while ( cin.get(c) ) { ++NumberOfCharacters; if (c == '\n') { ++NumberOfLines } } cout << "Characters: " << NumberOfCharacters << endl << "Lines: " << NumberOfLines << endl; Extracts all characters
84
File Processing #include <iostream> #include <fstream>
using namespace std; int main() { ifstream fin("mydata.txt"); int ValuesProcessed = 0; float ValueSum = 0; float Value; while ( fin >> Value ) { ValueSum += Value; ++ValuesProcessed; } if (ValuesProcessed > 0) { ofstream fout("average.txt"); float Average = ValueSum / ValuesProcessed; fout << "Average: " << Average << endl; return 0; else { cerr << "No list to average" << endl; return 1;
85
Iteration Do’s Key Points
Make sure there is a statement that will eventually terminate the iteration criterion The loop must stop! Make sure that initialization of loop counters or iterators is properly performed Have a clear purpose for the loop Document the purpose of the loop Document how the body of the loop advances the purpose of the loop
86
The Do-While Statement
Syntax do Action while (Expression) Semantics Execute Action If Expression is true then execute Action again Repeat this process until Expression evaluates to false Action is either a single statement or a group of statements within braces Action true Expression false
87
Waiting for a Proper Reply
char Reply; do { cout << "Decision (y, n): "; if (cin >> Reply) Reply = tolower(Reply); else Reply = 'n'; } while ((Reply != 'y') && (Reply != 'n'));
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.