Table of Contents

Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: MENTAL MATH (VERTICALLY AND CROSSWISE MULTIPLICATION)

Objective

To further explore and implement a programmatic solution (ie simulation) of a real life process- the mental math trick of multiplying numbers together.

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:

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 numbers together.

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 two double digit numbers together

Unlike the “squares ending in 5” and “by 11” optimized special cases, we need a method for addressing multiplication in general.

That will be solved via the “vertically and crosswise” method.

In short, this consists of a sequence of simpler multiplication and addition operations:

   41
 x 37
--------
 (4x3) (7x4)+(1x3) (1x7)    multiply each column verically (1x7 gets us 7)
   12    28 +  3     7      between the two verticals is our crosswise
   12      31        7
   
    2       1        7      these are the place sums from the raw operations
   1       3        0       there are the carry results from the raw operations
   
    2       1        7      the carries are then applied in an addition operation
   13      0                    to the next place to the left
   ---------------------
   15       1        7 = 1517  and that result (sans any further carries) is our answer

And we now have the correct result.

As another example, let us look at 47 x 13:

      47
    x 13
    ------
    (4x1) (4x3)+(7x1) (7x3)
      4      12+7       21
      4       19        21
      
      4        9         1
     0        1         2 
     ---------------------
      4       (19+2)     1
      
      4       21         1
      
      4        1         1
              2         0
              
      4+2      1         1
      
      6        1         1
              
              = 611

Got it? Try it with some other examples.

Multiplying two three-digit numbers together

In this case we merely extend the pattern of vertically and crosswise, namely:

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.

Let's look at 123 x 234:

   123
 x 234
 -------        carry_in
     3x4          [+ 0] ones place    (sum of 2 carry out of 1)
    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)

  We then take the sums from each operation and place them as appropriate
 in their position in the final number (ones place is furthest on the right).

 123 x 234 = 28782

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:

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.

Your program should:

Execution

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:

lab46:~/src/cprog/vertcross$ ./vertcross
Enter number1: 34
Enter number2: 26
34 x 26 = 884
lab46:~/src/cprog/vertcross$ 

Second, a three digit value with 1 argument:

lab46:~/src/cprog/vertcross$ ./vertcross 137
Enter number2: 32
137 x 032 = 4384
lab46:~/src/cprog/vertcross$ 

Submission

To successfully complete this project, the following criteria must be met:

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog vertcross vertcross.c
Submitting cprog project "vertcross":
    -> vertcross.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.