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:

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: For example:

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 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: