computer programming · •putting things together •writing programs with iteration using what we...

18
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Iterative Programs: Putting It All Together 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Upload: others

Post on 05-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

Computer ProgrammingDr. Deepak B Phatak

Dr. Supratik ChakrabortyDepartment of Computer Science and Engineering

IIT Bombay

Session: Iterative Programs: Putting It All Together

1Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Page 2: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

• Iteration idioms in programming

• “while …”, “do … while …” and “for …” loops in C++

• Use of assignment expressions and their variants in loops

• Use of “break” statements in loops

2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics

Page 3: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

• Putting things together• Writing programs with iteration using what we have learnt

• Use of “break” and “continue” statements

3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture

Page 4: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

Putting It All Together: An Example

• Given positive integer inputs “m” and “n”,

calculate 3min(m, n) and 2max(m,n), and print their values

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4

Page 5: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() {

int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) { minMN++; threeRaisedMin *= 3; }

maxMN++; twoRaisedMax *= 2;

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5

Page 6: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() {

int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) { minMN++; threeRaisedMin *= 3; }

maxMN++; twoRaisedMax *= 2;

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6

Page 7: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() {

int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) { minMN++; threeRaisedMin *= 3; }

maxMN++; twoRaisedMax *= 2;

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7

Page 8: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() {

int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) { minMN++; threeRaisedMin *= 3; }

maxMN++; twoRaisedMax *= 2;

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8

Page 9: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() {

int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) { minMN++; threeRaisedMin *= 3; }

maxMN++; twoRaisedMax *= 2;

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9

Conditionally iterate min(m,n) times

Page 10: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() {

int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) { minMN++; threeRaisedMin *= 3; }

maxMN++; twoRaisedMax *= 2;

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10

Page 11: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Min/Max Example

int main() { int minMN = 0, maxMN = 0, twoRaisedMax = 1, threeRaisedMin = 1;

int m, n, i, j; // Inputs and running counters

cout << “Give m and n: “; cin >> m >> n;

for (i = m, j = n; ((i >= 1) || (j >= 1)); i--, j--) // Iterate max(m, n) times

{ if ((i >= 1) && (j >= 1)) {

minMN++; threeRaisedMin *= 3; // Conditionally iterate min(m,n) times

}

maxMN++; twoRaisedMax *= 2; // Executed max(m, n) times

}

cout << “2^max is: “ << twoRaisedMax << “, 3^min is: “ << threeRaisedMin;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11

Page 12: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

Putting It All Together: Quiz Marks Problem

Read quiz 1 marks of CS101 students one at a time

Any negative marks other than -1000 is invalid and must be ignored

Marks of -1000 denotes end of input

Compute number of valid marks, sum, average, minimum and maximum

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12

Page 13: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Quiz Marks Problem

int main () {

int marks, sum, count, min, max;

float average;

for (sum = 0, count = 1; ; count++) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

if (marks < 0) { if (marks != -1000) { cout << “Invalid marks!!!” << endl; … Repeat loop …}

else { cout << “All marks read!!!” << endl; break; }

}

// Update sum, min, max

}

average = sum/(count + 0.0);

cout << “Count of valid marks: “ << count << “Sum: “ << sum;

cout << “Average: “ << average << “Min: “ << min << “Max: “ << max << endl;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13

Page 14: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Quiz Marks Problem

int main () {

int marks, sum, count, min, max;

float average;

for (sum = 0, count = 1; ; count++) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

if (marks < 0) { if (marks != -1000) { cout << “Invalid marks!!!” << endl; … Repeat loop …}

else { cout << “All marks read!!!” << endl; break; }

}

// Update sum, min, max

}average = sum/(count + 0.0);

cout << “Count of valid marks: “ << count << “Sum: “ << sum;

cout << “Average: “ << average << “Min: “ << min << “Max: “ << max << endl;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14

Skip all instructions in body of loop in this iteration and exit loop

Page 15: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

“continue” statement in C++ Program

int main () {

int marks, sum, count, min, max;

float average;

for (sum = 0, count = 1; ; count++) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

if (marks < 0) { if (marks != -1000) { cout << “Invalid marks!!!” << endl; continue; }

else { cout << “All marks read!!!” << endl; break; }

}

// Update sum, min, max

}

average = sum/(count + 0.0);

cout << “Count of valid marks: “ << count << “Sum: “ << sum;

cout << “Average: “ << average << “Min: “ << min << “Max: “ << max << endl;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15

Skip all instructions in body of loop in this iteration and start next iteration of loop

Page 16: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Quiz Marks Problem

int main () {

int marks, sum, count, min, max;

float average;

for (sum = 0, count = 1; ; count++) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

if (marks < 0) { if (marks != -1000) { cout << “Invalid marks!!!” << endl; continue; }

else { cout << “All marks read!!!” << endl; break; }

}

// Update sum, min, max

}

average = sum/(count + 0.0);

cout << “Count of valid marks: “ << count << “Sum: “ << sum;

cout << “Average: “ << average << “Min: “ << min << “Max: “ << max << endl;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16

sum = sum + marks;if (count == 1) { min = marks; max = marks; }else {

min = (min > marks) ? marks: min;max = (max < marks) ? marks: max;

}

Page 17: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

C++ Program for Quiz Marks Problem

int main () {

int marks, sum, count, min, max;

float average;

for (sum = 0, count = 1; ; count++) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

if (marks < 0) { if (marks != -1000) { cout << “Invalid marks!!!” << endl; continue; }

else { cout << “All marks read!!!” << endl; break; }

}

// Update sum, min, max

}

average = sum/(count + 0.0);

cout << “Count of valid marks: “ << count << “Sum: “ << sum;

cout << “Average: “ << average << “Min: “ << min << “Max: “ << max << endl;

return 0;

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 17

Page 18: Computer Programming · •Putting things together •Writing programs with iteration using what we have learnt •Use of “break” and “continue” statements 3 Dr. Deepak B

IIT Bombay

Summary

• Examples of two C++ programming problems

• Iterative constructs with assignment expressions

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 18