User Tools

Site Tools


Sidebar

projects

haas:spring2014:cprog:projects:vertcross

This is an old revision of the document!


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
              
              = 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:

  • there are 3 columns to multiply vertically
  • there are 2 crosswise operations to perform

Let's look at 123 x 234:

   123
 x 234
 -------
  (1x2)         (2x3)         (3x4)    the vertical multiplications
      (1x3)+(2x2)   (2x4)+(3x3)        the crosswise operations
             (1x4)+(2x3)               an outer-most crosswise operation
  ---------------------------------
    02   06    12                      as this is a 3 digit problem, we need 2 places
      3+4   8+9
         4+6
  ------------------
    020612
     0717
      10
    ------
    028782 = 28782 (as a leading zero does not impact the value at all)

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$ 

Reflection

Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project.

  • Can this process be extended for four digit numbers?
  • How about five digit numbers?

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/spring2014/cprog/projects/vertcross.1394702728.txt.gz · Last modified: 2014/03/13 09:25 by wedge