Computer Science 173
Intermediate Computer Programming

Denison
CS173 Examples and Labs


Homework 1

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, including the preconditions and postconditions of the function.

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

Problem 1

Consider the following problem from discrete mathematics:

Call pi the ith prime. p1 = 2; p2 = 3...
p1 + 1 is prime; p1 * p2 + 1 is prime; p1 * p2 * p3 + 1 is prime.
Find the first n such that p1*p2*p3*...*pn + 1 is not prime

Write a C++ program to solve this problem, printing out both n and Pn. The program can be written in a single source file, called prime1.cpp. There is no input to the program, and the output consists of the console message with the answer to the problem. Consider where you have opportunities to use a function to solve a subproblem. Clearly define the preconditions and postconditions of such a function, and use a function prototype at the top of the C++ source file and include the function definition following the main function definition.

Problem 2

Suppose that we wish to define our own library of global functions, so that the functions are not within any particular class and are accessible to a main program. To start with, suppose we want to define the following functions that are applicable to our discrete math studies:

  1. power2 -- function to raise 2 to the given power. The argument of this function is an integer and the function returns 2 raised to the argument. Argument values must be greater than or equal to 0, so the result is also an integer. The implementation of this function must use a loop.
  2. ceiling -- take the ceiling of a given double argument. The function returns the input argument "rounded up" to the next highest integral number. Numbers with no fractional part map to themselves. The result of the function is a double.
  3. myfloor -- take the floor of a given double argument. The function returns the input argument "rounded down " to the next lowest integral number. Numbers with no fractional part map to themselves. The result of the function is a double.

We will use two source files for our library. The file "mymath.h" will contain comments and the function prototypes for the three library functions. The file "mymath.cpp" will #include the mymath.h file and will contain comments as well as the definitions for the three library functions. Note that you are not permitted to use the provided <cmath> library in the implementation of your functions.

To compile a library that is self-contained within a single source file, we issue the following compilation command at a Terminal shell:

bressoud@219a> g++ -c mymath.cpp

The -c option to the compiler instructs g++ to compile to "object code", and do not create a final executable file. The result of a successful compilation (one with no syntax errors) is a file named "mymath.o" in the current directory. This object file may then be combined with a main program to create an executable that includes the library. See the next problem for how to do this.

Problem 3

The final problem will largely be self-defined. We want to create a program to test our library of functions created in Problem 2. Points will be awarded for both creativity and for completeness. The ceiling and myfloor functions should be tested for both positive and negative numbers and for values with precisely no fractional part and for those with very small fractional parts. The set of tests comprising the test plan should be enumerated and described in comments.

For the power2 function, a test plan should also be provided and executed and the function should also be applied in the following way:

Given a set A of cardinality n, compute and output the number of subsets of A.

Be imaginative and come up with an "application" for your floor and ceiling functions as well.

If our test program is in a file named libtest.cpp, we can compile it and also combine it with our library object file using the following command:

bressoud@219a> g++ -o libtest mymath.o libtest.cpp

This differs from your single file compilation command only in the addition of specifying the library object file along with the main source file to be compiled.