int feet = 0, inches = 0;
int feet(0), inches(0);
int count = 0;
double distance = 1.5;
Alternatively, you could use
int count(0);
double distance(1.5);
3. sum = n1 + n2;
4. length = length + 8.3;
5. product = product * n;
6. The actual output from a program such as this is dependent on the system and the history of the use of the system.
#include <iostream>
using namespace std;
int main()
{
int first, second, third, fourth, fifth;
cout << first << " " << second << " " << third
<< " " << fourth << " " << fifth << endl;
return 0;
}
7. There is no unique right answer for this one. Below are possible answers:
speed
pay_rate
highest
or max_score
cout << "The answer to the question of\n"
<< "Life, the Universe, and Everything is 42.\n";
cout << "Enter a whole number and press return: ";
cin >> the_number;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world\n";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n1, n2, sum;
cout << "Enter two whole numbers\n";
cin >> n1 >> n2;
sum = n1 + n2;
cout << "The sum of " << n1 << " and "
<< n2 << " is " << sum << endl;
return 0;
}
cout << endl << "\t";
#include <iostream>
using namespace std;
int main( )
{
double one(1.0), two(1.414), three(1.732),
four(2.0),five(2.236);
cout << "\tN\tSquare Root\n";
cout << "\t1\t" << one << endl
<< "\t2\t" << two << endl
<< "\t3\t" << three << endl
<< "\t4\t" << four << endl
<< "\t5\t" << five << endl;
return 0;
}
3 * x
3 * x + y
(x + y) / 7 Note that x + y / 7 is not correct.
(3 * x + y) / (z + 2)
bcbc
(1/3) * 3 is equal to 0
Since 1
and 3
are of type int
, the /
operator performs integer division, which discards the remainder, so the value of 1/3
is 0
, not 0.3333
. This makes the value of the entire expression 0 * 3
, which of course is 0
.
#include <iostream>
using namespace std;
int main()
{
int number1, number2;
cout << "Enter two whole numbers: ";
cin >> number1 >> number2;
cout << number1 << " divided by " << number2
<< " equals " << (number1/number2) << endl
<< "with a remainder of " << (number1%number2)
<< endl;
return 0;
}
52.0
9/5
has int
value 1
; since numerator and denominator are both int
, integer division is done; the fractional part is discarded.
f = (9.0 / 5) * c + 32.0;
or this
f = 1.8 * c + 32.0;
20. 030406
The strings are concatenated with the +
operator.
if (score > 100)
cout << "High";
else
cout << "Low";
You may want to add \n
to the end of these quoted strings depending on the other details of the program.
if (savings >= expenses)
{
savings = savings - expenses;
expenses = 0;
cout << "Solvent";
}
else {
cout << "Bankrupt";
}
You may want to add \n
to the end of these quoted strings depending on the other details of the program.
if ( (exam >= 60) && (programs_done >= 10) )
cout << "Passed";
else cout << "Failed";
You may want to add \n
to the end of these quoted strings depending on the other details of the program.
if ( (temperature >= 100) || (pressure >= 200) )
cout << "Warning";
else
cout << "OK";
You may want to add \n
to the end of these quoted strings depending on the other details of the program.
(x < −1) || ( x > 2)
(1 < x) && (x < 3)
0
is false
. In the section on type compatibility, it is noted that the int
value 0
converts to false
.
1
is true
. In the section on type compatibility, it is noted that a nonzero int
value converts to true
.
−1
is true
. In the section on type compatibility, it is noted that a nonzero int
value converts to true
.
10
7
4
1
29. There would be no output, since the Boolean expression (x < 0)
is not satisfied and so the while
statement ends without executing the loop body.
30. The output is exactly the same as it was for Self-Test Exercise 27.
31. The body of the loop is executed before the Boolean expression is checked, the Boolean expression is false, and so the output is
−42
32. With a do-while
statement the loop body is always executed at least once. With a while
statement there can be conditions under which the loop body is not executed at all.
33. This is an infinite loop. The output would begin with the following and conceptually go on forever:
10
13
16
19
(Once the value of x
becomes larger than the largest integer allowed on your computer, the program may stop or exhibit other strange behavior, but the loop is conceptually an infinite loop.)
#include <iostream>
using namespace std;
int main()
{
int n = 1;
while (n <= 20)
{
cout << n << endl;
n++;
}
return 0;
}
if (x < 0)
{
x = 7;
cout << "x is now positive.";
}
else {
x = −7;
cout << "x is now negative.";
}
36. The first line is a comment and is not executed. So the entire output is just the following line:
Self-Test Exercise
#include <iostream>
using namespace std;
int main()
{
const double LITERS_PER_GALLON = 3.78533;
double gallons, liters;
cout << "Enter the number of gallons:\n";
cin >> gallons;
liters = gallons*LITERS_PER_GALLON;
cout << "There are " << liters << " in "
<< gallons << " gallons.\n";
return 0;
}