User Tools

Site Tools


haas:fall2019:c4eng:projects:cnv0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
haas:fall2019:c4eng:projects:cnv0 [2019/10/07 11:46] – [Specifications] wedgehaas:fall2019:c4eng:projects:cnv0 [2019/10/07 18:14] (current) – [Specifications] wedge
Line 7: Line 7:
  
 =====Objective===== =====Objective=====
-To create a program that can calculate and determine the number of factor pairs of a given number, starting with values with exactly 2 sets of factor pairs.+To create a program that can calculate and determine the number of factor pairs of a given number, starting with values composed of exactly 2 sets of factor pairs.
  
 =====Reading===== =====Reading=====
Line 19: Line 19:
 For 17, a prime number, we have just ONE factor pair: 1 and 17: For 17, a prime number, we have just ONE factor pair: 1 and 17:
  
-17 % 1 == 0 +  * 17 % 1 == 0 
-17 % 17 == 0+  17 % 17 == 0
  
 All other values (2-16) when we divide them into 17 results in a non-zero value for the remainder. All other values (2-16) when we divide them into 17 results in a non-zero value for the remainder.
Line 50: Line 50:
 The number 14: The number 14:
  
-  * 14 % 2 == 0  // another factor!+  * 14 % 2 == 0   another factor!
   * 14 % 3 == 2   * 14 % 3 == 2
   * 14 % 4 == 2   * 14 % 4 == 2
   * 14 % 5 == 4   * 14 % 5 == 4
   * 14 % 6 == 2   * 14 % 6 == 2
-  * 14 % 7 == 0  // another factor!+  * 14 % 7 == 0   another factor!
   * 14 % 8 == 6   * 14 % 8 == 6
   * 14 % 9 == 5   * 14 % 9 == 5
Line 97: Line 97:
     * all code within the same scope aligned to its indentation level     * 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   * have proximal comments explaining your rationale and what is going on, throughout your code
-  * to STDERR, prompt for the number (range appropriate of a signed long int)+  * to STDERR, prompt for the number (range appropriate of an unsigned long int)
     * properly store this in a variable of type **unsigned long int**     * properly store this in a variable of type **unsigned long int**
   * immediately after the input, check to make sure the input number is a positive number greater than or equal to 2; if in violation, display an error (to STDERR) and exit with a non-zero value.   * immediately after the input, check to make sure the input number is a positive number greater than or equal to 2; if in violation, display an error (to STDERR) and exit with a non-zero value.
 +  * proceed to evaluate the input number, determining whether or not it is a secondary (nary(2)) number.
 +    * if it is, display to STDOUT that it is a secondary number (see execution section below for message)
 +    * if it is not, display to STDOUT that it is not a secondary number (again, see execution section below)
   * using a single return statement at the conclusion of the code, return a 0 indicating successful operation   * using a single return statement at the conclusion of the code, return a 0 indicating successful operation
  
Line 107: Line 110:
 =====Execution===== =====Execution=====
  
-====Sample incrementation run====+====Secondary number output====
 <cli> <cli>
-lab46:~/src/c4eng/bcf0$ ./bcf0 +lab46:~/src/c4eng/cnv0$ ./cnv0 
-Enter starting value (0-1023)12 +Enter a number6 
-Enter ending value (0-1023): 28 +6 is a secondary number 
-0000001100 +lab46:~/src/c4eng/cnv0
-0000001101 +
-0000001110 +
-0000001111 +
-0000010000 +
-0000010001 +
-0000010010 +
-0000010011 +
-0000010100 +
-0000010101 +
-0000010110 +
-0000010111 +
-0000011000 +
-0000011001 +
-0000011010 +
-0000011011 +
-0000011100 +
-lab46:~/src/c4eng/bcf0+
 </cli> </cli>
  
-The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate. +====Non-secondary number output====
- +
-====Sample decrementation run====+
 <cli> <cli>
-lab46:~/src/c4eng/bcf0$ ./bcf0 +lab46:~/src/c4eng/cnv0$ ./cnv0 
-Enter starting value (0-1023)17 +Enter a number7 
-Enter ending value (0-1023): 0 +7 is NOT a secondary number
-0000010001 +
-0000010000 +
-0000001111 +
-0000001110 +
-0000001101 +
-0000001100 +
-0000001011 +
-0000001010 +
-0000001001 +
-0000001000 +
-0000000111 +
-0000000110 +
-0000000101 +
-0000000100 +
-0000000011 +
-0000000010 +
-0000000001 +
-0000000000 +
-lab46:~/src/c4eng/bcf0$ +
 </cli> </cli>
  
-====Sample run with invalid input given (scenario 1)====+====Additional outputs====
 <cli> <cli>
-lab46:~/src/c4eng/bcf0$ ./bcf0 +lab46:~/src/c4eng/cnv0$ ./cnv0 
-Enter starting value (0-1023)5543 +Enter a number8 
-ERRORinput value must be between 0-1023! +8 is a secondary number 
-lab46:~/src/c4eng/bcf0+lab46:~/src/c4eng/cnv0$ ./cnv0 
 +Enter a number: 16 
 +16 is NOT a secondary number 
 +lab46:~/src/c4eng/cnv0$ ./cnv0 
 +Enter a number: 21 
 +21 is a secondary number 
 +lab46:~/src/c4eng/cnv0
 </cli> </cli>
  
-====Sample run with invalid input given (scenario 2)====+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: 
 <cli> <cli>
-lab46:~/src/c4eng/bcf0./bcf0 +lab46:~/src/c4eng/cnv0gcc -Wall --std=gnu99 -o cnv0 cnv0.c 
-Enter starting value (0-1023): 255 +lab46:~/src/c4eng/cnv0
-Enter ending value (0-1023): 7168 +
-ERROR: input value must be between 0-1023! +
-lab46:~/src/c4eng/bcf0+
 </cli> </cli>
  
 =====Reference===== =====Reference=====
-In the C4ENG public directory, inside the **bcf0** subdirectory, will be a copy of my implementation (in executable form), which abides by the project specifications. Please compare its output against that of your implementation.+In the C4ENG 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.
  
-=====Obtaining binary values===== +<cli> 
-You might wonder howwhen you are limited to non-binary input, how you can obtain the binary value so that you can work with it.+lab46:~/src/c4eng/cnv0$ /var/public/fall2019/c4eng/cnv0/ref_cnv0 
 +Enter a number: 6 
 +6 is a secondary number 
 +lab46:~/src/c4eng/cnv0$  
 +</cli> 
 +=====Verification===== 
 +In additionI have also placed a **cnv0verify** script in that same subdirectory, which will test your program against a range of values, to determine overall correctness.
  
-There are a couple ways to go about this: +<cli> 
- +lab46:~/src/c4eng/cnv0$ /var/public/fall2019/c4eng/cnv0/cnv0verify 
-====Method oneconvert from decimal to binary==== +[  1] you have: errshould beerr    [  2] you have no, should be:  no 
-Using the division method, you can convert a decimal value to binarycontinually dividing the value (or its quotient) by the base, until the quotient is 0, then we use the remainders to give us the binary value: + 3] you have:  no, should be:  no    [  4] you haveyes, should beyes 
- +[  5] you have:  no, should be:  no    [  6] you haveyes, should beyes 
-  * value / base +[  7] you have no, should be:  no    [  8] you haveyes, should beyes 
-  * value = 15 + 9] you haveyes, should be: yes    [ 10] you haveyes, should be: yes 
-  * value / 2 +[ 11] you have:  no, should be:  no    [ 12] you have no, should be:  no 
-    * ie15 / 2 +[ 13] you have:  no, should be no    [ 14] you haveyes, should beyes 
-      * quotient+[ 15] you have: yesshould be: yes    [ 16] you have:  no, should be:  no 
-      * remainder: 1 +[ 17] you have:  no, should be:  no    [ 18] you have:  no, should be:  no 
-  * value = quotient +[ 19] you have:  no, should be:  no    [ 20] you have:  noshould be:  no 
-  * value / 2 +[ 21] you have: yesshould be: yes    [ 22] you have: yesshould be: yes 
-    * ie: 7 / 2 +[ 23] you have:  noshould be:  no    [ 24] you have:  no, should be:  no 
-      * quotient: 3 +[ 25] you have: yes, should be: yes    [ 26] you have: yesshould be: yes 
-      * remainder+[ 27] you have: yesshould beyes    28you have:  no, should be:  no 
-  * value = quotient +29you have:  no, should be:  no    30you have:  no, should be:  no 
-  * value / 2 +31you have:  no, should be:  no    32you have:  noshould be:  no 
-    * ie3 / 2 +[ 33] you have: yes, should be: yes    [ 34] you have: yes, should be: yes 
-      * quotient1 +[ 35] you have: yesshould be: yes    36you have:  no, should be:  no 
-      * remainder+lab46:~/src/c4eng/cnv0$  
-  * value = quotient +</cli>
-  * value / 2 +
-    * ie1 / 2 +
-      * quotient0 +
-      * remainder+
- +
-The binary for 15 (decimal) is 1111 (binary) +
- +
-Or: +
-  * value = 11 +
-  * value / 2 +
-    * quotient+
-    * remainder1 +
-  * value = 5 +
-  * value / 2 +
-    * quotient+
-    * remainder1 +
-  * value = 2 +
-  * value / 2 +
-    * quotient+
-    * remainder+
-  * value = 1 +
-  * value / 2 +
-    * quotient+
-    * remainder+
- +
-The binary for 11 (decimal) is 1011 (binary). +
- +
-NOTE that the order in which we get the remainders produces the number from right to left. +
- +
-====Method 2bitwise AND the place values==== +
-If one understands the weight values corresponding with the places of each bit in a binary numberwe can simply do a bitwise AND and see if the result is greater than 0 or not: +
- +
- 2 to the 7   2 to the 6   2 to the 5  ^  2 to the 4  ^  2 to the 3  ^  2 to the 2  ^  2 to the 1  ^  2 to the 0  ^ +
- 128   64  |  32  |  16  |  8  |  4  |  2  |  1  | +
- +
-Soif we wanted to see if the 2 to the 7th bit is active, we can simply: +
- +
-<code c> +
-    value  = number & 128; +
-</code> +
- +
-We can then check of value is greater than 0; if it is, we've got a 1 in that position, if it isn't, we have a 0. +
- +
-We can then repeat the operation for 643216, etc. down to 1, to get each bit of our number. +
- +
-NOTE that I have only taken us out to 8-bits. You may need to extend this to incorporate all the allowed values for this project. +
- +
-====Using Arrays==== +
-Depending on the approach taken, you might have a need to store each of the bits for later display (for exampleif you produce the bits in reverse order to the manner you wish to display them). +
- +
-An array is like a regular variable, although instead of being able to store exactly one value, it acts as a container for MANY variables (all of the same type). We can then address each value through an offset (0 being the first slot, 1 being the second, etc.) +
- +
-===The utility of arrays=== +
-First, we need to identify a need; just as we needed to do with loops. +
- +
-Let's say we had the following standalone variables: +
- +
-<code c> +
-    int num1  = 13; +
-    int num2  = 73; +
-    int num3  = 26; +
-    int num4  = 57; +
-</code> +
- +
-We likely understand how to work with each of the four independent variablesbut we can't exactly automate accessing them, such as through a loop. +
- +
-This is where an array can come in handy. Witnessthe equivalent storage of numbers using an array: +
- +
-<code c> +
-    int num[4]; // declare a 4 element integer array +
-    num[0]  = 13; +
-    num[1]  = 73; +
-    num[2]  = 26; +
-    num[3]  = 57; +
-</code> +
- +
-What value does this offer us? Wellfor one, we can automate the access of the array. Let's say we wanted to display the array contents (we have to do so one element at a time): +
- +
-<code c> +
-    int index  = 0; +
-    int max    = 4; +
-    for (index = 0; index < max; index = index + 1) +
-    { +
-        fprintf (stdout"%d\n", num[index]); +
-    } +
-</code> +
- +
-Perhaps an array can be of some use in this project?+
 =====Submission===== =====Submission=====
 To successfully complete this project, the following criteria must be met: To successfully complete this project, the following criteria must be met:
  
-  * Code must compile cleanly (no warnings or errors)+  * Code must compile cleanly (no notes, warnings, nor errors)
   * Output must be correct, and match the form given in the sample output above.   * Output must be correct, and match the form given in the sample output above.
   * Code must be nicely and consistently indented   * Code must be nicely and consistently indented
Line 308: Line 198:
  
 <cli> <cli>
-$ submit c4eng bcf0 bcf0.c +$ submit c4eng cnv0 cnv0.c 
-Submitting c4eng project "bcf0": +Submitting c4eng project "cnv0": 
-    -> bcf0.c(OK)+    -> cnv0.c(OK)
  
 SUCCESSFULLY SUBMITTED SUCCESSFULLY SUBMITTED
Line 320: Line 210:
  
 <code> <code>
-78:bcf0:final tally of results (78/78) +78:cnv0:final tally of results (78/78) 
-*:bcf0:proper error checking and status reporting performed [13/13] +*:cnv0:proper error checking and status reporting performed [13/13] 
-*:bcf0:correct variable types and name lengths used [13/13] +*:cnv0:correct variable types and name lengths used [13/13] 
-*:bcf0:proper output formatting per specifications [13/13] +*:cnv0:proper output formatting per specifications [13/13] 
-*:bcf0:runtime tests of submitted program succeed [13/13] +*:cnv0:runtime tests of submitted program succeed [13/13] 
-*:bcf0:no negative compiler messages for program [13/13] +*:cnv0:no negative compiler messages for program [13/13] 
-*:bcf0:code is pushed to lab46 repository [13/13]+*:cnv0:code is pushed to lab46 repository [13/13]
 </code> </code>
  
haas/fall2019/c4eng/projects/cnv0.1570448803.txt.gz · Last modified: 2019/10/07 11:46 by wedge