Comp 1671
Solutions to homework5/review
From text, chapter 5
10. sum = 0;
count = 0;
while (count < 10)
{
cin >> num;
sum = sum + num;
count++;
}
11. output is:
11 18 25
13. a. 18
b. 14
c. false - the program would compile, but when we run it, it goes
into an infinite loop
17. a. *
b. an infinite loop, it outputs * forever
c. an infinite loop, it outputs * forever
d. **** (4 stars)
e. ****** (6 stars)
f. *** (3 stars)
18. int i;
int sum(0);
for (i = 1; i<= 100; i++)
if (i%3==0)
sum+=i;
22.
#include <iostream>
#include <iomanip>
using namespace std;
const int N=2137;
int main()
{
int a, b, c, d, i;
c = 0; // initialize c!!
a = 3;
b = 5;
c = c + d;
// N = a + n; Not allowed to store in a const variable, and n is undeclared
for (i= 3; i <= N; i++)
{
cout << setw(5) << i;
i = i + 1; // probably a logic error, increment is already done
}
return 0;
}
30a. outputs:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
30b. 1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
30c. outputs the triangle
2 3 4 5
3 4 5
4 5
5
30d. outputs the triangle
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
30e. outputs the integers from 1 to 100, 10 per line
30f. outputs
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
Chapter 6 problems:
1. a. false (you also need a #include statement)
b. true
c. true
d. true
e. false
5. a. 4
b. 26
c. 10 4 0
d. 0
Review problems:
1. 1101101, 1101110, 1101111, 1110000, 1110001, 1110010, 1110011, 1110100,
1110101, 1110110, 1110111
2. Answers will vary, but your partner should come up with your original
number
3. Legal: money, humuhumunukunuku, Take_2, i, Index
Illegal: 2D, joe@moo.com, happy-day, under cover, xRay!
4a. cin >> num_reps;
4b. total_wealth *=2;
or total_wealth = total_wealth * 2;
or total_wealth = total_wealth + total_wealth;
or total_wealth += total_wealth;
to name a few.
4d. (sorry for the wacky numbering)
if (x < 0)
y = x*x+4;
else if (0 <= x && x <= 2)
y = 5;
else
y = 1/(x+3);
4e. float sum=0; // running total
int i; // counter
float num; // user inputs this number
for (i=0; i<10; i++)
{
cout << "Enter a number: ";
cin >> num;
sum += num;
}
cout << "Total is " << sum;
4f.
int num; // user inputs this number
int even_total(0); // running total of even entries
int odd_total(0); // running total of odd entries
do {
cout << "Please enter number (-1 to quit): ";
cin >> num;
if (num >= 0) {
if (num%2 == 0)
even_total += num;
else
odd_total += num;
}
} while (num >= 0);
cout << "Even total: " << even_total << endl;
cout << "Odd total: " << odd_total << endl;
5.
6. answers will vary.
7. for (counter = 0; counter < 100; counter+=2)
cout << counter;
(outputs the even integers from 0 to 98)
8. (again, sorry for the wacky order)
a. 4
b. 5
d. duh, c is never incremented, so although the loop is executed 101 times,
c remains 0 throughout
c. 100
9. No. Consider the expression in the "if" statement. The second part of
the && is (1/(j+1)) > -1). If this expression is evaluated, there will
be a divide by zero (since j is -1). But now consider the first part
of the expression, j>0. This is false, and so by short-circuit evaluation
(see p. 155), the result of the && is definitely false. The second part
of the expression (the dangerous part) is never evaluated.
10. Yikes, this code contains the nasty error of = instead of ==.
the expression in the if statement (x=2) is an assignment statement,
which is executed (the number 2 is stored in x), and evaluted to true.
Thus, we output 2*x, or 4.
11. First notice that x>3 is false, and (x*y>10) is true, so the expression
in the first if evaluates to true. We proceed to the next if.
(x-y < 0) is true, so !(x-y<0) is false, so we don't need to evaluate
the second part of the && expression - the whole expression is false no
matter what. Thus, we execute the "else" statement immediately following
the second if. We execute cout << --x-y;
Now we look up in the operator precedence chart whether -- or - occurs
first, and we have (--x)-y
We execute --x which reduces the value in x to 2, and itself also takes
a value of 2. Finally, we subtract y (which is 4), for a final result of
output of -2.
12. if (Value == 1 || Value ==2)
cout << 2*Value;
else if (Value ==3 || Value == 4)
cout << 3*Value;
else
cout << 4*Value;
13. a *= 2*b + c is equivalent to a = a * (2*b + c)
this can also be written as a = 2*a*b + a*c
14. salary *= 1.02 + bonus;
note: salary * 1.02 + salary*bonus is equivalent to salary*(1.02+bonus)
15. 8.2 2.2 11
standard calculation for the statement num2 *= 2*i + num1 (no funny
stuff)
since i, j are integers i/j is an integer division. i/j is 0.