1. It is known that the harmonic series is divergent; that is, it grows without bound. However, it diverges very slowly—summing 10000 terms only amounts to about 9.78751. Write C++ code for a program accepts a user-supplied value for M and then computes and displays the minimum value of N so that . Use a while structure for looping.
SOLN:
#include <iostream.h>
**************************************
#include <stdlib.h>
#include <iomanip.h>
int main() {
char quit;
cout << "**************************************\n"
<< "* Enter a value for the harmonic *\n"
<< "* series to exceed and the program *\n"
<< "* returns the number of terms in the *\n"
<< "* harmonic series that are needed to *\n"
<< "* reach that value. *\n"
<< "**************************************\n";
double M=1.,n,h_series;
cout << "\n\nEnter a value for sum(1/n,n=1..M) to beat: ";
cin>>M;
do {
h_series=0; n = 1.;
while (h_series < M) {
h_series += 1/n;
++n;
}
cout << "\nn = " << setprecision(12) << n
<< " terms are needed to exceed " << M << " .\n";
cout << "\nAnother? Or enter '0' to quit: ";
cin>>M;
} while( M > 1.);
system("PAUSE");
return 0;
}
--------------------------------------------------------------
Here is the output from this code:
* Enter a value for the harmonic *
* series to exceed and the program *
* returns the number of terms in the *
* harmonic series that are needed to *
* reach that value. *
**************************************
Enter a value for sum(1/n,n=1..M) to beat: 10
n = 12368 terms are needed to exceed 10 .
Another? Or enter '0' to quit: 15
n = 1835422 terms are needed to exceed 15 .
Another? Or enter '0' to quit: 20
n = 272400601 terms are needed to exceed 20 .
Another? Or enter '0' to quit: 21
n = 740461602 terms are needed to exceed 21 .
Another? Or enter '0' to quit: 0
Press any key to continue . . .
2. Write C++ code for a program to produce a random integer between 1 and 100 and allow the user to guess what it is. If it is too low or too high, inform the user and prompt them to guess a number in the narrowed interval of remaining possibilities. The screen for game play might look something like this:
Enter a guess for the number between 1 and 100
62
Your guess is too low.
Enter a guess for the number between 62 and 100
86
Your guess is too low.
Enter a guess for the number between 86 and 100
95
Your guess is too low.
Enter a guess for the number between 95 and 100
98
Your guess is too low.
Enter a guess for the number between 98 and 100
99
Congratulations! The target was, in fact, 99.
SOLN:
#include <iostream.h>
#include <stdlib.h>#include <time.h>
int main()
{
int lower = 0, upper = 100, guess, goal = (rand()%100 + 1);
cout << "Guess a number beween 1 and 100: ";
do cin >> guess; while(guess<1 || guess>100);
while( guess != goal ) {
if (guess < goal) {
lower = guess;
cout << "\nYour guess is too low. "
<< "\nEnter a guess for the number between "
<< lower << " and " << upper << ": ";
}
else {
upper = guess;
cout << "\nYour guess is too high. "
<< "\nEnter a guess for the number between "
<< lower << " and " << upper << ": ";
}
do cin >> guess; while(guess<=lower || guess>=upper);
}
cout << "\nCongratulations! The target was, in fact, " << goal << endl;
system("PAUSE");
return 0;
}
a.
#include <iostream>
int main()
{
int x = 10;
while(x != 1) x /= 5;
cout << x;
return 0;
}
SOLN: The problem with this code is that x will have subsequent values 10, 2, 0 and then 0 again and again, but never 1. So the condition for exiting the loop is never met, leading to an infinite loop. This is a logic error. How to fix it depends on what it is intended to accomplish, which, admittedly, is somewhat murky. It looks like the goal is to print the number 1, so why not replace the entire body with the single statement, cout << “1”; ?
b.
The following
code is meant to assure that the input is in the range between 1 and 100. How does it do?
#include
<iostream.h>
void main()
cout << “\nEnter a number
between 1 and 100:” endl
do
cin >> guess;
while(guess>0
&& guess<UpperBound)
cout << “Your guess was “
<< setw(5) guess;
return 0;
SOLN: There are some syntax errors.
(1) Missing braces {
} at beginning and end of main().
(2) Between the string and
endl on the first cout
there should be the out stream operator,
<<.
(3) Also after the
endl there
needs to be a semicolon.
(4) Variables “guess” and “UpperBound”
are never declared.
(5) There is no semicolon after the
do/while
statement.
(6) Missing stream operator between
setw(5)
and guess in the last cout statement.
(7) If you’re going to use “setw(),”
you’d better include iomanip.h.
(8) Shouldn't return 0 if the return type is declared void.
There is also a logic error in that the condition for continuing the loop
should be
“guess<0 || guess>UpperBound”
4. Write
a program to carry out the activity in the flow chart below. What is the result when the input is 42?
SOLN:
void main() {
int x = 42;
while(
x > 1) {
if
(x%3==0) {
x = x/3+1;
if (x%5==0)
x = x/5 + 2;
}
x = 2*x+1;
}
For the input
x = 42,
there is no output
since x
assigned values 15,5,11,23,…
and all subsequent values are not
divisible
by 3. To see this, note that
5%3 = 2,
and that for any x with x%3=2, there is
an integer k such that
x =
3k+2 and
thus 2x+1 = 6k+5
also has a remainder 2
after division by 3.