instructions - GVSU School of Computing and Information Systems

advertisement
EGR/CS 261
Laboratory Activities
1
School of Engineering
Grand Valley State University
EGR/CS 261-02 – Assignment #4
Looping and Conditional Statements
Objectives

To practice using loops

To practice using conditionals

To practice using flowcharts to aid code development
Due Date

Friday, October 8
Background
Prime numbers are integers that have only two multiplicative factors: themselves and 1. For
example, the first five prime numbers are 2, 3, 5, 7 and 11 (1 is not prime since it has only one
factor: namely, the number 1). The Fundamental Theorem of Arithmetic shows that the primes
are the building blocks of the positive integers: every positive integer is a product of prime
numbers in one and only one way. Prime numbers have many applications including
cryptography (the coding of information for the purposes of securing content). One common type
of cryptography (public-key cryptography) uses a pair of prime numbers, one that is used in the
formulation of the encryption key and another that is used in the decryption key. In order to
make more robust cryptographic algorithms, bigger and bigger prime numbers are desired. Your
task today will be to a) determine which of a given set of numbers are prime numbers AND
b) determine which numbers in a range of numbers are prime.
Technique
If a number is prime that means that there are no smaller numbers than the number that
divide evenly into it, other than one. For example, assume we wish to test if the number 11 is
prime. We successively test to see if 10, 9, 8, 7, 6, 5, 4, 3 or 2 divide evenly into 11; clearly,
none do, and hence, 11 must be prime. A little more thought reveals that we did not have to test
all the way down to 2, for if this were a factor, a larger number would have already successfully
divided evenly. This is easy to see for an example of 10; we do not have to test down to 2 since
Copyright © 2010 Padnos College of Engineering and Computing
EGR/CS 261
Laboratory Activities
2
the test for the factor 5 already identified the number 2 as a factor. In fact, we only need to test
down to the truncated square root of the number we wish to test (this is true only if the number
we are testing is greater than 3 – ie if you want to test 66, you only need to test if 66 is divisible
by numbers from 65 to 8). The test for whether one number divides evenly into another can be
preformed easily using the modulo operator (%).
Steps to determine if the number, n, is prime:
1. Declare variables in main: start, stop, prime_flag, current_number
2. Read in number to check from file or user, n.
3. Set prime_flag = 1, i.e. assume that the number n is prime
4. Set start = n-1; and stop = (int)floor(sqrt(n));
5. Use current_number to loop from start to stop and check if n%current_number
evaluates to 0.
6. If the remainder is equal to 0 print out the intermediate factors and reset the prime_flag
to 0.
7. Check the value of the prime_flag, if its 0 then declare the number as not a prime
number else declare it a prime number.
Part I – [50 points]
Description (asst4a.c)
You are to create a program that will read in an integer and test to see if that number is prime
(the numbers are listed in the file “prime_nos.txt” provided for you on the course website). You
must run your program one time for the entire number set. Test for EOF (or use the function feof
()) to know when to stop processing (see the example endoffile.c posted on course website). You
are to determine if each integer read is prime or not, and output the result to stdout. If a number
is prime, your output should read:
101 = 101 x 1
101: PRIME Number
For a number that turns out not to be prime, your output should read:
333 = 333 x 1
333 = 111 x 3
333 = 37 x 9
333: NOT A PRIME Number
Copyright © 2010 Padnos College of Engineering and Computing
EGR/CS 261
Laboratory Activities
3

Part II – [30 points]

Description (asst4b.c)
Revise your program of lab4a.c to handle a range of integers as specified by the user. For
example, the user can request the program to find all prime numbers in the range of 900 to 1000.
Your output should now just list ONLY the prime numbers in the given range and NOT the
intermediate factors OR the numbers that are not prime. Only test for ranges that have a start
number greater than 5. Remember to include error checks and COMMENT your code.

Part III – [20 points]

Description (asst4c.c)
Write a program that will calculate and print bills for the city power company. The rates
vary depending on whether the use is residential, commercial, or industrial. A code of “R”
means residential use, a code of “C” means commercial use, and a code of “I” means
industrial use. Any other code should be treated as an error.
The rates are computed as follows:
R: $6.00 plus $0.052 per kwh used
C: $60.00 for the first 1000 kwh and $0.045 for each additional kwh
I: Rate varies depending on time of usage:
Peak hours: $76.00 for first 1000 kwh and $0.065 for each additional
kwh
Off-peak hours: $40.00 for first 1000 kwh and $0.028 for each
additional kwh
Your program should prompt the user to enter an integer account number, the use code
(type char), and the necessary consumption figures in whole numbers of kilowatt–hours.
Your program should display the amount due from the user.
NOTE: Account numbers between 0 and 16383 (both inclusive) are considered to be
operating at peak hours, whereas account numbers between 16384 and 32767 (both
inclusive) are considered to be operating at off-peak hours.

Copyright © 2010 Padnos College of Engineering and Computing
EGR/CS 261
Laboratory Activities
Deliverables
You need to submit the following:

Printouts of results from parts (a), (b) and (c).

Printouts of asst4a.c, asst4b.c and asst4c.c source code files.
Remember to staple everything together and write your name on all work handed in.
Copyright © 2010 Padnos College of Engineering and Computing
4
Download