Table of Contents

Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: MENTAL MATH (ALL MULTIPLICATION)

Objective

To reinforce our coding skills, we will revisit the previous mental math multiplication methods, look at a new one, and write a program that will pick the most optimal method for performing multiplication.

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.

Review

In previous projects, we explored various means of performing mental math multiplications:

Vertically and Crosswise

Considered the “general” method, this is essentially a set of simple multiplications and additions:

   52
  x19
  ----
  0518   the two vertical multiplications (05 and 18)
   47    the crosswise
  ----
  0988   add them to get the result

Multiply by 11

If one of the numbers is all one's (such as 11), we can employ a simple trick of pivoting and addition:

    32
   x11
   ----
   3 (3+2) 2
   3  5    2
   352

squares

If both numbers are identical, AND end in 5, the following trick can be used:

  75
 x75
 ----
 7*8 5*5
 56  25
 5625

proximity to base

There is another trick worthy of exploration, and that involves cases where the two numbers being multiplied are near a major power of the base we're working in (10)… powers would be things like 10, 100, 1000, etc.

It simplifies the process to addition and simpler multiplication:

   91    -9   91 is 9 away from 100
  x95    -5   95 is 5 away from 100
  ----
   86    45   86 is 91-5 (or 95-9), 45 is -9*-5
   
   = 8645

Selecting the optimal approach

When performing these tasks by hand (or my mind), we must make a decision based on the inputs available (the incoming data– the numbers to be multiplied). We recognize conformance to an existing method, and take the more optimized approach.

So, since computers can also make decisions based on a set of information, we can rig up a program to analyze the input values and choose the best method to take. That is precisely what this project is aiming to have you implement.

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 1-, 2-, and 3-digit values.

First up, two-digit values without argument:

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

        34
       x26
       ----
       0624
        26
       ----
       0884 

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
  x032
  ------
  000914
   0327
    02
  ------
  004384

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.

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 allmult allmult.c
Submitting cprog project "allmult":
    -> allmult.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.