CPSC 231: Assignment 4 (Due Wednesday, July 17)

Weighting = 3% (Executable  solutions to the assignment can be found in Unix under /home/231/assignments/assignment4).  The assignment must be completed on the Computer Science computers.  You must hand in your submission via a printed typescript.  For the assignment you must run the following inputs within your script session: 1,  100, 1000, 1001.  You can run additional data to demonstrate any additional functions of your program but the above inputs are require for your submission.  (Failure to run these values will result in the loss of a letter "step", e.g., from "A" to "A-", "A-" to "B+").

For further details please refer to the link on general assignment specifications.

"B version" of the assignment: Input a number and yield it's factors

(If you fulfill all assignment specifications listed here then the maximum grade that you can get is a "B").    For this version of the assignment you are to write a program that will prompt the user to input an integer between 2 and 1000.  The program will then display all the factors of that number.  A factor of a number will evenly divide into that number.  You can view a more precise mathematical definition at the following link.  The program will display only a maximum of 10 factors on a line (see Figure 1 below).    After that succeeding factors will displayed on the next line.  Also columns must be neatly formatted so that numbers in the same column are right justified.


Figure 1: Example output of the factors program.

"A version" of the assignment: Input a number and yield it's prime factors

(If you fulfill all assignment specifications then the maximum grade that you can get is a "A").  This version of the assignment will display the output in the fashion above however it will display only the prime factors for the number that the user inputs.  Prime factors of a number are ones that are factors of the number but cannot be evenly divided by any other number  (save 1 and itself).  Prime factors start at the number 2 (not one).     For example, the number 5 is prime because it's only factors are one and itself.  The number six, however is not prime because it's factors (and not necessarily prime factors) include 1, 2, 3 and 6.  Since some of it's factors are numbers other than 1 or itself six so it is not prime.  For a more precise definition of a prime number please refer to the following link.  (You might not want to code your assignment based on this definition though, it's included more for precision than a hint as to how to do the assignment...)  In addition, the Mathematics department at Washington State provides an web form that allows you to determine all of the prime factors of a number.  

Figure 2: Example output of the prime factors program

It is suggested that you get a working B-version of the assignment first.  Make a copy of this working program via the Unix copy command (e.g., if the working program is called a4.p then at the Unix prompt type "cp a4.p a4.BAK" to make a copy called "a4.BAK".  Because the A-version of the program does what the B-version does except that it restricts the possibilities you should be able build upon it.  

(Big) Hint: The user inputs some number n.  The first program checks from one to n to see which number is a factor of the number n.   The second program merely looks at this set of factors to determine if it is prime or not.

Further questions?  Still stuck?  Don't forget all of the help resources available!

 

Learning objectives:

To understand decision making and looping constructs enough to apply them in a working Pascal program.