User Tools

Site Tools


Sidebar

projects

haas:spring2017:cprog:projects:vertcross

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:

  • ability to access and manipulate arrays
  • code modularity using functions

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:

  • last digit is the result of vertically multiplying the last digit of each number
  • first digit (unsimplified– carry could impact it) is the result of vertically multiplying the first digit of each number
  • middle digit is the result of a crosswise (firstof1 x lastof2) + (firstof2 x lastof1)
  • We focus on the single digits… any double digit results act as carries to the next place on the left
   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:

  • multiply right-most column vertically (ones place)
  • 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)

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:

  • obtain its input from the command-line (2 arguments: the two numbers)
    • if there is only 1 argument, ask the user for the second number
    • if there are no arguments, ask the user for both numbers
  • determine from the input the quantity of digits in each number
    • add any leading zeros in order to match the length of both numbers
  • perform the correct algorithm (vertical and crosswise) against the input
  • implement said algorithm in a function that is called
  • propagate any carries
  • output the final value

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:

  • 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 (no “cheating”)
  • algorithm must be located within a specific function and called during execution
  • 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 in some way resemble 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 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.

haas/spring2017/cprog/projects/vertcross.txt · Last modified: 2014/03/13 09:42 by 127.0.0.1