User Tools

Site Tools


Sidebar

projects

wcp1 (due 20240124)
pct0 (bonus; due 20240125)
pct1 (bonus; due 20240125)
abc0 (due 20240131)
pct2 (due 20240131)
wcp2 (due 20240131)
dac0 (due 20240207)
pct3 (bonus; due 20240207)
wcp3 (due 20240207)
gtf0 (due 20240214)
pct4 (due 20240214)
wcp4 (due 20240214)
bwp1 (bonus; due 20240228)
pct5 (bonus; due 20240228)
usr0 (due 20240228)
wcp5 (due 20240228)
gfo0 (due 20240306)
pct6 (due 20240306)
tpb0 (due 20240306)
wcp6 (due 20240306)
pct7 (bonus; due 20240313)
tpb1 (due 20240313)
wcp7 (due 20240313)
pct8 (due 20240320)
tpb2 (due 20240320)
wcp8 (due 20240320)
pct9 (bonus; due 20240327)
wcp9 (due 20240327)
bwp2 (bonus; due 20240410)
fwg0 (due 20240410)
gfo1 (due 20240410)
pctA (due 20240410)
wcpA (due 20240410)
wpa0 (due 20240410)
pctB (bonus; due 20240417)
upf0 (due 20240417)
wcpB (due 20240417)
ldg0 (due 20240424)
pctC (due 20240424)
wcpC (due 20240424)
pctD (bonus; due 20240501)
wcpD (bonus; due 20240501)
gfo2 (due 20240508)
pctE (bonus; due 20240508)
wcpE (bonus; due 20240508)
EoCE (due 20240516)
haas:spring2024:unix:projects:permcombo

Project: MENTAL MATH (MULTIPLY BY 11)

A project for C/C++ Programming.

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

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 implement and use command-line arguments
  • ability to access and manipulate arrays

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, textual 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, string 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.

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 the command-line (argument 1, which will be processed against 11)
    • if there is no first argument, fall back to asking the user for input
  • 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

Execution

Several operating behaviors are shown, namely, with and without command-line arguments and 1-, 2-, and 3-digit values.

First up, three digit value without argument:

lab46:~/src/cprog/multby11$ ./multby11
Enter value: 345
345 x 11 = 3795
lab46:~/src/cprog/multby11$ 

Second, a two digit value without argument:

lab46:~/src/cprog/multby11$ ./multby11
Enter value: 32
32 x 11 = 352
lab46:~/src/cprog/multby11$ 

Next, a one digit value with argument:

lab46:~/src/cprog/multby11$ ./multby11 7
7 x 11 = 77
lab46:~/src/cprog/multby11$ 

Finally, two digit value with argument:

lab46:~/src/cprog/multby11$ ./multby11 567
567 x 11 = 6237
lab46:~/src/cprog/multby11$ 

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)
  • Executed program must display a total of 1 or 2 lines (depending on presence of argument)
  • 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 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 multby11 multby11.c
Submitting cprog project "multby11":
    -> 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.

Verify submission

To verify you submitted successfully, you may run the following (from anywhere on lab46):

lab46:~$ verify cprog multby11
multby11: submitted successfully

Note if automated assessment is available for the project, you may actually see results in the output as well.

haas/spring2024/unix/projects/permcombo.txt · Last modified: 2014/01/17 12:12 by 127.0.0.1