(Problem
1), (10
pts).
(A) (2 pts)
4-18,
on page 296. (If you don't know the answer, test it)
(B) (2 pts)
4-21,
on page 297. (If you don't know the answer, test it)
(C) (6 pts) 4-15,
on page 295. (The solution is given, your job is to modify
it.
Instead of inputting 20
numbers, use rand() to generate 20 random numbers)
(Problem 2), (10 pts). 4-17, on page 295.
(Problem
4),
(5 pts). 4-14,
on page 295.
(You need
only
do one of the "median" OR "mode". If you can do both, you will
get
5 extra pts)
// Exercise 4.15 Solution
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <iomanip>
using std::setw;
int main()
{ const int SIZE = 20;
int a[ SIZE ] = { 0 }, subscript =
0, duplicate,
value;
cout << "Enter 20 integers
between
10 and 100:\n";
for ( int i = 0; i < SIZE; ++i ) {
duplicate = 0;
cin >> value;
for ( int j = 0; j
<
subscript; ++j )
if (
value == a[ j ] ) {
duplicate = 1;
break;
}
if ( !duplicate )
a[ subscript++
] = value;
}
cout << "\nThe nonduplicate
values
are:\n";
for (int i = 0; a[ i ] != 0;
++i )
cout <<
setw( 4
) << a[ i ];
cout << endl;
return 0;
}
// Exercise 4.17 hint: study the
solution of
hw 3-34, and then study the following:
for ( long i = 1; i <= ROLLS; ++i
) {
x = 1 + rand() % 6;
y = 1 + rand() % 6;
++sum[ x + y ];
}
cout << setw( 10 ) <<
"Sum" <<
setw( 10 ) << "Total" << setw( 10 )
<< "Expected"
<< setw( 10 ) << "Actual\n"
<< setiosflags(
ios::fixed | ios::showpoint );
for ( int j = 2; j < SIZE; ++j
)
cout <<
setw( 10
) << j << setw( 10 ) << sum[ j ] <<
setprecision(
3 )
<< setw( 9 ) << 100.0 * expected[ j ] / 36 << "%"
<<
setprecision( 3 )
<< setw( 9 ) << 100.0 * sum[ j ] / 36000 << "%\n";
// Exercise 4.11 Part A hint:
study page 263-264 first. Only a few small changes on
Fig4.16
are needed.
===================================