User Tools

Site Tools


Sidebar

projects

wcp1 (due 20240828)
wcp2 (due 20240904)
pct0 (bonus; due 20240905)
pct1 (bonus; due 20240905)
pct2 (due 20240905)
abc0 (due 20240906)
gtf0 (due 20240911)
pct3 (bonus; due 20240911)
wcp3 (due 20240911)
dtr0 (due 20240918)
pct4 (due 20240918)
wcp4 (due 20240918)
mmf0 (due 20240926)
pct5 (bonus; due 20240926)
wcp5 (due 20240926)
cnv0 (due 20241002)
gfo0 (due 20241002)
pct6 (due 20241002)
wcp6 (due 20241002)
fwg0 (due 20241009)
pct7 (bonus; due 20241009)
wcp7 (due 20241009)
bwp1 (bonus; due 20241016)
cnv1 (due 20241016)
pct8 (due 20241016)
wcp8 (due 20241016)
fwg1 (due 20241023)
pct9 (bonus; due 20241023)
wcp9 (due 20241023)
fwg2 (due 20241030)
gfo1 (due 20241030)
pctA (due 20241030)
wcpA (due 20241030)
fwg3 (due 20241106)
pctB (bonus; due 20241106)
wcpB (due 20241106)
oop0 (due 20241113)
pctC (due 20241113)
wcpC (due 20241113)
pctD (bonus; due 20241120)
wcpD (bonus; due 20241120)
bwp2 (bonus; due 20241204)
gfo2 (due 20241204)
pctE (bonus; due 20241204)
wcpE (bonus; due 20241204)
EoCE (due 20241216)
haas:fall2024:cprog:projects:mbe0

Corning Community College

CSCS1320 C/C++ Programming

Project: MENTAL MATH - MULTIPLY BY 11 (mbe0)

Errata

  • <description> (DATESTRING)

Objective

To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of multiplying any 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 or arrays 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).
  • and, as I've seen some try to compensate for with the restriction on loops: no goto statements either! If you already know how to use loops, great, show me you can confidently write code without redirecting program flow for the purposes of repetition.

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 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 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 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 unsigned short integer value
    • no string processing!
  • declare and utilize these 9 unsigned char variables, named and described as follows:
    • sum10000 (to store the 10000s place sum digit)
    • carry1000 (to store the 1000s place carry digit)
    • sum1000 (to store the 1000s place sum digit)
    • carry100 (to store the 100s place carry digit)
    • sum100 (to store the 100s place sum digit)
    • carry10 (to store the 10s place carry digit)
    • sum10 (to store the 10s place sum digit)
    • carry1 (to store the 1s place carry digit)
    • sum1 (to store the 1s place sum digit)
  • perform the described algorithm against the input and these sum/carry variables
  • all output except for final value goes to STDERR
  • output the final value to STDOUT
    • display each digit individually (all packed together)
  • no loops, repetition, recursion, nor goto statements

Execution

Several operating behaviors are shown as examples.

A two digit value with no carries:

lab46:~/src/cprog/mbe0$ ./mbe0
Enter value: 32
 32 x 11 =               3   (3+2)   2
         =               3     5     2
         = 352
lab46:~/src/cprog/mbe0$ 

A two digit value with carries:

lab46:~/src/cprog/mbe0$ ./mbe0
Enter value: 86
 86 x 11 =               8   (8+6)   6
         =               8    14     6
         =             (8+1)   4     6
         =               9     4     6
         = 946
lab46:~/src/cprog/mbe0$ 

A three digit value with no carries:

lab46:~/src/cprog/mbe0$ ./mbe0
Enter value: 123
123 x 11 =         1   (1+2) (2+3)   3
         =         1     3     5     3
         = 1353
lab46:~/src/cprog/mbe0$ 

A three digit value with carries:

lab46:~/src/cprog/mbe0$ ./mbe0
Enter value: 567
567 x 11 =         5   (5+6) (6+7)   7
         =         5    11    13     7
         =       (5+1) (1+1)   3     7
         =         6     2     3     7
         = 6237
lab46:~/src/cprog/mbe0$ 

The maximum input value (three digit value with carries; note how it fills the spacing):

lab46:~/src/cprog/mbe0$ ./mbe0
Enter value: 999
999 x 11 =         9   (9+9) (9+9)   9
         =         9    18    18     9
         =       (9+1) (8+1)   8     9
         =        10     9     8     9
         = (0+1)   0     9     8     9
         =   1     0     9     8     9
         = 10989
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.

Output Specification

As you can see, there's some spacing at work in the program's output:

Enter value: 967
967 x 11 =         9   (9+6) (6+7)   7
         =         9    15    13     7
         =       (9+1) (5+1)   3     7
         =        10     6     3     7
         = (0+1)   0     6     3     7
         =   1     0     6     3     7
         = 10637

With the exception of the final (packed together) 10637, everything is displayed to STDERR (that 10637 is the only thing to display to STDOUT).

Some important things of note:

  • The input value should be right justified in a 3 space allocated location to just before the “ x 11”.
  • The equal sign has a space padding it on each side: “ = ”
  • The output is calibrated for working with 5 digits. If there are no digits in those further left places, blanks must be displayed instead (in the 10637 example above, the first four lines are only dealing with 4 digits, until a carry propagates over to a 5th digit).
  • Each digit of output needs to be calibrated to potentially display an addition operation, wrapped in parenthesis (as you see in the above example: (9+1)
    • if only a single value is being displayed, it must appear where the '+' sign would be (so everything will line up in a digit-centered-like fashion).
    • if the number to display is a 2-digit number (10-18), its one's place needs to line up with the '+' sign.
    • each digit space has a single space separating it from the next digit space.
    • The final digit column (which should never have a carry) has a newline immediately following it (no trailing spaces).
  • At the beginning, the leftmost and rightmost digits are displayed, with the additions visualized on the inner digits, according to the length.
  • If there are any additions, the next line needs to show the result of those additions.
  • Following any result, carries should be checked for. If any carries are generated, a new line visualizing the additions must be displayed, then another line with the result. This process needs to propagate as many times as needed (thankfully, as you only have to consider 2- and 3- digit numbers and aren't allowed to use loops/gotos/repetition, there is a fixed upper bound on how many times you need to process these potential changes.
    • Obviously, if the problem resolves sooner, it does, without needing to display those extra lines. Study the other execution examples, they demonstrate this.

You will probably find some application for selection statements, gaining further experience with them and likely deploying them with more sophisticated relational conditions (even compound ones).

Output formatting is still an important aspect to keep in mind. The computer needs to be told exactly what to do, and our default habits would likely be to do “whatever works”… so I am maintaining an exactness on my requirements for output to ensure we continue to establish these good habits.

Another aspect of the output requirements is that they will force a focus on the individual steps of processing using this algorithm. This should help add exposure to developing good habits of ceasing to automatically read between the lines, and to identify and focus on the discrete steps needed to accomplish the task at hand.

Verification

Following are some procedures you can follow to verify if your program's output is in conformance with overall project specifications.

STDOUT verification of answer

As the final answer (and ONLY the answer) is to be output to STDOUT, your can run the following to check to see if this is the case with your program:

3-digit result

lab46:~/src/cprog/mbe0$ ./mbe0 2>/dev/null <<< 64
704
lab46:~/src/cprog/mbe0$ 

4-digit result

lab46:~/src/cprog/mbe0$ ./mbe0 2>/dev/null <<< 512
5632
lab46:~/src/cprog/mbe0$ 

5-digit result

lab46:~/src/cprog/mbe0$ ./mbe0 2>/dev/null <<< 927
10197
lab46:~/src/cprog/mbe0$ 

Total output comparison

If you'd like to check if the entirety of your output is correct (especially in relation to spacing), you can do the following.

I have saved sample (correct) outputs on the system that you can check against. The following commands will let you do so:

First, save your output to a file

I have saved program outputs for the following inputs:

  • 37
  • 73
  • 128
  • 480
  • 907
  • 933

If you run your program with one of these same inputs, you can compare your results for correctness.

In the below example, I do this for an input value of 37:

lab46:~/src/cprog/mbe0$ ./mbe0 <<< 37 2>output.37 1>>output.37
lab46:~/src/cprog/mbe0$ 

What we have done is fed in the input via a here string (form of STDIN redirect), and then output both STDERR and STDOUT into a common file (appending STDOUT, after the STDERR output).

You should now have a file called output.37 in your current directory.

Next, check it against mine

I have these files (by the same names), saved in the CPROG Public Directory (under the mbe0 directory).

By using the diff command, you can see differences, if any. If there are no differences, the output matches (this is good, and what you want).

lab46:~/src/cprog/mbe0$ diff output.37 /var/public/SEMESTER/cprog/mbe0/output.37
lab46:~/src/cprog/mbe0$ 

If you see output, that means there are differences, and that your output likely isn't in conformance with project specifications.

You can repeat this for the other data files (output.73 for an input of 73, etc.)

Isolate just the STDOUT or the STDERR

Additionally, you may want to specifically look at your program's STDOUT or STDERR independent of each other.

To do this, you can do the following.

To isolate STDOUT and STDERR into separate files, you can do the following:

lab46:~/src/cprog/mbe0$ ./mbe0 <<< 37 1>stdout.37 2>stderr.37
lab46:~/src/cprog/mbe0$ 

You can then compare those particular collections of information against my copies (located in the mbe0 subdirectory of the CPROG Public Directory, by the same file names).

automated verification

I have rigged up pchk to work for this project; it will check for differences and compare MD5sum hashes for stderr, stdout, and total (combined) output.

Once you have everything complete, this is a good final check to do to ensure everything is in order.

lab46:~/src/cprog/mbe0$ pchk cprog mbe0
===================================================
=          mbe0 output validation check           =
===================================================
      stderr diff: MATCH    stderr md5sum: MATCH
[ 37] stdout diff: MATCH    stdout md5sum: MATCH
      output diff: MATCH    output md5sum: MATCH

      stderr diff: MATCH    stderr md5sum: MATCH
[ 73] stdout diff: MATCH    stdout md5sum: MATCH
      output diff: MATCH    output md5sum: MATCH

      stderr diff: MATCH    stderr md5sum: MATCH
[128] stdout diff: MATCH    stdout md5sum: MATCH
      output diff: MATCH    output md5sum: MATCH

      stderr diff: MATCH    stderr md5sum: MATCH
[480] stdout diff: MATCH    stdout md5sum: MATCH
      output diff: MATCH    output md5sum: MATCH

      stderr diff: MATCH    stderr md5sum: MATCH
[907] stdout diff: MATCH    stdout md5sum: MATCH
      output diff: MATCH    output md5sum: MATCH

      stderr diff: MATCH    stderr md5sum: MATCH
[933] stdout diff: MATCH    stdout md5sum: MATCH
      output diff: MATCH    output md5sum: MATCH
===================================================
=     matches: 36, mismatches:  0, total: 36      =
===================================================
lab46:~/src/cprog/mbe0$ 

Since your project submission will be evaluated in part by compliance to output specifications, you probably want to check to see how you are doing before submitting.

Reflection

Be sure to provide any commentary on your journal 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)
    • Again, I will be compiling as follows: gcc -Wall -o mbe0 mbe0.c
  • Submit the program in a file called mbe0.c
  • 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
  • No loops (nor attempts to iterate/repeat code)!
  • No arrays!
  • 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 mbe0.c
Submitting cprog project "mbe0":
    -> mbe0.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.

What I'll be looking for:

52:mbe0:final tally of results (52/52)
*:mbe0:adequate indentation and comments in code [4/4]
*:mbe0:program correctly implements specified algorithm [8/8]
*:mbe0:input obtained from STDIN as single unsigned short integer [4/4]
*:mbe0:sum variables declared and used appropriately in processing [4/4]
*:mbe0:carry variables declared and used appropriately in processing [4/4]
*:mbe0:processing output properly spaced and displayed to STDERR [4/4]
*:mbe0:final output displayed to STDOUT as packed individual digits [4/4]
*:mbe0:effective usage of fprintf() and fscanf() [4/4]
*:mbe0:runtime tests succeed [8/8]
*:mbe0:no negative compiler messages for code [4/4]
*:mbe0:code is pushed to lab46 repository [4/4]
haas/fall2024/cprog/projects/mbe0.txt · Last modified: 2017/10/15 20:49 by 127.0.0.1