CS 1320 (Principles of Algorithm Design I):
Homework #8
- Assigned:
- April 7, 2000.
- Due:
- April 14, 2000, at start of class.
- Credit:
- 30 points (10 points per problem).
Instructions
Write a C++ program to solve each of the problems below.
Your code should begin with comments that describe
what the program is supposed to do, in terms of its inputs
and outputs.
Any function you write should begin with brief comments
describing its purpose and preconditions/postconditions.
See the sample programs
for examples.
Problems
Decimal-to-binary conversion
Recall that earlier this term we discussed an algorithm for
converting a non-negative decimal (base 10) integer X to
binary (base 2).
The algorithm computes the bits (binary digits)
of the answer in reverse order;
if we call the bits of the answer
r0,
r1, ....
rN-1,
the algorithm looks like this (using pseudocode as we did back
then):
i = 0
beginLoop:
if X > 0
divide X by 2, saving the remainder as ri
and replacing X with the quotient
i = i + 1
go to beginLoop
else
if i = 0
print 0
else
print ri-1 ... r0
Write a C++ program that uses this algorithm to convert a
non-negative decimal (base 10) number to binary and prints the
result. For example:
- If the user enters 5, the program should print 101.
- If the user enters 0, the program should print 0.
- If the user enters 25, the program should print 11001.
- If the user enters a negative number, the program should
print an error message.
You can assume that the number the user enters is not too big
to fit into the computer's int data type; you can also
assume that the int data type is no more than 32 bits.
Calculating median
The median of N numbers
x1,
x2, ...
xN
is a number y such that there are as many
xi's greater than or equal to y as there are
xi's less than or equal to y.
So,
if we rearrange the
xi's to be in order from smallest to largest:
- if N is odd, the median is the element in the middle
of the rearranged sequence, and
- if N is even, the median is the average of the two
elements in the middle of the rearranged sequence.
For example:
- The median of 2.2, 1.1, 4.4, 5.5, and 3.3 is 3.3.
- The median of 2.2, 4.4, 1.1, and 3.3 is 2.75 (average of 2.2 and
3.3).
Write a C++ program that calculates the median of up to 20
numbers (not necessarily integers) read from standard input
and prints the result to standard output.
If the user enters no numbers, the program should print a message
that no numbers were entered and not attempt the calculation.
If the user tries to enter more than 20 numbers, the program should
print a warning message and use only the first 20 numbers entered.
Hint:
There is a library function called sort() that
sorts the elements of an array, i.e., rearranges them to be in order,
from smallest to largest.
To use this function
to sort the first N elements of array nums,
include the line #include <algorithm> in your program,
and call the function as follows:
sort(nums, nums+N);
The array nums can be an array of ints,
floats, doubles, or just about any other type that
can be sorted from smallest to largest.
Hint:
See the sample program
print_reverse
for an example of how to determine whether we've already read
everything the user entered.
Finding primes
An integer N is said to be prime if it is greater than 1
and cannot be evenly divided by any number other than itself and 1.
About 250 B.C., the Greek mathematician Eratosthenes proposed
the following method of finding all primes less than
or equal to N.
- Write down all the integers from 2 through N in order.
- Circle 2 (a prime), and then go through the rest of the list
and cross out all other numbers that are multiples of 2.
(These numbers cannot be prime.)
- Find the first number m
in the list that is neither circled nor
crossed out. This number is prime. Circle it, and cross
out all other numbers that are multiples of m.
(These numbers cannot be prime.)
- Continue in this manner until all numbers less than or equal
to the square root of N are either circled or
crossed out. (You can stop then because every number in
the list is either prime or has a factor less than or equal
to the square root of N.)
- The primes are then all the numbers in the list
that are not crossed out, including the ones that are circled.
Write a C++ program to use this method to find and print all the
prime numbers less than or equal to 100. (So, this program does not
need any input from the user.)
Hint:
Consider how to represent "crossing out an element of a list"
in your program.
It may be helpful to consider how we kept track of which
faces of the die had been seen already in
the example program simulating rolling a die
(roll_dice_v1.cc).
Additional tip:
There is a library function fill() that will assign
the same value to every element of an array.
To use this function to set each of the
first N elements of array x
to have value y,
include the line #include <algorithm> in your program,
and call the function as follows:
fill(x, x+N, y);
The array x can be an array of ints,
floats, bools, or just about any other type,
so long as y is of the same type.
For example, to declare and initialize an array of 10 ints,
all 0, we could write:
#include <algorithm>
const int N = 10;
int nums[N];
fill(nums, nums + N, 0)
See
roll_dice_v2.cc
for another example of using this function.
What to turn in
Submit your source code as described in the
guidelines for programming assignments.
For this assignment:
- Use a subject header of "cs1320 homework 8".
- Submit three source files, one for each problem.
Please choose names for the files that indicate
which program solves which problem.
(For example,
you could use dec2bin.cc,
median.cc, and
primes.cc.)
You do not need to submit any input files;
I will supply the required input files when I test your
programs.