CSCS1320 C/C++ Programming

PROJECT: SQUARES OF FIVE (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.

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.

There are other (perhaps more direct) means of performing this task, but that is not the objective here: you are to implement your solution adhering to the process described herein, which will expose you to important programmatic and problem solving details.

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-

  • 5 squared is 5*5 or 25
  • 8 squared is 8*8 or 64

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:

  • 5
  • 15
  • 25
  • 35
  • 45
  • 55
  • 65
  • 75
  • 85
  • 95
  • 105
  • 115
  • 125
  • 235
  • 245
  • 485
  • 495
  • 505
  • 995

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:

  • 1's increment (1+1) is 2, so 1*2
  • 2's increment (2+1) is 3, so 2*3
  • 3's increment (3+1) is 4, so 3*4
  • 4's increment (4+1) is 5, so 4*5
  • 9's increment (9+1) is 10, so 9*10

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

GRABIT

I have prepared a grabit for resources related to this project. To obtain it:

lab46:~/src/SEMESTER/DESIG$ grabit DESIG sof0
make: Entering directory '/var/public/SEMESTER/DESIG/sof0'
'/var/public/SEMESTER/DESIG/sof0/Makefile' -> '/home/user/src/SEMESTER/DESIG/sof0/Makefile'
'/var/public/SEMESTER/DESIG/sof0/sof0.c' -> '/home/user/src/SEMESTER/DESIG/sof0/sof0.c'
make: Leaving directory '/var/public/SEMESTER/DESIG/sof0'
lab46:~/src/SEMESTER/DESIG$ 

At which point you can change into the newly created and populated sof0 directory.

Be sure to add, commit, and push this to your lab46 clone of your repository.

Then, if working on your pi/system, pull and update to get the files available there, or continue working on it on lab46.

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:

  • prompt the user for the number (input)
    • input number as an unsigned short integer
  • perform the task (process)
    • isolate one's digit mathematically, store in a variable (unsigned short int)
    • isolate remaining digits mathematically, store in another variable (unsigned short int)
    • perform the algorithm on the two pieces, storing their results in two separate variables (of type unsigned short int)
  • display the final value (output)
    • display the beginning and ending parts together (but stored in separate variables)
    • display the resulting number to STDOUT (right-justified in a space supporting the largest possible value – see output example below)
    • display any supporting text to STDERR (display of source values left-justified in a space supporting 3-digit values – see output example below).
  • because we have not officially learned how to do selection/have the computer react to conditions, please implement with the assumption that the user will ALWAYS input a correct value. Do not worry about having to check for invalid or illegal input values (I will not be checking for such when I evaluate your project).

COMPILING

The compilation process for working with your source code is as follows:

lab46:~/src/SEMESTER/cprog/sof0$ gcc -Wall --std=gnu18 -o sof0 sof0.c
lab46:~/src/SEMESTER/cprog/sof0$ 

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=gnu18 -o BINARYFILE SOURCEFILE

The BINARYFILE comes immediately after the -o, NOT the SOURCEFILE (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=gnu18 (switch compiler to use a newer standard of the C language, with GNU extensions) are options given to the compiler.

EXECUTION

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

lab46:~/src/SEMESTER/cprog/sof0$ ./sof0
Enter value: 75
75  x 75  =   5625
lab46:~/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 105x105:

lab46:~/src/SEMESTER/cprog/sof0$ ./sof0
Enter value: 105
105 x 105 =  11025
lab46:~/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:

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

What we are doing here is two-fold:

  • We are using command-line I/O redirection to redirect STDERR (which is bound to file descriptor #2) to the system bit-bucket.
  • We are "redirecting" STDIN using a here string, providing the program's input of 105 right on the command-line at time of execution.

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

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

What we are doing here:

  • We are using command-line I/O redirection to redirect STDOUT (which is bound to file descriptor #1) to the system bit-bucket.

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 FOR CONSIDERATION

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.

  • Why/how does this trick work for 1-digit numbers?
  • Considering our 1-, 2-, and 3-digit domain restriction for this project, how many candidate values are there for input?
  • What is the smallest input value?
  • What is the largest input value?
  • How many input values are there that end in 5?
  • What is the largest square that can be calculated given the project input restrictions?
  • How many digits is the largest square?
  • How can knowing how many digits the largest square is help you implement your solution?

SUBMISSION

To successfully complete this project, the following criteria must be met:

  • Code must compile/execute cleanly (no notes, warnings, nor errors)
  • Code must be nicely and consistently indented
  • Code must be well commented
  • Do NOT double space your code. Group like statements together.
  • Track/version the source code in your private semester 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:

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.

What I'll be looking for:

RUBRIC

I'll be evaluating the project based on the following criteria:

130:sof0:final tally of results (130/130)
*:sof0:grabit the code on lab46 by Sunday before deadline [13/13]
*:sof0:code is pushed to private semester repository [13/13]
*:sof0:proper output formatting per specifications [26/26]
*:sof0:clean compile, no compiler messages [26/26]
*:sof0:program conforms to project specifications [26/26]
*:sof0:progrm passes verification checks [26/26]

NOTE: The spirit of program implementation includes your process using the mental math approach, not a more seemingly-straightforward computational approach, or one given to you out of context by AI.

Additionally:

  • Solutions not abiding by spirit of project will be subject to a 50% overall deduction
  • Solutions not utilizing descriptive why and how comments will be subject to a 25% overall deduction
  • Solutions not utilizing indentation to promote scope and clarity will be subject to a 25% overall deduction
  • Solutions not organized and easy to read are subject to a 25% overall deduction