Homework 6 Solution
1. Assuming the following declarations:
int i = 7, j = 3, k = 5;
char c =
'H';
double x =
7.3, y = 0.5;
string s =
"do", t = "ne";
Fill in the following table, giving the type and
value of each expression.
|
Expression |
Type |
Value |
|
i + x |
double |
14.3 |
|
c - 'A' |
int |
7 |
|
s == "do" && x<5 || x > 6 |
bool |
true |
|
k = j * y |
int |
1 |
|
cin >> k |
istream |
Leave Blank |
2. Write a program that will read in three integers and print out the greatest of the three integers.
Turn in written answers to the first question, your source code, and a screen capture of your program running.
Solution
// KGS
// Homework 6
// This prints the largest of three integers
#include <iostream>
using namespace std;
int main (){
int num1, num2, num3;
cout << "This program was written by KGS." << endl;
cout << "Enter three integers: ";
cin >> num1 >> num2
>> num3;
// Is the first number the largest? Use
>= because okay if num1 is same as one of the others.
if (num1 >= num2 &&
num1 >= num3){
cout
<< "The greatest integer is "
<< num1 <<"." << endl;
}
// Is the
second number the largest?
else
if (num2 >= num1 && num2 >= num3){
cout
<< "The greatest integer is "
<< num2 <<"." << endl;
}
// Otherwise, the third number
must be the greatest -- so we just need ‘else’ no test
else {
cout
<< "The greatest integer is "
<< num3 <<"." << endl;
}
return 0;
}