This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2014:cprog:projects:vertcross [2014/02/22 12:55] – [Program] wedge | haas: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) | ||
| | ||
- | | + | |
- | 2 | + | 2 |
- | | + | |
- | 2 | + | 2 |
- | | + | |
- | | + | --------------------- |
- | | + | |
</ | </ | ||
Line 72: | Line 72: | ||
| | ||
4 | 4 | ||
+ | | ||
4 | 4 | ||
| | ||
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 18, when dissecting the number into its place values, we have: | + | ====Multiplying two three-digit numbers together==== |
+ | In this case we merely extend | ||
- | * 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, | + | Note the stretching/ |
- | < | + | Let's look at 123 x 234: |
- | 1 < | + | |
- | 9 | + | |
- | + 9 | + | |
- | | + | |
- | | + | |
- | </ | + | |
- | + | ||
- | 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: | + | |
< | < | ||
- | 1 <-- carry (to be added to 10s position) | + | 123 |
- | | + | x 234 |
- | +09 | + | ------- carry_in |
- | ---- | + | |
- | | + | 2x4 + 3x3 + 1 tens place (sum of 8 carry out of 1) |
+ | 4x1 + 2x3 + 2x3 + 1 hundreds | ||
+ | | ||
+ | | ||
- | </ | + | 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). |
- | < | + | 123 x 234 = 28782 |
- | 1 | + | |
- | 0 | + | |
- | +0 | + | |
- | -- | + | |
- | 1 <-- sum (of 10s position) | + | |
</ | </ | ||
- | 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 | + | A dual benefit |
+ | ====Another depiction==== | ||
+ | Here is another, more illustrative, | ||
- | Once we are all said and done, we concatenate the tens and ones places together: | + | {{ :haas: |
- | 1 (ten) and 8 (ones): 18 | + | {{ :haas: |
- | ====Multiplying two three-digit numbers together==== | + | {{ :haas: |
- | In this case we merely extend the pattern of vertically and crosswise, namely: | + | |
- | * there are 3 columns to multiply vertically | + | {{ : |
- | * there are 2 crosswise operations to perform | + | |
- | Let's look at 123 x 234: | + | {{ :haas: |
- | < | + | {{ : |
- | 123 | + | |
- | x 234 | + | |
- | | + | |
- | (1x2) | + | |
- | (1x3)+(2x2) | + | |
- | | + | |
- | --------------------------------- | + | |
- | 02 | + | |
- | 3+4 8+9 | + | |
- | 4+6 | + | |
- | ------------------ | + | |
- | 020612 | + | |
- | | + | |
- | 10 | + | |
- | ------ | + | |
- | 028782 = 28782 (as a leading zero does not impact the value at all) | + | |
- | </ | + | |
- | 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), | + | {{ : |
=====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 | + | * determine from the input the quantity of digits in each number |
* add any leading zeros in order to match the length of both numbers | * add any leading zeros in order to match the length of both numbers | ||
- | * perform the correct algorithm against the input | + | * perform the correct algorithm |
* implement said algorithm in a function that is called | * implement said algorithm in a function that is called | ||
* propagate any carries | * propagate any carries | ||
Line 169: | Line 149: | ||
=====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 | ||
- | ---- | ||
- | | ||
- | |||
34 x 26 = 884 | 34 x 26 = 884 | ||
- | |||
lab46: | lab46: | ||
</ | </ | ||
Line 196: | Line 166: | ||
lab46: | lab46: | ||
Enter number2: 32 | Enter number2: 32 | ||
- | |||
- | 137 | ||
- | x032 | ||
- | ------ | ||
- | 000914 | ||
- | 0327 | ||
- | 02 | ||
- | ------ | ||
- | 004384 | ||
- | |||
137 x 032 = 4384 | 137 x 032 = 4384 | ||
- | |||
lab46: | lab46: | ||
</ | </ | ||
- | =====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===== |