User Tools

Site Tools


haas:spring2015:cprog:projects:mbe1

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:mbe1 [2015/02/06 19:09] – [Using loops and arrays together for universal harmony] wedgehaas:spring2015:cprog:projects:mbe1 [2015/02/07 13:31] (current) wedge
Line 6: Line 6:
 ~~TOC~~ ~~TOC~~
  
-======Project: MENTAL MATH - MULTIPLY BY 11 (mbe0)======+======Project: MENTAL MATH - MULTIPLY BY 11 (mbe1)======
  
 =====Objective===== =====Objective=====
Line 272: Line 272:
  
 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. 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.
-====Multiplying any single digit number by 11==== 
-This may be a pattern of which you are already aware- to multiply any single-digit number (base 10) by eleven, you simply duplicate the digit twice. 
  
-In the case of 1 x 11, we get: 11 
  
-For 2 x 11, we see22+====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, plusI wanted you to focus on the basics of variable usage and if-statements.
  
-For x 11, we have: 33+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).
  
-and this trick works all the way through 9 x 11, yielding: 99 +===3-digits (review)=== 
- +Again, to review, let'look at a 3-digit example. 123 x 11:
-====Multiplying any double digit number by 11==== +
-Here we do a pivot and then perform simple arithmetic to obtain the middle value. +
- +
-In the case of 10 x 11, we take 10 and pivot it, getting 1 and 0, respectively our first and last digit of our soon-to-be solution. +
- +
-To get the middle value, we add these two values together: 1+0=1 +
- +
-So, the result of 10 x 11 is: 1 (1+0) 0 +
-or: 110 +
- +
-Let's try it with 32 x 11: +
- +
-<code> +
-32 x 11 = 3 (3+2) 2 +
-        = 3 5     2 +
-        = 352 +
-</code> +
- +
-This is almost the entire process, but there's one other factor we need to be awareif the summing of the first and last values yields a value greater than 10, we must propagate the carry to the next digit to the left (i.e. the first digit)+
- +
-For example, let us take the maximum two digit value (99): +
- +
-Using this process as it has been described thus far, we would (incorrectly) get:  +
- +
-<code> +
-99 x 11 9 (9 + 9) 9 +
-        9 18      9 +
-        9189 +
-</code> +
- +
-But that would be incorrect mathematically. +
- +
-To compensate (or, to present the full rules for the trick), we take the sum of this result as the middle digit, and apply the carry to the next digit to the left, so: +
- +
-<code> +
-99 x 11 = 9     (9+9) 9 +
-        = (9+1) 8     9 +
-        = 10    8     9 +
-        = 1089 +
-</code> +
- +
-And we now have the correct result. +
- +
-As another example, let us look at 47 x 11: +
- +
-<code> +
-47 x 11 = 4     (4+7) 7 +
-        = (4+1) 1     7 +
-        = 5         7 +
-        = 517 +
-</code> +
- +
-Got it? Try it with some other examples. +
- +
-===sum vs. carry=== +
-In grade school, when learning to do arithmetic by hand (you still are taught how to do arithmetic by hand, right?), we first learned the concept of sum and carry. This bore value as we were applying this to place values of the number. +
- +
-Little did you know then, but you were learning the basics of effective numerical and logical problem solving within the domain of Computer Science in grade school! +
- +
-For example, in the case of the number 18, when dissecting the number into its place values, we have: +
- +
-  * one 10 +
-  * eight 1s +
- +
-In single digit terminology, 18 is expressed as sum of 8 with a carry of 1. We see this more clearly when producing the value, see the original equation of 9+9: +
- +
-<code> +
-  1   <-- carry (to be added to 10s position) +
-   9 +
- + 9 +
- ---- +
-    <-- sum (of 1s position) +
-</code> +
- +
-See what is happening here? The basis for adding multiple-digit numbersPerhaps it would make more sense if we showed how adding 9 + 9 was in fact adding two 2-digit numbers together: +
- +
-<code> +
-  1   <-- carry (to be added to 10s position) +
-  09 +
- +09 +
- ---- +
-    <-- sum (of 1s position) +
- +
-</code>  +
-Then we have the follow-up addition to determine the value of the 10s place: +
- +
-<code> +
-  1 +
-  0 +
- +0 +
- -- +
-  1  <-- sum (of 10s position) +
-</code> +
- +
-and we would technically have a resulting carry of 0 (but adding zero to any values gives us the value itself-- the so-called **additive identity property** we learned in math class). +
- +
-Once we are all said and done, we concatenate the tens and ones places together: +
- +
-1 (ten) and 8 (ones): 18 +
- +
-====Multiplying any three-digit number by 11==== +
-In this case we merely extend the pattern from double digits, rippling through a series of comparing each set of two consecutive digits. +
- +
-Let's look at 123 x 11:+
  
 <code> <code>
-123 x 11 = 1 (1 + 2) (2 + 3) 3 +123 x 11 = 1       (1 + 2) (2 + 3) 3 
-         = 1 3             3+         = (1 + 0) (3 + 0) 5        (what are those + 0's? Carry values.) 
 +         = 1       3             3
          = 1353          = 1353
 </code> </code>
Line 400: Line 295:
 <code> <code>
 567 x 11 = 5       (5 + 6) (6 + 7) 7 567 x 11 = 5       (5 + 6) (6 + 7) 7
-         = (5 + 1(+ 1) 3       7+         = (5)+1   (11)+1  (13)+0   the outside numbers are the carry values
          = 6                   7          = 6                   7
          = 6237          = 6237
Line 414: Line 309:
 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. 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 the program that will use the above method to compute the requested one-, two-, or three-digit value against multiplicand of 11 (without using any multiplication to obtain your result).+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 data type limitation... the maximum size of an int: **signed int**s can have a maximum value of 2.4 billion, so unless we change to a different data type (or different method of inputting the source number), this will be our limitation.
  
 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 integer value
-  * determine from the input if it is a one-, two-, or three-digit number+  * determine the number of digits of the inputted value (store this in a variable)
   * perform the correct algorithm against the input   * perform the correct algorithm against the input
   * propagate any carries   * propagate any carries
-  * output the final value +  * use an array (**digit**) to store individual digits from the number input 
-    * you can display each digit individually, or combine them into one variable and display that (whichever you prefer)+  * use another array (**result**) to store the digits of the result number, following manipulations 
 +    * hint: you will want to make the **result** array one element larger. Why is this? 
 +  * Display output showing aspects of the process (see example execution below) 
 +  * output the final value (by iterating through the array, displaying one value at a time)
  
 =====Execution===== =====Execution=====
Line 435: Line 352:
 Enter value: 31415926 Enter value: 31415926
 Digits detected: 8 Digits detected: 8
-result[0] = 6 + 
-result[1] = +Obtaining unique digits, storing in array... 
 +digit[0] = 6 
 +digit[1] = 2 
 +digit[2] = 9 
 +digit[3] = 5 
 +digit[4] = 1 
 +digit[5] = 4 
 +digit[6] = 1 
 +digit[7] = 3 
 + 
 +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 31415926 x 11 = 345575186
 lab46:~/src/cprog/mbe1$  lab46:~/src/cprog/mbe1$ 
Line 446: Line 384:
 lab46:~/src/cprog/mbe1$ ./mbe1 lab46:~/src/cprog/mbe1$ ./mbe1
 Enter value: 7104 Enter value: 7104
 +Digits detected: 4
 +
 +Obtaining unique digits, storing in array...
 +digit[0] = 4
 +digit[1] = 0
 +digit[2] = 1
 +digit[3] = 7
 +
 +Applying process...
 +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 7104 x 11 = 78144
 lab46:~/src/cprog/mbe1$  lab46:~/src/cprog/mbe1$ 
Line 455: Line 409:
 lab46:~/src/cprog/mbe1$ ./mbe1 lab46:~/src/cprog/mbe1$ ./mbe1
 Enter value: 56789 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 56789 x 11 = 624679
 lab46:~/src/cprog/mbe1$  lab46:~/src/cprog/mbe1$ 
Line 460: Line 432:
  
 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.
-=====Reflection===== 
-Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project. 
- 
-  * Does this process work for four digit numbers? 
-  * How about five digit numbers? 
-  * Do you see a pattern for now this trick could be extended? 
  
 =====Submission===== =====Submission=====
Line 471: Line 437:
  
   * Code must compile cleanly (no warnings or errors)   * Code must compile cleanly (no warnings or errors)
-  * Output must be correct, and match the form given in the sample output above.+  * Output must be correct, and resemble the form given in the sample output above.
   * Code must be nicely and consistently indented (you may use the **indent** tool)   * Code must be nicely and consistently indented (you may use the **indent** tool)
   * Code must utilize the algorithm presented above   * Code must utilize the algorithm presented above
Line 477: Line 443:
     * have a properly filled-out comment banner at the top     * have a properly filled-out comment banner at the top
     * have at least 20% of your program consist of **<nowiki>//</nowiki>**-style descriptive comments     * have at least 20% of your program consist of **<nowiki>//</nowiki>**-style descriptive comments
-  * Output Formatting (including spacing) of program must conform to the provided output (see above). 
   * Track/version the source code in a repository   * Track/version the source code in a repository
   * Submit a copy of your source code to me using the **submit** tool.   * Submit a copy of your source code to me using the **submit** tool.
Line 484: Line 449:
  
 <cli> <cli>
-$ submit cprog mbe1 multby11v2.c mbe1.c+$ submit cprog mbe1 mbe1.c
 Submitting cprog project "mbe1": Submitting cprog project "mbe1":
-    -> multby11v2.c(OK) 
     -> mbe1.c(OK)     -> mbe1.c(OK)
  
haas/spring2015/cprog/projects/mbe1.1423249782.txt.gz · Last modified: 2015/02/06 19:09 by wedge