User Tools

Site Tools


haas:fall2019:c4eng:projects:bcf0

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:bcf0 [2019/09/30 12:29] – [Sample run using multiplication] wedgehaas:fall2019:c4eng:projects:bcf0 [2019/10/05 20:16] (current) – [Method 2: bitwise AND the place values] wedge
Line 13: Line 13:
  
 =====Program===== =====Program=====
-It is your task to write a program that, upon accepting various pieces of input from the user, acts as a binary counter, displaying the resultant values in binary as from a starting to and ending progression.+It is your task to write a program that, upon accepting various pieces of input from the user, acts as a binary counter, displaying the resultant values in binary as from a starting to an ending progression.
  
 =====Specifications===== =====Specifications=====
Line 23: Line 23:
   * 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 starting value (0-1023)   * to STDERR, prompt for the starting value (0-1023)
-    * properly store this in a variable of type **unsigned short int**+    * properly store this in a variable of type **signed short int**
   * to STDERR, prompt for the ending value (0-1023)   * to STDERR, prompt for the ending value (0-1023)
-    * properly store this in a variable of type **unsigned short int**+    * properly store this in a variable of type **signed short int**
   * immediately after each input, check to make sure starting and ending values fall appropriately in the range stated; if in violation, display an error (to STDERR) and exit with a non-zero value.   * immediately after each input, check to make sure starting and ending values fall appropriately in the range stated; if in violation, display an error (to STDERR) and exit with a non-zero value.
   * check the starting and ending values to determine if we are to perform an incrementation or a decrementation.   * check the starting and ending values to determine if we are to perform an incrementation or a decrementation.
Line 40: Line 40:
 ====Sample incrementation run==== ====Sample incrementation run====
 <cli> <cli>
 +lab46:~/src/c4eng/bcf0$ ./bcf0
 Enter starting value (0-1023): 12 Enter starting value (0-1023): 12
 Enter ending value (0-1023): 28 Enter ending value (0-1023): 28
Line 64: Line 65:
 The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate. The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.
  
-====Sample run using addition====+====Sample decrementation run====
 <cli> <cli>
-lab46:~/src/c4eng/mtf1$ ./mtf1 +lab46:~/src/c4eng/bcf0$ ./bcf0 
-Which operation: + +Enter starting value (0-1023): 17 
-Starting Row (0-9): 5 +Enter ending value (0-1023): 0 
-Ending Row (0-9): +0000010001 
-Starting Column (0-9): 2 +0000010000 
-Ending Column (0-9): 7 +0000001111 
- + |  2  3  4  5  6  7 +0000001110 
----+------------------- +0000001101 
- 5 |  7  8  9 10 11 12 +0000001100 
- 6 |  8  9 10 11 12 13 +0000001011 
- 7 |  9 10 11 12 13 14 +0000001010 
-lab46:~/src/c4eng/mtf1+0000001001 
 +0000001000 
 +0000000111 
 +0000000110 
 +0000000101 
 +0000000100 
 +0000000011 
 +0000000010 
 +0000000001 
 +0000000000 
 +lab46:~/src/c4eng/bcf0
 </cli> </cli>
  
-====Sample run using subtraction==== +====Sample run with invalid input given (scenario 1)====
 <cli> <cli>
-lab46:~/src/c4eng/mtf1$ ./mtf1 +lab46:~/src/c4eng/bcf0$ ./bcf0 
-Which operation: - +Enter starting value (0-1023): 5543 
-Starting Row (0-9): 2 +ERRORinput value must be between 0-1023! 
-Ending Row (0-9)+lab46:~/src/c4eng/bcf0
-Starting Column (0-9): 1 +
-Ending Column (0-9): 4 +
- - |  1  2  3  4 +
----+------------- +
- 2 |  1  0 -1 -2 +
- 3 |  2  1  0 -1 +
- 4 |  3  2  1  0 +
- 5 |  4  3  2  1 +
- 6 |  5  4  3  2 +
-lab46:~/src/c4eng/mtf1+
 </cli> </cli>
  
-====Sample run using division====+====Sample run with invalid input given (scenario 2)====
 <cli> <cli>
-lab46:~/src/c4eng/mtf1$ ./mtf1 +lab46:~/src/c4eng/bcf0$ ./bcf0 
-Which operation: / +Enter starting value (0-1023): 255 
-Starting Row (0-9): 6 +Enter ending value (0-1023): 7168 
-Ending Row (0-9): 9 +ERRORinput value must be between 0-1023! 
-Starting Column (0-9)+lab46:~/src/c4eng/bcf0
-Ending Column (0-9): 4 +
- / |  0  1  2  3  4 +
----+---------------- +
- 6 | NA  6  3  2  1 +
- 7 | NA  7  3  2  1 +
- 8 | NA  8  4  2  2 +
- 9 | NA  9  4  3  2 +
-lab46:~/src/c4eng/mtf1+
 </cli> </cli>
  
-====Sample run using remainder==== +=====Reference===== 
-<cli> +In the C4ENG public directoryinside the **bcf0** subdirectorywill be a copy of my implementation (in executable form)which abides by the project specifications. Please compare its output against that of your implementation.
-lab46:~/src/c4eng/mtf1$ ./mtf1 +
-Which operation: % +
-Starting Row (0-9): 5 +
-Ending Row (0-9): 8 +
-Starting Column (0-9): 1 +
-Ending Column (0-9): 5 +
- % |  1  2  3  4  5 +
----+---------------- +
- 5 |  0  1  2  1  0 +
- 6 |  0  0  0  2  1 +
- 7 |  0  1  1  3  2 +
- 8 |  0  0  2  0  3 +
-lab46:~/src/c4eng/mtf1$  +
-</cli> +
-====Sample run with invalid operation given==== +
-<cli> +
-lab46:~/src/c4eng/mtf1$ ./mtf1 +
-Which operation: x +
-ERROR: Must specify '+''-', '*''/'or '%'. +
-lab46:~/src/c4eng/mtf1$  +
-</cli>+
  
-====Sample run with invalid row configuration==== +=====Obtaining binary values===== 
-<cli> +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:~/src/c4eng/mtf1$ ./mtf1 +
-Which operation: + +
-Starting Row (0-9): 7 +
-Ending Row (0-9): 2 +
-ERROR: Starting row is greater than ending row +
-lab46:~/src/c4eng/mtf1$  +
-</cli>+
  
 +There are a couple ways to go about this:
  
-=====Reference===== +====Method one: convert from decimal to binary==== 
-In the C4ENG public directoryinside the **mtf1** subdirectorywill be copy of my implementation (in executable form), which abides by the project specificationsPlease compare its output against that of your implementation.+Using the division methodyou can convert a decimal value to binary, continually 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: 
 + 
 +  value / base 
 +  value = 15 
 +  value / 2 
 +    ie: 15 / 2 
 +      * quotient: 7 
 +      * remainder: 1 
 +  * value = quotient 
 +  * value / 2 
 +    * ie: 7 / 2 
 +      * quotient: 3 
 +      * remainder: 1 
 +  * value = quotient 
 +  * value / 2 
 +    * ie: 3 / 2 
 +      * quotient: 1 
 +      * remainder: 1 
 +  * value = quotient 
 +  * 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 = 5 
 +  * value / 2 
 +    * quotient: 2 
 +    * remainder: 1 
 +  * value = 2 
 +  * value / 2 
 +    * quotient: 1 
 +    * remainder: 0 
 +  * value = 1 
 +  * value / 2 
 +    * 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 numberwe can simply do 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; 
 +</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 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, you might have a need to store each of the bits for later display (for example, if you produce the bits in reverse order to the manner you wish to display them)
 + 
 +An array is like a regular variablealthough 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 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]  = 13; 
 +    num[1]  = 73; 
 +    num[2]  = 26; 
 +    num[3]  = 57; 
 +</code> 
 + 
 +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; 
 +    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:
Line 169: Line 239:
  
 <cli> <cli>
-$ submit c4eng mtf1 mtf1.c +$ submit c4eng bcf0 bcf0.c 
-Submitting c4eng project "mtf1": +Submitting c4eng project "bcf0": 
-    -> mtf1.c(OK)+    -> bcf0.c(OK)
  
 SUCCESSFULLY SUBMITTED SUCCESSFULLY SUBMITTED
Line 181: Line 251:
  
 <code> <code>
-78:mtf1:final tally of results (78/78) +78:bcf0:final tally of results (78/78) 
-*:mtf1:proper error checking and status reporting performed [13/13] +*:bcf0:proper error checking and status reporting performed [13/13] 
-*:mtf1:correct variable types and name lengths used [13/13] +*:bcf0:correct variable types and name lengths used [13/13] 
-*:mtf1:proper output formatting per specifications [13/13] +*:bcf0:proper output formatting per specifications [13/13] 
-*:mtf1:runtime tests of submitted program succeed [13/13] +*:bcf0:runtime tests of submitted program succeed [13/13] 
-*:mtf1:no negative compiler messages for program [13/13] +*:bcf0:no negative compiler messages for program [13/13] 
-*:mtf1:code is pushed to lab46 repository [13/13]+*:bcf0:code is pushed to lab46 repository [13/13]
 </code> </code>
  
haas/fall2019/c4eng/projects/bcf0.1569846586.txt.gz · Last modified: 2019/09/30 12:29 by wedge