User Tools

Site Tools


Sidebar

projects

cci0 (due 20200123)
pct1 (bonus; due 20200123)
wcp1 (due 20200123)
fwf0 (due 20200129)
pct2 (due 20200129)
wcp2 (due 20200129)
dtr0 (due 20200205)
sof0 (due 20200205)
pct3 (due 20200205)
wcp3 (due 20200205)
dow0 (due 20200212)
pct4 (due 20200212)
wcp4 (due 20200212)
bwp0 (bonus; due 20200226)
pct5 (due 20200226)
wcp5 (due 20200226)
cos0 (due 20200304)
pct6 (due 20200304)
wcp6 (due 20200304)
cbf0 (due 20200311)
pct7 (due 20200311)
wcp7 (due 20200311)
pct8 (due 20200318)
wcp8 (due 20200318)
sam0 (due 20200325)
pct9 (due 20200325)
wcp9 (due 20200325)
afn0 (due 20200401)
pctA (due 20200401)
wcpA (due 20200401)
cnv0 (due 20200422)
pctB (due 20200422)
wcpB (due 20200422)
bwp1 (bonus; due 20200422)
gfo0 (due 20200429)
pctC (due 20200429)
wcpC (due 20200429)
eoce (due 20200513)
haas:spring2020:cprog:projects:cnv0

This is an old revision of the document!


Corning Community College

CSCS1320 C/C++ Programming

Project: COMPUTATION - CALCULATING N-ARY VALUES (cnv0)

Objective

To create a program that can calculate and determine the number of factor pairs (nary value) over a given range of numbers.

Reading

In “The C Book”, please read through Chapter 6.

Background

In mathematics, you have likely encountered the notion of “prime” numbers, those values which are divisible only by 1 and the number itself.

Expanding our view on the situation, when considering factors of a number, we have the presence of a “factor pair”; ie a pair of two values that are evenly divisible into that number.

For 17, a prime number, we have just ONE factor pair: 1 and 17:

  • 17 % 1 == 0
  • 17 % 17 == 0

All other values (2-16) when we divide them into 17 results in a non-zero value for the remainder.

In this way, prime, or primary, numbers, have exactly ONE factor pair. To further simplify matters, we can call it an N-ary(1) or nary(1) value. Where the number indicates the number of factor pairs.

A secondary, or nary(2) number, on the other hand, has exactly TWO sets of factor pairs.

Take the number 6, for instance:

  • factor pair of 1 and 6
  • factor pair of 2 and 3

Where 17 was a primary number, 6 is a secondary number.

Determining factor pairs

We are going to be exploring a basic, brute force, method of determining factors for a number, and that is the “trial by division” method.

Here, we successively divide a number by potential factors, to see if the factor evenly divides into the number. For convenience, we will assume the 1 and number factor pair, because EVERY number is evenly divisible by 1 and itself.

So, the number 5:

  • 5 % 2 == 1
  • 5 % 3 == 2
  • 5 % 4 == 1

No other evenly divisible factors were found in the range 2-(N-1), therefore we are only left with the factor pair of 1 and N, making 5 an nary(1) value.

The number 14:

  • 14 % 2 == 0 another factor!
  • 14 % 3 == 2
  • 14 % 4 == 2
  • 14 % 5 == 4
  • 14 % 6 == 2
  • 14 % 7 == 0 another factor!
  • 14 % 8 == 6
  • 14 % 9 == 5
  • 14 % 10 == 4
  • 14 % 11 == 3
  • 14 % 12 == 2
  • 14 % 13 == 1

Because factor pairs ALWAYS come in a set of 2, we have the factor pairs of 1 and 14, along with 2 and 7.

How about 12:

  • 12 % 2 == 0
  • 12 % 3 == 0
  • 12 % 4 == 0
  • 12 % 5 == 2
  • 12 % 6 == 0
  • 12 % 7 == 5
  • 12 % 8 == 4
  • 12 % 9 == 3
  • 12 % 10 == 2
  • 12 % 11 == 1

There are 4 additional factors discovered here, giving us a total of 6 factors, or three factor pairs:

  • 1, 12
  • 2, 6
  • 3, 4

Notice also how the factors are nested: 1 and 12 are the outermost, 2 and 6 are encapsulated within that, and inside there, 3 and 4.

Because there are 3 factor pairs, 12 would be considered an nary(3) value (or a tertiary number).

Program

It is your task to write a program that, upon accepting various pieces of input from the user, computes the number of factor pairs of a given number or range of numbers, displaying to STDOUT all the numbers in that range that qualify as a secondary number.

Specifications

Your program should:

  • have valid, descriptive variable names of length no shorter than 4 symbols
  • have consistent, well-defined indentation (no less than 4 spaces per level of indentation)
    • all code within the same scope aligned to its indentation level
  • have proximal comments explaining your rationale and what is going on, throughout your code
  • from the command-line, obtain the needed parameters for your program (require these arguments to be present, displaying errors to STDERR and exiting if they are not present):
    • argv[1]: N-ary value (how many factor pairs are we requiring; 1 for prime, 2 for secondary, 3 for tertiary, etc.); this should be stored and managed as an unsigned char.
    • argv[2]: lower-bound (where to start processing, inclusive of the lower bound value); this should be stored in an unsigned short int
    • argv[3]: upper-bound (where to stop processing, inclusive of the upper bound value); this should be shored in an unsigned short int
  • immediately after the input, check to make sure the specified parameters are positive numbers:
    • N-ary value must be between 1 and 16 (inclusive of 1 and 16)
    • lower bound must be between 2 and 40000 (inclusive of 2 and 40000)
    • upper bound must be between 2 and 65000 (inclusive of 2 and 65000)
  • proceed to evaluate the appropriate number range, determining whether or not it is an N-ary number of runtime specification
    • if it is, display the value to STDOUT in space-separated form (see execution section below for message)
    • if it is not, do not display anything related to that value (again, see execution section below)
  • using a single return statement at the conclusion of the code, return a 0 indicating successful operation
    • for all error results, use exit() instead; there should only be exactly ONE return() statement in your entire program

Some additional points of consideration:

  • Note that the driving variables in your loops need to be at least of type short int, otherwise you may get a warning when you compile it.

Execution

Primary number (nary(1)) output

lab46:~/src/cprog/cnv0$ ./cnv0 1 8 24
11 13 17 19 23 
lab46:~/src/cprog/cnv0$ 

Secondary number (nary(2)) output

lab46:~/src/cprog/cnv0$ ./cnv0 2 3 12
4 6 8 9 10 
lab46:~/src/cprog/cnv0$ 

Tertiary number (nary(3)) output

lab46:~/src/cprog/cnv0$ ./cnv0 3 11 37
12, 16, 18, 20, 28, 32 
lab46:~/src/cprog/cnv0$ 

The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.

Compiling

As we have been doing all along, use the following options to gcc when compiling:

lab46:~/src/cprog/cnv0$ gcc -Wall --std=gnu99 -o cnv0 cnv0.c
lab46:~/src/cprog/cnv0$ 

Reference

In the CPROG public directory, inside the cnv0 subdirectory, will be a copy of my implementation (in executable form, by the name ref_cnv0), which abides by the project specifications. Please compare its output against that of your implementation.

lab46:~/src/cprog/cnv0$ /var/public/spring2020/cprog/cnv0/ref_cnv0 2 2 40
4 6 8 9 10 14 15 21 22 25 26 27 33 34 35 38 39
lab46:~/src/cprog/cnv0$ 

Verification

In addition, I have also placed a cnv0verify script in that same subdirectory, which will test your program against a range of values, to determine overall correctness.

lab46:~/src/cprog/cnv0$ /var/public/spring2020/cprog/cnv0/cnv0verify
[  1] you have: err, should be: err    [  2] you have:  no, should be:  no
[  3] you have:  no, should be:  no    [  4] you have: yes, should be: yes
[  5] you have:  no, should be:  no    [  6] you have: yes, should be: yes
[  7] you have:  no, should be:  no    [  8] you have: yes, should be: yes
[  9] you have: yes, should be: yes    [ 10] you have: yes, should be: yes
[ 11] you have:  no, should be:  no    [ 12] you have:  no, should be:  no
[ 13] you have:  no, should be:  no    [ 14] you have: yes, should be: yes
[ 15] you have: yes, should be: yes    [ 16] you have:  no, should be:  no
[ 17] you have:  no, should be:  no    [ 18] you have:  no, should be:  no
[ 19] you have:  no, should be:  no    [ 20] you have:  no, should be:  no
[ 21] you have: yes, should be: yes    [ 22] you have: yes, should be: yes
[ 23] you have:  no, should be:  no    [ 24] you have:  no, should be:  no
[ 25] you have: yes, should be: yes    [ 26] you have: yes, should be: yes
[ 27] you have: yes, should be: yes    [ 28] you have:  no, should be:  no
[ 29] you have:  no, should be:  no    [ 30] you have:  no, should be:  no
[ 31] you have:  no, should be:  no    [ 32] you have:  no, should be:  no
[ 33] you have: yes, should be: yes    [ 34] you have: yes, should be: yes
[ 35] you have: yes, should be: yes    [ 36] you have:  no, should be:  no
lab46:~/src/cprog/cnv0$ 

Submission

To successfully complete this project, the following criteria must be met:

  • Code must compile cleanly (no notes, warnings, nor errors)
  • Output must be correct, and match the form given in the sample output above.
  • Code must be nicely and consistently indented, to show scope and maximize readability
  • Code must be well commented (why and how comments)
  • Do NOT double space your code. Group like statements together.
  • Output Formatting (including spacing) of program must conform to the provided output (see above).
  • Track/version the source code in your lab46 repository
  • Submit a copy of your source code to me using the submit tool.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog cnv0 cnv0.c
Submitting cprog project "cnv0":
    -> cnv0.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

What I'll be looking for:

78:cnv0:final tally of results (78/78)
*:cnv0:proper error checking and status reporting performed [13/13]
*:cnv0:correct variable types and name lengths used [13/13]
*:cnv0:proper output formatting per specifications [13/13]
*:cnv0:runtime tests of submitted program succeed [13/13]
*:cnv0:no negative compiler messages for program [13/13]
*:cnv0:code is pushed to lab46 repository [13/13]

Additionally:

  • Solutions not abiding by spirit of project will be subject to a 25% overall deduction
  • Solutions not utilizing descriptive why and how comments will be subject to a 25% overall deduction
  • Solutions not utilizing consistent, sensible indentation to promote scope and clarity will be subject to a 25% overall deduction
  • Solutions not organized and easy to read are subject to a 25% overall deduction
haas/spring2020/cprog/projects/cnv0.1585312443.txt.gz · Last modified: 2020/03/27 12:34 by wedge