This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:fall2019:c4eng:projects:cnv0 [2019/10/07 11:46] – [Specifications] wedge | haas: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 | + | To create a program that can calculate and determine the number of factor pairs of a given number, starting with values |
=====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 | + | |
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 // | + | * 14 % 2 == 0 |
* 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 // | + | * 14 % 7 == 0 |
* 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 | + | * to STDERR, prompt for the number (range appropriate of an unsigned |
* 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: | + | lab46: |
- | Enter starting value (0-1023): 12 | + | Enter a number: 6 |
- | Enter ending value (0-1023): 28 | + | 6 is a secondary number |
- | 0000001100 | + | lab46: |
- | 0000001101 | + | |
- | 0000001110 | + | |
- | 0000001111 | + | |
- | 0000010000 | + | |
- | 0000010001 | + | |
- | 0000010010 | + | |
- | 0000010011 | + | |
- | 0000010100 | + | |
- | 0000010101 | + | |
- | 0000010110 | + | |
- | 0000010111 | + | |
- | 0000011000 | + | |
- | 0000011001 | + | |
- | 0000011010 | + | |
- | 0000011011 | + | |
- | 0000011100 | + | |
- | lab46: | + | |
</ | </ | ||
- | 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: | + | lab46: |
- | Enter starting value (0-1023): 17 | + | Enter a number: 7 |
- | 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: | + | |
</ | </ | ||
- | ====Sample run with invalid input given (scenario 1)==== | + | ====Additional outputs==== |
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | Enter starting value (0-1023): 5543 | + | Enter a number: 8 |
- | ERROR: input value must be between 0-1023! | + | 8 is a secondary number |
- | lab46: | + | lab46:~/ |
+ | Enter a number: 16 | ||
+ | 16 is NOT a secondary number | ||
+ | lab46: | ||
+ | Enter a number: 21 | ||
+ | 21 is a secondary number | ||
+ | lab46: | ||
</ | </ | ||
- | ====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: | + | lab46: |
- | Enter starting value (0-1023): 255 | + | lab46: |
- | Enter ending value (0-1023): 7168 | + | |
- | ERROR: input value must be between 0-1023! | + | |
- | lab46: | + | |
</ | </ | ||
=====Reference===== | =====Reference===== | ||
- | In the C4ENG public directory, inside the **bcf0** subdirectory, | + | In the C4ENG public directory, inside the **cnv0** subdirectory, |
- | =====Obtaining binary values===== | + | < |
- | You might wonder how, when you are limited to non-binary input, how you can obtain the binary value so that you can work with it. | + | lab46: |
+ | Enter a number: 6 | ||
+ | 6 is a secondary number | ||
+ | lab46: | ||
+ | </ | ||
+ | =====Verification===== | ||
+ | In addition, I have also placed a **cnv0verify** script in that same subdirectory, | ||
- | There are a couple ways to go about this: | + | <cli> |
- | + | lab46:~/ | |
- | ====Method one: convert from decimal to binary==== | + | [ 1] you have: err, should be: err [ 2] you have: no, should be: |
- | Using the division method, | + | [ 3] you have: |
- | + | [ 5] you have: | |
- | * value / base | + | [ 7] you have: no, should be: |
- | * value = 15 | + | [ 9] you have: yes, should be: yes [ 10] you have: yes, should be: yes |
- | * value / 2 | + | [ 11] you have: no, should be: no [ 12] you have: no, should be: |
- | * ie: 15 / 2 | + | [ 13] you have: no, should be: |
- | * quotient: 7 | + | [ 15] you have: yes, should be: yes [ 16] you have: |
- | * 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: no, should be: |
- | | + | [ 21] you have: yes, should be: yes [ 22] you have: yes, should be: yes |
- | * ie: 7 / 2 | + | [ 23] you have: no, should be: |
- | * quotient: | + | [ 25] you have: yes, should be: yes [ 26] you have: yes, should be: yes |
- | * remainder: 1 | + | [ 27] you have: yes, should be: yes |
- | * value = quotient | + | [ 29] you have: no, should be: no |
- | * value / 2 | + | [ 31] you have: no, should be: no |
- | * ie: 3 / 2 | + | [ 33] you have: yes, should be: yes [ 34] you have: yes, should be: yes |
- | * quotient: 1 | + | [ 35] you have: yes, should be: yes |
- | * remainder: 1 | + | lab46: |
- | * value = quotient | + | </cli> |
- | * value / 2 | + | |
- | * ie: 1 / 2 | + | |
- | * quotient: 0 | + | |
- | * remainder: 1 | + | |
- | + | ||
- | The binary for 15 (decimal) is 1111 (binary) | + | |
- | + | ||
- | Or: | + | |
- | * value = 11 | + | |
- | * value / 2 | + | |
- | * quotient: 5 | + | |
- | * remainder: 1 | + | |
- | | + | |
- | * value / 2 | + | |
- | * quotient: 2 | + | |
- | * remainder: 1 | + | |
- | | + | |
- | * value / 2 | + | |
- | * quotient: 1 | + | |
- | * remainder: 0 | + | |
- | * value = 1 | + | |
- | | + | |
- | * quotient: 0 | + | |
- | * remainder: 1 | + | |
- | + | ||
- | 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 2: bitwise AND the place values==== | + | |
- | If one understands the weight values corresponding with the places of each bit in a binary number, we 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 | | + | |
- | + | ||
- | So, if we wanted to see if the 2 to the 7th bit is active, we can simply: | + | |
- | + | ||
- | <code c> | + | |
- | value = number & 128; | + | |
- | </ | + | |
- | + | ||
- | 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 64, 32, 16, 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, | + | |
- | + | ||
- | 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; | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | We likely understand how to work with each of the four independent variables, but we can't exactly automate accessing them, such as through a loop. | + | |
- | + | ||
- | This is where an array can come in handy. Witness, the equivalent storage of numbers using an array: | + | |
- | + | ||
- | <code c> | + | |
- | int num[4]; // declare a 4 element integer array | + | |
- | num[0] | + | |
- | num[1] | + | |
- | num[2] | + | |
- | num[3] | + | |
- | </ | + | |
- | + | ||
- | What value does this offer us? Well, for 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; | + | |
- | | + | |
- | for (index = 0; index < max; index = index + 1) | + | |
- | { | + | |
- | fprintf (stdout, " | + | |
- | } | + | |
- | </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 | + | * Code must compile cleanly (no notes, |
* 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: | ||
< | < | ||
- | 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] |
</ | </ | ||