Computer Science 173
Intermediate Computer Programming

Denison
CS173 Homework 2


Homework 2

Overall homework comments:

Program grading will not be based solely on whether or not the programs "works." It will also be based on the organization, structure, and documentation (in the form of program comments) for the submitted work.

Be sure and document your code well. Include:

  1. Your name in a comment block at the top of every source file.
  2. A high level description of the problem solved by the program.
  3. Descriptive comments for each function.

Please see the C++ Programming Style Guide and follow the conventions described there. Up to 20% of the grade for this homework will be based on following these conventions and practicing good documentation.

Programs

  1. (Pg. 141, #14) A prime number is an integer greater than 1 whose only positive divisors are 1 and the integer itself. The Greek mathematician Eratosthenes developed an algorithm, known as the Sieve of Eratosthenes, for finding all prime numbers less than or equal to a given number, n -- that is, all prime numbers in the range 2 through n. Consider the list of numbers from 2 through n. Two is the first prime number, but the multiples of 2 (4, 6, 8, ...) are not, and so they are corssed out in the list. The first number after 2 that was not crossed out is 3, the next prime. We then cross out from the list all higher multiples of 3 (6, 9, 12, ...). Continuing, we find the _next_ number not crossed out, and find it is 5. So we cross hour all higher multiples of 5 (10, 15, 20, ...). We repeat this procedure until we reach the first number in the list that has not been crossed out and whose square is greater than n. All the numbers that remain in the list are the primes from 2 to n. Write a program that uses this algorithm with an array as the "list" to find all the prime numbers from 2 through n. Use dynamic allocation to create the array. Name your program sieve.cpp.

  2. When you join the Benevolent Order of Programmers, you can be known at BOP meetings by your real name, your job title, or by your secret BOP name. Write a program named bop.cpp that can list members by real name, by job title, by secret name, or by a member's preference. Base the program on the following structure:

    // Benevolent Order of Programmers name structure
    struct bop {
    char fullname[strsize]; // real name
    char title[strsize]; // job title
    char bopname[strsize]; // secret BOP name
    int preference; // 0 = fullname, 1 = title, 2 = bopname
    };

    In the program, create a small array of such structures and initialize it to suitable values. Have the program run a loop that lets the user select from different alternatives:

    a. display by name
    b. display by title
    c. display by bopname
    d. display by preference
    q. quit

    Note that "display by preference" does not mean display the preference member; it means display the member corresponding to the preference number. For instance, if preference is 1, choice d would display the programmer's job title. A sample run may look something like the following:

    Benevolent Order of Programmers Report
    a. display by name b. display by title
    c. display by bopname d. display by preference
    q. quit
    Enter your choice: a
    Wimp Macho
    Raki Rhodes
    Celia Laiter
    Hoppy Hipman
    Pat Hand
    Next choice: d
    Wimp Macho
    Junior Programmer
    MIPS
    Analyst Trainee
    LOOPY
    Next choice: q
    Bye!