User Tools

Site Tools


haas:spring2014:cprog:projects:vertcross

Differences

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

Link to this comparison view

Next revision
Previous revision
haas:spring2014:cprog:projects:vertcross [2014/02/20 17:48] – created wedgehaas:spring2014:cprog:projects:vertcross [2014/03/13 09:42] (current) – [Multiplying two double digit numbers together] wedge
Line 45: Line 45:
  (4x3) (7x4)+(1x3) (1x7)    multiply each column verically (1x7 gets us 7)  (4x3) (7x4)+(1x3) (1x7)    multiply each column verically (1x7 gets us 7)
    12    28 +  3          between the two verticals is our crosswise    12    28 +  3          between the two verticals is our crosswise
-   12    31          7+   12      31        7
        
-    2              7      these are the place sums from the raw operations +    2              7      these are the place sums from the raw operations 
-                0       there are the carry results from the raw operations+                0       there are the carry results from the raw operations
        
-    2                   the carries are then applied in an addition operation +    2                   the carries are then applied in an addition operation 
-   13                      to the next place to the left +   13                         to the next place to the left 
-   -------------- +   --------------------- 
-   15         7 = 1517    and that result (sans any further carries) is our answer+   15              7 = 1517  and that result (sans any further carries) is our answer
 </code> </code>
  
Line 72: Line 72:
      ---------------------      ---------------------
       4       (19+2)     1       4       (19+2)     1
 +      
       4       21         1       4       21         1
              
       4        1         1       4        1         1
               2         0               2         0
 +              
 +      4+2      1         1
 +      
 +      6        1         1
                              
               = 611               = 611
Line 82: Line 87:
 Got it? Try it with some other examples. 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. 
  
-For example, in the case of the number 18when dissecting the number into its place values, we have:+====Multiplying two three-digit numbers together==== 
 +In this case we merely extend the pattern of vertically and crosswisenamely:
  
-  * one 10 +  * multiply right-most column vertically (ones place) 
-  * eight 1s+  * multiply crosswise the two right-most columns (tens place) 
 +  * multiply crosswise the left- and right-most columns, vertically multiply the middle (hundreds) 
 +  * multiply crosswise the two left-most columns (thousands) 
 +  * multiply vertically the left-most column (ten thousands)
  
-In single digit terminology, 18 is expressed as a sum of 8 with a carry of 1. We see this more clearly when producing the valuesee the original equation of 9+9:+Note the stretching/expanding approach that is taken here-- with each successive place, the range of columns involved in that position's calculation grows (including an additional column to the left with each position)until all columns are involved, then we shrink it progressively one column at a time (removing columns from the right) as we wane down to a single column on the left.
  
-<code> +Let's look at 123 x 234:
-  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 numbers. Perhaps it would make more sense if we showed how adding 9 + 9 was in fact adding two 2-digit numbers together:+
  
 <code> <code>
-  1   <-- carry (to be added to 10s position+   123 
-  09 + x 234 
- +09 + -------        carry_in 
- ---- +     3x4          [+ 0] ones place    (sum of 2 carry out of 1
-    <-- sum (of 1s position)+    2x4 + 3x3      + 1  tens place    (sum of 8 carry out of 1) 
 +   4x1 2x3 + 2x3 + 1  hundreds      (sum of 7 carry out of 1) 
 +  1x3 + 2x2        + 1  thousands     (sum of 8 carry out of 0) 
 + 2x1               + 0  ten thousands (sum of 2 carry out of 0)
  
-</code>  +  We then take the sums from each operation and place them as appropriate 
-Then we have the follow-up addition to determine the value of the 10s place:+ in their position in the final number (ones place is furthest on the right).
  
-<code> + 123 x 234 = 28782
-  +
-  0 +
- +0 +
- -- +
-  1  <-- sum (of 10s position)+
 </code> </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).+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. 
 +====Another depiction==== 
 +Here is another, more illustrative, method of solving a problem using the vertical and crosswise method:
  
-Once we are all said and done, we concatenate the tens and ones places together:+{{ :haas:spring2014:cprog:projects:step1.jpg |}}
  
-1 (ten) and 8 (ones)18+{{ :haas:spring2014:cprog:projects:step2.jpg |}}
  
-====Multiplying two three-digit numbers together==== +{{ :haas:spring2014:cprog:projects:step3.jpg |}}
-In this case we merely extend the pattern of vertically and crosswise, namely:+
  
-  * there are 3 columns to multiply vertically +{{ :haas:spring2014:cprog:projects:step4.jpg |}}
-  * there are 2 crosswise operations to perform+
  
-Let's look at 123 x 234:+{{ :haas:spring2014:cprog:projects:step5.jpg |}}
  
-<code> +{{ :haas:spring2014:cprog:projects:step6.jpg |}}
-   123 +
- x 234 +
- ------- +
-  (1x2)         (2x3)         (3x4)    the vertical multiplications +
-      (1x3)+(2x2)   (2x4)+(3x3)        the crosswise operations +
-             (1x4)+(2x3)               an outer-most crosswise operation +
-  --------------------------------- +
-    02   06    12                      as this is a 3 digit problem, we need 2 places +
-      3+4   8+9 +
-         4+6 +
-  ------------------ +
-    020612 +
-     0717 +
-      10 +
-    ------ +
-    028782 = 28782 (as a leading zero does not impact the value at all) +
-</code>+
  
-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.+{{ :haas:spring2014:cprog:projects:step7.jpg |}}
 =====Program===== =====Program=====
 It is your task to write the program that will use the above method to compute the requested two- and three-digit values together. It is your task to write the program that will use the above method to compute the requested two- and three-digit values together.
Line 161: Line 141:
     * if there is only 1 argument, ask the user for the second number     * if there is only 1 argument, ask the user for the second number
     * if there are no arguments, ask the user for both numbers     * if there are no arguments, ask the user for both numbers
-  * determine from the input if it is a one-, two-, or three-digit number +  * determine from the input the quantity of digits in each number  
-    * assume for one-digit there are leading zeros to match it with the biggest number +    * add any leading zeros in order to match the length of both numbers 
-  * perform the correct algorithm against the input +  * perform the correct algorithm (vertical and crosswise) against the input 
-  * display some semblance of the intermediate steps involved+  * implement said algorithm in a function that is called
   * propagate any carries   * propagate any carries
   * output the final value   * output the final value
  
 =====Execution===== =====Execution=====
-Several operating behaviors are shown, namely, with and without command-line arguments and 1-, 2-, and 3-digit values.+Several operating behaviors are shown, namely, with and without command-line arguments and 2-, and 3-digit values.
  
 First up, two-digit values without argument: First up, two-digit values without argument:
Line 177: Line 157:
 Enter number1: 34 Enter number1: 34
 Enter number2: 26 Enter number2: 26
- 
-        34 
-       x26 
-       ---- 
-       0624 
-        26 
-       ---- 
-       0884  
- 
 34 x 26 = 884 34 x 26 = 884
- 
 lab46:~/src/cprog/vertcross$  lab46:~/src/cprog/vertcross$ 
 </cli> </cli>
Line 196: Line 166:
 lab46:~/src/cprog/vertcross$ ./vertcross 137 lab46:~/src/cprog/vertcross$ ./vertcross 137
 Enter number2: 32 Enter number2: 32
- 
-   137 
-  x032 
-  ------ 
-  000914 
-   0327 
-    02 
-  ------ 
-  004384 
- 
 137 x 032 = 4384 137 x 032 = 4384
- 
 lab46:~/src/cprog/vertcross$  lab46:~/src/cprog/vertcross$ 
 </cli> </cli>
  
-=====Reflection===== 
-Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project. 
- 
-  * Can this process be extended for four digit numbers? 
-  * How about five digit numbers? 
  
 =====Submission===== =====Submission=====
Line 224: Line 178:
   * 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 (no "cheating")   * Code must utilize the algorithm presented above (no "cheating")
 +  * algorithm must be located within a specific function and called during execution
   * Code must be commented   * Code must be commented
     * have a properly filled-out comment banner at the top     * have a properly filled-out comment banner at the top
haas/spring2014/cprog/projects/vertcross.1392918509.txt.gz · Last modified: 2014/02/20 17:48 by wedge