Table of Contents

Corning Community College

CSCS1320 C/C++ Programming

Project: MENTAL MATH - SQUARES OF FIVES (sof0)

Objective

To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of computing the square of any number ending with 5.

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 computing the square of any one, two-, or three-digit number that ends with 5.

Background

Mental Math constitutes an intersection of mental techniques 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 numeric manipulation and simple (reduced) multiplication. To wit:

Squaring a value

Squaring is essentially multiplying a number by itself-

While not outwardly a difficult procedure, the nature of multiplying multiple digit numbers in your head can quickly result in more steps (and more steps means more time, if doing things the traditional way).

Finding a shortcut through this process enables it to remain solidly within the realm of mental math, and makes for a good algorithm to practice implementing on the computer.

This particular trick relies on a subset of the squares: those ending with a 5 (a five in the ones place).

The implementational scope of this trick will be just values of one-, two-, and three-digits ending with 5:

Squaring values ending with 5

The trick here is two-fold. First, we separate the one's place 5 from the rest of the number (which can be accomplished in our mind's easily enough, but on the computer we must resort to some math).

We then take that isolated five and square it; we'll get 25. That is how our result will end (so bam! we now have our tens and ones place already solved)

Next, we take the remaining digits of the original value, and multiply it by its increment:

We take this result and append the 25 after it.

For example:

15 * 15 = 1*(1+1) 5*5
        = 1*2     5*5
        = 2       25
        = 225

and:

75 * 75 = 7*(7+1) 5*5
        = 7*8     5*5
        = 56      25
        = 5625

For a single digit number like 5, when you take the 5 away, what do you get? ZERO. Zero times anything is zero, so the result is 0 25, or 25 (ie this process still works).

For three digit numbers like 105, we have 10, and its increment is 11, so 10 x 11 = 110.

105 * 105 = 10*(10+1) 5*5
          = 10*11     5*5
          = 110       25
          = 11025

Program

It is your task to write the program that will use the above method to compute the square of the input value ending with a 5 (you are to input the entire number, including the 5 at the end).

Your program should:

Execution

yourpi:~/src/SEMESTER/cprog/sof0$ ./sof0.armv7l
Enter value: 75
75  x 75  =   5625
yourpi:~/src/SEMESTER/cprog/sof0$ 

The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.

Note how the two “75” values are left-justified within a 3-space slot (with the multiplication 'x' and equal sign '=' being padded with a space on either side). This information should all be displayed to STDERR.

Similarly, here's an example of 105×105:

yourpi:~/src/SEMESTER/cprog/sof0$ ./sof0.armv7l
Enter value: 105
105 x 105 =  11025
yourpi:~/src/SEMESTER/cprog/sof0$ 

The 'x' and '=' padding persists, but because we're squaring a 3-digit value vs. a 2-digit value, we occupy the entire allocated space on the screen.

If you'd like to verify successful output to STDOUT/STDERR, you can perform the following tests. First, verify that the answer (and ONLY the answer), is being sent to STDOUT – we do this by eliminating STDERR entirely:

yourpi:~/src/SEMESTER/cprog/sof0$ ./sof0.armv7l 2> /dev/null <<< 105
 11025
yourpi:~/src/SEMESTER/cprog/sof0$ 

What we are doing here is two-fold:

Similarly, if we were to eliminate STDOUT entirely (for verifying STDERR output):

yourpi:~/src/SEMESTER/cprog/sof0$ ./sof0.armv7l 1> /dev/null
Enter value: 75
75  x 75  = yourpi:~/src/SEMESTER/cprog/sof0$ 

What we are doing here:

Verification

One of the tests I will perform for output compliance of your code will involve comparing your program's output against a range of input values, to see if they all output in conformance with project specifications.

I will make use of a checksum to verify exactness.

You will need to run this from your sof0 project directory with a compiled and operational binary by the name of sof0.

You can check your project by typing in the following at the prompt:

lab46:~/src/SEMESTER/cprog/sof0$ pchk cprog sof0

If all aligns, you will see this:

==================================================
=   CPROG sof0 project output validation tool    =
==================================================
sof0 checksum is: 822a47fb2a45845500b6c10878045bd5
your checksum is: 822a47fb2a45845500b6c10878045bd5
==================================================
    verification: SUCCESS!
==================================================

If something is off, your checksum will not match the sof0 checksum, and verification will instead say “MISMATCH”, like follows (note that a mismatched checksum can be anything, and likely not what is seen in this example):

==================================================
=   CPROG sof0 project output validation tool    =
==================================================
sof0 checksum is: 822a47fb2a45845500b6c10878045bd5
your checksum is: 92af264c86823a61529948caaeac53e0
==================================================
    verification: MISMATCH
==================================================

Questions / Food for Thought

These are things I'd like you to contemplate, even use as potential material on your weekly journal entry. The more you think about and understand the problem, the better your ability to solve it and other problems.

Review of Compiling/Executing

Just to review the compilation/execution process for working with your source code, if we had a file, hello.c, that we wished to compile to a binary called hello, we'd first want to compile the code, as follows.

If compiling on lab46:

lab46:~/src/SEMESTER/cprog/PROJECT$ gcc -Wall --std=gnu99 -funsigned-char -o PROJECT.x86_64 PROJECT.c
lab46:~/src/SEMESTER/cprog/PROJECT$ 

If compiling on your pi system:

yourpi:~/src/SEMESTER/cprog/PROJECT$ gcc -Wall --std=gnu99 -funsigned-char -o PROJECT.armv7l PROJECT.c
yourpi:~/src/SEMESTER/cprog/PROJECT$ 

Assuming there are no syntax errors or warnings, and everything compiled correctly, you should just get your prompt back. In the event of problems, the compiler will be sure to tell you about them.

Conceptually, the arrangement is as follows:

gcc -Wall --std=gnu99 -funsigned-char -o BINARY_FILE SOURCE_FILE

The BINARY_FILE comes immediately after the -o, NOT the SOURCE_FILE (the source file must never immediately follow a -o). It can precede, and such is perfectly valid (especially if you feel that way more intuitive).

The -Wall (treat all warnings as errors, increase general verbosity about warnings) and --std=gnu99 (switch compiler to use the C99 standard of the C language, with GNU extensions) are options given to the compiler. The -funsigned-char flag forces “char” types lacking a signed/unsigned qualifier to default to unsigned.

To execute your binary, we need to specify a path to it, so we use ./, which basically references the current directory.

On lab46:

lab46:~/src/SEMESTER/cprog/PROJECT$ ./PROJECT.x86_64

On your pi system:

yourpi:~/src/SEMESTER/cprog/PROJECT$ ./PROJECT.armv7l

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:

lab46:~/src/SEMESTER/cprog/sof0$ make submit

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.

I'll be looking for the following:

39:sof0:final tally of results (39/39)
*:sof0:obtained via grabit by Sunday before deadline [3/3]
*:sof0:program compiles successfully, no errors [3/3]
*:sof0:program compiles with no warnings [3/3]
*:sof0:program performs stated task/algorithm [3/3]
*:sof0:program output conforms to formatting expectations [3/3]
*:sof0:proper error checking and status reporting performed [3/3]
*:sof0:code implements solution using relevant concepts [3/3]
*:sof0:code updates committed/pushed to lab46 semester repo [3/3]
*:sof0:code uses correct variable types and name lengths [3/3]
*:sof0:project is submitted with relevant and complete source [3/3]
*:sof0:project is submitted on lab46 using 'make submit' [3/3]
*:sof0:project is submitted with pi and lab46 binaries [3/3]
*:sof0:runtime tests of submitted program succeed [3/3]

Additionally: