Professional Documents
Culture Documents
Professor Douglas Troeger The City College of New York Spring, 2016
Professor Douglas Troeger The City College of New York Spring, 2016
Professor Douglas Troeger The City College of New York Spring, 2016
Csc 102
In fact, even
ADD X Y Z
#include<iostream>
using namespace std;
int main () {
int first, second, third;
cout << "Enter three integers, separated by spaces" << endl;
cin >> first >> second >> third;
cout << "The sum of " << first << ", " << second << ", and "
<< third << " is " << first + second + third << endl;
return 0;
}
Solving the main problem
if (min == first)
cout << "The sum of the squares of the two largest of " << first << ", " << second
<< ", and " << third << " is " << second * second + third * third << endl;
if (min == second)
cout << "The sum of the squares of the two largest of " << first << ", " << second
<< ", and " << third << " is " << first * first + third * third << endl;
if (min == third)
cout << "The sum of the squares of the two largest of " << first << ", " << second
<< ", and “ << third << " is " << first * first + second * second << endl;
How can we test this idea?
9332621544394415268169923885626670049071596826438
1621468592963895217599993229915608941463976156518
2862536979208272237582511852109168640000000000000
00000000000
#include <iostream>
using namespace std;
int main () {
int first, second, third;
cout << "Enter three integers, separated by spaces" << endl;
cin >> first >> second >> third;
int min;
// min computation as on previous slide
// sum of squares of two which are not equal to min from the stub solution
return 0;
}
#include <iostream>
using namespace std;
int main () {
int first, second, third;
cout << "Enter three integers, separated by spaces" << endl;
cin >> first >> second >> third;
SumOfSquaresOfTwoLargest.cpp
page 2
int min;
if (first <= second)
if (first <= third) // so (first <= second) and (first <= third)
min = first;
else // so (first <= second) and (third < first), ie, third < first <=
second
min = third;
else
if (first <= third) // so (first > second) and (first <= third), ie, second < first <=
third
min = second;
else
if (second <= third) // so (second < first) and (second <= third)
min = second;
else // so (second < first) and (third < second), ie, third < second < first
min = third;
SumOfSquaresOfTwoLargest.cpp
page 3
if (min == first)
cout << "The sum of the squares of the two largest of " << first << ", " << second << ", and "
<< third << " is " << second * second + third * third << endl;
if (min == second)
cout << "The sum of the squares of the two largest of " << first << ", " << second << ", and "
<< third << " is " << first * first + third * third << endl;
if (min == third)
cout << "The sum of the squares of the two largest of " << first << ", " << second << ", and "
<< third << " is " << first * first + second * second << endl;
return 0;
}
Testing the complete program
Suppose the new program passes all its tests. Are we done?
if (min == first)
cout << ...
if (min == second)
cout << ...
if (min == third)
cout << …
Even if it is the case that min == first, the remaining tests will be executed
unnecessarily. Can you see how to use if-else to improve this?
Reading and Homework
This lecture draws primarily on Savitch,
Chapters 1 and 2
1.1 Computer Systems 2.1 Variables and Assignments
1.2 Programming and Problem 2.2 Input and Output
Solving
2.3 Data Types and Expressions
1.3 Introduction to C++
2.4 Simple Flow of Control – pp
1.4 Testing and Debugging 74 - 84
We also used material on nested if statements from Chapter 3 – pp
120 – 123.