User Tools

Site Tools


haas:fall2019:c4eng:projects:allmult

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:

  • 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.

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:

  • 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 if it is a one-, two-, or three-digit number
    • assume for one-digit there are leading zeros to match it with the biggest number
  • perform the correct algorithm against the input
  • implement said algorithm in a function that is called
  • display some semblance of the intermediate steps involved
  • propagate any carries
  • output the final value

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.

  • 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 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.

haas/fall2019/c4eng/projects/allmult.txt · Last modified: 2014/02/20 13:27 by 127.0.0.1