int main()
{
roundToIntegers();
return
0;
}
void
roundToIntegers(
void )
{ double x, y;
cout
<< setiosflags( ios::fixed | ios::showpoint );
for
(
int loop = 1; loop <= 5; loop++ ) {
cout << "Enter a number: ";
cin >> x;
y = floor( x + .5 );
cout << x << " rounded is " << setprecision( 1 )
<<
y << endl;
}
}
Problem
(3-19):
study the fig03_04.cpp
on p.165,
which
takes more than one inputs.
Problem
(3-20):
study the solution of 3.21.
// Exercise 3.21 Solution
#include
<iostream>
using std::cout;
using std::endl;
using std::cin;
bool even( int );
int main()
{ int x;
for
(
int i = 1; i <= 3; ++i ) {
cout << "Enter an integer: ";
cin >> x;
if ( even( x ) )
cout << x << " is an even integer\n\n";
else
cout << x << " is an odd integer\n\n";
}
cout
<< endl;
return
0;
}
bool even(
int a )
{ return !( a %
2
);
}
Problem
(3-23):
study the solution of 3.22.
// Exercise 3.22 Solution
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
void square( int );
int main()
{ int side;
cout << "Enter side: ";
cin >> side;
cout << '\n';
square( side );
cout << endl;
return 0;
}
void square( int s )
{ for ( int row = 1; row <= s; ++row ) {
for ( int col = 1;
col
<= s; ++col )
cout
<< '*';
cout << '\n';
}
}
Problem
(3-40):
study Fig03_14 on page 187, Fig03_15 on page 189,
and ex03_50
on
page 236.
Problem
(3-41):
Part (b) only.
Cut and paste Fig03_15 on page 189 for your use. If you
computer
is not
fast enough to run the problem, you may cut and paste the
following
solution.
// Exercise 3.41 Part A Solution - the Instructor
// NOTE: This exercise was accidently placed in this
// chapter. The solution utilizes ARRAYS which are
// introduced in the next chapter.
#include <iostream>
using std::cout;
using std::endl;
int MAX = 22; // the maximum number
for which
the
// fibonacci value can be calculated
// on 2-byte integer systems
int fibonacci( int );
int main()
{
for ( int loop = 0; loop <= MAX;
++loop
)
cout <<
"fibonacci("
<< loop << ") = " << fibonacci( loop )
<< "\n";
cout << endl;
return 0;
}
int fibonacci( int n )
{
int fib[ 23 ];
fib[ 0 ] = 0;
fib[ 1 ] = 1;
for ( int j = 2; j <= n; ++j )
fib[ j ] = fib[ j -
1
] + fib[ j - 2 ];
return fib[ n ];
}
===================================
Exam-3 on Nov. 6 (covers Chapter 3).
Do following problems for preparing Exam-3.
(1) Study Fig3.19 on page 183. Then convert
the
"Inline function" program into a program
with a "normal"
function,
which we know better.
(2) Study 3.51 on page 219. What does the
program
do? (if you don't know, test it on the PC)
(3) review the problems on page 223 to 228.