Corning Community College
CSCS1320 C/C++ Programming
~~TOC~~
======Project: MENTAL MATH - MULTIPLY BY 11 (mbe0)======
=====Objective=====
To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of multiplying any one-, two-, or three-digit number by eleven.
=====Prerequisites/Corequisites=====
In addition to the new skills required on previous projects, to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
* ability to add single-digit numbers together and determine their sum and carry
* ability to make decisions (if statement)
* some may have experience, others may have had it suggested as a means of solving the problem "better", but PLEASE: do NOT use any loops in the central solving of this project. It is important to develop an appreciation of the process (including how redundant it can get) so we can make more informed choices on how to better optimize the code. Don't worry, there will be plenty of time for loops (such as next week's project).
=====Scope=====
The allure of using (and learning) a programming language is to be able to effectively use it to solve problems, which in and of themselves are simulations of some process we can do in "the real world".
In this case, we will be writing a program which will implement the mental math techniques for multiplying any one-, two-, or three-digit number by eleven.
=====Background=====
Mental Math constitutes an intersection of mental tricks and math- instead of utilizing a purely math-only solution, manipulations or simplifications in the computational process may take place enabling an individual to, once having learned the process, solve such problems in their head, and typically without the use of a calculating device.
The process in this case is one of pattern matching, number manipulation, and simple arithmetic. To wit:
====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 see: 22
For 3 x 11, we have: 33
and this trick works all the way through 9 x 11, yielding: 99
====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:
32 x 11 = 3 (3+2) 2
= 3 5 2
= 352
This is almost the entire process, but there's one other factor we need to be aware- if 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:
99 x 11 = 9 (9 + 9) 9
= 9 18 9
= 9189
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:
99 x 11 = 9 (9+9) 9
= (9+1) 8 9
= 10 8 9
= 1089
And we now have the correct result.
As another example, let us look at 47 x 11:
47 x 11 = 4 (4+7) 7
= (4+1) 1 7
= 5 1 7
= 517
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 a sum of 8 with a carry of 1. We see this more clearly when producing the value, see the original equation of 9+9:
1 <-- carry (to be added to 10s position)
9
+ 9
----
8 <-- sum (of 1s position)
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)
09
+09
----
8 <-- sum (of 1s position)
Then we have the follow-up addition to determine the value of the 10s place:
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 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:
123 x 11 = 1 (1 + 2) (2 + 3) 3
= 1 3 5 3
= 1353
And digit-based additions that generate a carry are similarly propagated.
567 x 11:
567 x 11 = 5 (5 + 6) (6 + 7) 7
= (5 + 1) (1 + 1) 3 7
= 6 2 3 7
= 6237
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.
=====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 a multiplicand of 11 (without using any multiplication to obtain your result).
Your program should:
* obtain its input from STDIN.
* 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
* perform the correct algorithm against the input
* propagate any carries
* output the final value
* you can display each digit individually, or combine them into one variable and display that (whichever you prefer)
=====Execution=====
Several operating behaviors are shown as examples.
A two digit value:
lab46:~/src/cprog/mbe0$ ./multby11
Enter value: 32
32 x 11 = 352
lab46:~/src/cprog/mbe0$
Next, a one digit value:
lab46:~/src/cprog/mbe0$ ./multby11
Enter value: 7
7 x 11 = 77
lab46:~/src/cprog/mbe0$
Finally, three digit value:
lab46:~/src/cprog/mbe0$ ./multby11
Enter value: 567
567 x 11 = 6237
lab46:~/src/cprog/mbe0$
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=====
To successfully complete this project, the following criteria must be met:
* Code must compile cleanly (no warnings or errors)
* Output must be correct, and match the form given in the sample output above.
* Code must be nicely and consistently indented (you may use the **indent** tool)
* Code must utilize the algorithm presented above
* Code must be commented
* have a properly filled-out comment banner at the top
* have at least 20% of your program consist of **//**-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
* Submit a copy of your source code to me using the **submit** tool.
To submit this program to me using the **submit** tool, run the following command at your lab46 prompt:
$ submit cprog mbe0 multby11.c
Submitting cprog project "mbe0":
-> multby11.c(OK)
SUCCESSFULLY SUBMITTED
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.