User Tools

Site Tools


haas:spring2015:cprog:projects:afn0

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:spring2015:cprog:projects:afn0 [2015/02/25 14:06] – [arrays] wedgehaas:spring2015:cprog:projects:afn0 [2015/03/20 20:45] (current) – [Prerequisites/Corequisites] wedge
Line 15: Line 15:
  
   * can perform this trick in your head/by hand (if you can't do it on your own, you have no business trying to tell the computer how to do it)   * can perform this trick in your head/by hand (if you can't do it on your own, you have no business trying to tell the computer how to do it)
-  * understand the pattern/process to doing it for any length number (2-digit through 24-digdt)+  * understand the pattern/process to doing it for any length number (2-digit through 18-digdt)
   * ability to deploy loops to simplify your process   * ability to deploy loops to simplify your process
   * ability to use arrays to facilitate the storage of your processed values   * ability to use arrays to facilitate the storage of your processed values
Line 125: Line 125:
  
 This function takes two parameters, two pieces of input, available to us in the form of variables, by those names, of those types. We make use of them as we need to in accomplishing the program at hand. This function takes two parameters, two pieces of input, available to us in the form of variables, by those names, of those types. We make use of them as we need to in accomplishing the program at hand.
-====Using loops and arrays together for universal harmony==== 
  
-To really make the most out of arrays in scaling our algorithms, using them in conjunction with loops gives us the most bang for our buck. The advantage of arrays+loops is that with the ONE consistent variable name, representing many NUMERICALLY-identifiable elements, we can work with ranges of data sets without the need to make tons of exceptions for each possible use case (worst case we just make an array of the maximum expected sizeand only use what we need).+Sowhen we wish to create functions of our own, we need:
  
-===42 everywhere===+  * the return type 
 +  * the function name 
 +  * 0 or more parameters, identifying their order and type
  
-To illustratehere we will declare an 11 element array (called **data**), and fill each element with the value 42 using for loop:+For examplelet us make a sum() function. Here would be likely prototype (we'd place it above main()):
  
-<code c+<code> 
-int data[11]position = 0; +int sum(int *int);
- +
-for(position = 0; position < 11; position=position+1// see, using long form, could have done "position++" +
-+
-    data[position] = 42; +
-}+
 </code> </code>
  
-===Display array contents=== +A function prototype (vs. its definition) will have terminating semi-colonas you see above.
-What if we wanted to print the contents of the array? Once again, we use loopand print out each value, one at a time.+
  
-Important considerations: +In our case, our sum() function has the following:
-  * againwith C, being true to how the computer actually works, we can only access the array one element at a time +
-  * because we know array indices start at 0, we have a known starting point +
-  * because we know how big our array is (11 elements, from previous example), we know how many elements to go for +
-  * each element is located one after the other-- 0 is followed by 1 is followed by 2 etc.+
  
-... therefore, we have all the ingredients for a **for** loop:+  * return type of **int** (particular variable name doesn't matter, type does) 
 +  * the function's name (sum) 
 +  * a comma-separated list of types corresponding to the parameters (again, variable names do not matter, but the type is important).
  
-<code c> +Our sum() function will take an integer array (denoted by the int pointer above)and a size (the secondregular int).
-for (position = 0; position < 11; position++) +
-+
-    fprintf(stdout"%d ", data[position]); +
-+
-fprintf(stdout"\n");  // what important role does this line play? +
-</code>+
  
-This should result in the following program output: +Now, parameter order very much matters. In our case, an "int *" came first, followed by an "int"... we need to be mindful of this order to successfully call and use the function.
- +
-<cli> +
-42 42 42 42 42 42 42 42 42 42 42  +
-</cli>+
  
-===Backwards?=== +====Function definition==== 
-What if we wanted to display the contents of our array in reverse (from position 10 to position 9, to 8, down to 0)?+While a function prototype is technically optional (you can put the definition in place of the prototype-- we just often use prototypes to further allow organization), we MUST have a function definition. This is nothing short of the code that dictates what operations the function in question will perform.
  
-We'd still want to use a loop, but look at how we structure it:+Our sum() function will be defined (below the main() function) as follows:
  
 <code c> <code c>
-for (position = 10; position >= 0; position--)+int sum(int *array, int size)
 { {
-    fprintf(stdout, "%d ", data[position]);+    int result = 0; 
 +    int i = 0; 
 +     
 +    for (i = 0; i < size; i++) 
 +        result = result + array[i]
 +         
 +    return(result);
 } }
-fprintf(stdout, "\n");  // what important role does this line play? 
 </code> </code>
  
-Notice how the loop-terminating relational statements differ (comparing the two-- for forward and backwarddoes it make sense?)and also how we progress between individual elements (in one we are incrementingin this recent one we are decrementing).+====function calling==== 
 +Once we've declared (prototyped) and defined our functionnow all we have to do is use it! When you make use of a function, we refer to it as //calling//. We call the functionby name, providing and required parameters, and capturing any return value as we see fit.
  
-That should make sense before you try to proceed.+Here would be an example of calling the above-mentioned **sum()** function:
  
-===Thinking with arrays=== +<code c> 
-Using arrays in your algorithms represents a potential barrier you have to overcome. Up until this point, we've been getting used to labelling all our variables with unique, individual names.+int scores[4]; 
 +int tally 0;
  
-Now, with arrays, we have one common name, distinguishable by its element offset. That has been known to cause some conceptual problems due to the mildly abstract nature it creates. It would certainly not hurt to draw some pictures and manually work through some examples, step-by-step... it may be confusing at first, but the more you play with it, ask questions, play, read, etc., the sooner things will start to click.+scores[0] = 88; 
 +scores[1] = 47; 
 +scores[2] = 96; 
 +scores[3] = 73;
  
-As some of you have started to realize with **mbe0**, the actual core work of the project wasn't actually that hard, once you saw through the illusion of complexity we had placed in front of it. By using arrays, we can make our solutions even easier (and code even simpler)... but, we will initially have to eliminate self-imposed mental obstacles making the problem appear significantly more difficult than it actually is. +tally sum(scores, 4);
- +
- +
-====Multiplying a number (of varying digits) by 11==== +
-In **mbe0**we specifically looked at 3 usage cases for our mental math problem: 1-, 2-, and 3-digit number. I limited it to those because, lacking arrays and loops for that project, the code would have gotten impossibly long and complex, plus: I wanted you to focus on the basics of variable usage and if-statements. +
- +
-Now that we have those down, we can now apply arrays and loops to optimize and enhance a solution, and to allow it to scale to a wider range of possibilities (why limit ourselves to just 1-, 2-, and 3-digit values? Once we see the pattern, we can apply this to 4-, 5-, 6-digit numbers and beyond)+
- +
-===3-digits (review)=== +
-Again, to review, let's look at a 3-digit example. 123 x 11: +
- +
-<code> +
-123 x 11 = 1       (1 + 2) (2 + 3) 3 +
-         = (1 + 0) (3 + 0) 5        (what are those + 0's? Carry values.) +
-         = 1                   3 +
-         = 1353+
 </code> </code>
  
-And digit-based additions that generate a carry are similarly propagated.+Note, that it is rather important to match the type and order of parameters. Due to the nature of the array (especially the form of array declaration) used, certain pointer-related details are being hidden from us, giving somewhat of a false impression. Further discussion about pointers will begin to shed light on that.
  
-567 x 11: 
  
-<code> 
-567 x 11 = 5       (5 + 6) (6 + 7) 7 
-         = (5)+1   (11)+1  (13)+0  7  the outside numbers are the carry values 
-         = 6                   7 
-         = 6237 
-</code> 
- 
-When doing this, we need to evaluate the number from right to left (just as we would do it if we were to compute it purely mathematically by hand): 
- 
-  * We know the last digit (1s place) of 567 x 11 right off the bat: 7 
-  * The second digit (10s place) is the sum of 6 and 7 (6+7) which is 13 (sum of 3, carry of 1), so: 3 
-  * The third digit (100s place) is the sum of 5 and 6 plus any carry from the 10s place (which is 1), so (5+6+1) which is 12 (sum of 2, carry of 1), so: 2 
-  * The fourth digit (1000s place) is the original first value (5 of the 567) plus any carry from the 100s place (which there is, a 1), so (5+1) which yields a sum of 6, carry of 0. 
- 
-A dual benefit of this project is that in addition to extending your programming experience / understanding of C, you could develop this as a mental ability (that is where it originated), and you could then use it as a means of checking your work. 
- 
-===4-digits=== 
-Now let us process a 4-digit example (look for similarities to the 3-digit process, specifically how this is merely an expansion, or an additional step-- due to the additional digit): 
- 
-4567 x 11: 
- 
-<code> 
-4567 x 11 = 4       (4 + 5) (5  + 6) (6 + 7) 7 
-          = (4)+1   (9)+1   (11)+1   (13)+0  7   the numbers outside are the carry 
-          = 5                    3       7 
-          = 50237 
-</code> 
- 
-Remember, we are processing this from right to left (so that the carry values can properly propagate). While there is no initial carry coming in, we'll add one anyway (0), so we see 13+0 (which is simply 13)... but because we're interested in place values, this is actually a sum of 3, carry of 1... and that one gets sent over to the next place (which has an 11)... so 11+1 will be 12, or sum of 2, carry 1... that carry will propagate to the next position to the left (the 9)... so there's a rippling effect taking place (math in action). 
- 
-Can you see how "the same" this process for 4-digit numbers is when comparing to the process for 3-digit numbers? And how the same comparison can be made for 2-digit, and 5-digit, 6-digit, etc.? Please take some time, working through some examples (by hand) to identify and notice the pattern, or essence, of this process. You need to see how it doesn't matter in the long run how many digits- because you're doing the same thing (just a different number of times). 
- 
-That "different number of times" will be based on the length of the number... could that be used to help us? 
- 
-(Also, the potential exception here would possibly be 1-digit values... if you cannot easily find a way to make 1-digit numbers work with greater-than-1-digit numbers, that's where an if-statement would come into play-- if 1-digit, do this specific process, else do the regular process). I'm not saying one universal solution isn't possible, but at this stage of your structured programming development, such solutions may take a bit more work (and that's okay). 
 =====Program===== =====Program=====
-It is your task to write an optimized version of your multiply by eleven program that will use arrays and loops to enable you to enhance and expand the functional capabilities of your program. No longer will you be limited by 1-2-, or 3-digit numbers, but you will be able to input up to 8-digit numbers and have your program successfully determine the result (and 8 is merely an arbitrary value I picked, you should easily be able to up it to the tens of thousands and experience no change in functionality-- actually, our 8-digit limit is considering a data type limitation... the maximum size of an int: **signed int**s can have a maximum value of 2.4 billionso unless we change to a different data type (or different method of inputting the source number)this will be our limitation.+It is your task to write program that obtains a long integer value from the user, and processes that single value into separate array elements (one digit per array element). Determining the number of digitsyou are to perform this "all from nine, last from ten" subtraction method on the number using array transactionsdisplaying a visual representation of the problem being solved to STDOUT.
  
 Your program should: Your program should:
   * obtain its input from STDIN.   * obtain its input from STDIN.
-    * input should be in the form of a single integer value+    * input should be in the form of a single (long) long integer value (you want a 64-bit data type)
   * determine the number of digits of the inputted value (store this in a variable)   * determine the number of digits of the inputted value (store this in a variable)
-  * perform the correct algorithm against the input +  * process that input long integer into separate array elements- one digit per element. 
-  * propagate any carries +    * you may assume a maximum array size of the maximum number of digits you're theoretically able to input that can be stored in a 64-bit value. 
-  * use an array (**digit**) to store individual digits from the number input +  * perform the "all from nine, the last from ten" operation on the array, storing the result in another array. 
-  * use another array (**result**) to store the digits of the result number, following manipulations +  * display the problem being solved, along with the answer 
-    * hint: you will want to make the **result** array one element largerWhy is this? +  * use functions to modularize your code: 
-  Display output showing aspects of the process (see example execution below) +    * have an **longint2array()** function that takes the long int, and returns an array (the function itself handles the processing of splitting up the long int into individual digits). 
-  output the final value (by iterating through the array, displaying one value at a time)+    * have a **printarray()** function, whose responsibility it is to display the indicated array to STDOUT
 +    have a **allfromnine()** function that takes the source array, does the processing, and returns ther result array.
  
-=====Execution===== +I might suggest the following function prototypes:
-Several operating behaviors are shown as examples.+
  
-An eight digit value:+<code c> 
 +unsigned char *longint2array(unsigned long int); 
 +void printarray(unsigned char *, unsigned char); 
 +unsigned char *allfromnine(unsigned char *); 
 +</code>
  
-<cli> +Some questions to contemplate:
-lab46:~/src/cprog/mbe1$ ./mbe1 +
-Enter value: 31415926 +
-Digits detected: 8+
  
-Obtaining unique digits, storing in array... +  * Why an array of unsigned chars when we're starting with a long (long) int? 
-digit[0] = 6 +    * Why is that the "best fit" size-wise? 
-digit[1] = 2 +    * Why will that not result in lost data? 
-digit[2] = 9 +  * Why unsigned? 
-digit[3] = 5 +    * What impact will that have on our input value's upper bound? 
-digit[4] = 1 +  * Why represent the size of the usable array as an unsigned char? 
-digit[5] = 4 +    * Why is this the "best fit" size-wise? 
-digit[6] +=====Execution===== 
-digit[7] +An example of your program in action:
- +
-Applying process... +
-result[0] 6 + 0 + 0 (sum of 6, carry out of 0) +
-result[1] 2 + 6 + 0 (sum of 8, carry out of 0) +
-result[2] 9 + 2 + 0 (sum of 1, carry out of 1) +
-result[3] 5 + 9 + 1 (sum of 5, carry out of 1) +
-result[4] 1 + 5 + 1 (sum of 7, carry out of 0) +
-result[5] 4 + 1 + 0 (sum of 5, carry out of 0) +
-result[6] 1 + 4 + 0 (sum of 5, carry out of 0) +
-result[7] 3 + 1 + 0 (sum of 4, carry out of 0) +
-result[8] = 3 + 0 + 0 (sum of 3, carry out of 0) +
- +
-Displaying result... +
-31415926 x 11 = 345575186 +
-lab46:~/src/cprog/mbe1$  +
-</cli> +
- +
-Next, a four digit value:+
  
 <cli> <cli>
-lab46:~/src/cprog/mbe1$ ./mbe1 +lab46:~/src/cprog/afn0$ ./afn0 
-Enter value: 7104 +Enter value: 31415926535897 
-Digits detected: 4+Digits detected: 14
  
-Obtaining unique digits, storing in array... + 100000000000000 
-digit[0] = 4 +- 31415926535897 
-digit[1] = 0 + --------------- 
-digit[2] = 1 +  68584073464102
-digit[3] = 7+
  
-Applying process... +lab46:~/src/cprog/afn0
-result[0] = 4 + 0 + 0 (sum of 4, carry out of 0) +
-result[1] = 0 + 4 + 0 (sum of 4, carry out of 0) +
-result[2] = 1 + 0 + 0 (sum of 1, carry out of 0) +
-result[3] = 7 + 1 + 0 (sum of 8, carry out of 0) +
-result[4] = 7 + 0 + 0 (sum of 7, carry out of 0) +
- +
-Displaying result... +
-7104 x 11 = 78144 +
-lab46:~/src/cprog/mbe1+
 </cli> </cli>
- 
-Finally, a five digit value: 
- 
-<cli> 
-lab46:~/src/cprog/mbe1$ ./mbe1 
-Enter value: 56789 
-Digits detected: 5 
- 
-Obtaining unique digits, storing in array... 
-digit[0] = 9 
-digit[1] = 8 
-digit[2] = 7 
-digit[3] = 6 
-digit[4] = 5 
- 
-Applying process... 
-result[0] = 9 + 0 + 0 (sum of 9, carry out of 0) 
-result[1] = 8 + 9 + 0 (sum of 7, carry out of 1) 
-result[2] = 7 + 8 + 1 (sum of 6, carry out of 1) 
-result[3] = 6 + 7 + 1 (sum of 4, carry out of 1) 
-result[4] = 5 + 6 + 1 (sum of 2, carry out of 1) 
-result[5] = 5 + 1 + 0 (sum of 6, carry out of 0) 
- 
-Displaying result... 
-56789 x 11 = 624679 
-lab46:~/src/cprog/mbe1$  
-</cli> 
- 
-The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate. 
  
 =====Submission===== =====Submission=====
Line 367: Line 253:
  
 <cli> <cli>
-$ submit cprog mbe1 mbe1.c +$ submit cprog afn0 afn0.c 
-Submitting cprog project "mbe1": +Submitting cprog project "afn0": 
-    -> mbe1.c(OK)+    -> afn0.c(OK)
  
 SUCCESSFULLY SUBMITTED SUCCESSFULLY SUBMITTED
haas/spring2015/cprog/projects/afn0.1424873202.txt.gz · Last modified: 2015/02/25 14:06 by wedge