projects
projects
Corning Community College
CSCS1320 C/C++ Programming
~~TOC~~
To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of multiplying any two- or three-digit number by eleven.
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:
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 any two- or three-digit number by eleven.
Mental Math constitutes an intersection of mental tricks and math- instead of utilizing a purely math-only solution, 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, number manipulation, and simple arithmetic. To wit:
Here we do a pivot and then perform simple arithmetic to obtain the middle value.
In the case of 10 x 11, we take 10 and pivot it, getting 1 and 0, respectively our first and last digit of our soon-to-be solution.
To get the middle value, we add these two values together: 1+0=1
So, the result of 10 x 11 is: 1 (1+0) 0 or: 110
Let's try it with 32 x 11:
32 x 11 = 3 (3+2) 2 = 3 5 2 = 352
This is almost the entire process, but there's one other factor we need to be aware- if the summing of the first and last values yields a value greater than 10, we must propagate the carry to the next digit to the left (i.e. the first digit).
For example, let us take the maximum two digit value (99):
Using this process as it has been described thus far, we would (incorrectly) get:
99 x 11 = 9 (9 + 9) 9 = 9 18 9 = 9189
But that would be incorrect mathematically.
To compensate (or, to present the full rules for the trick), we take the sum of this result as the middle digit, and apply the carry to the next digit to the left, so:
99 x 11 = 9 (9+9) 9 = (9+1) 8 9 = 10 8 9 = 1089
And we now have the correct result.
As another example, let us look at 47 x 11:
47 x 11 = 4 (4+7) 7 = (4+1) 1 7 = 5 1 7 = 517
Got it? Try it with some other examples.
In grade school, when learning to do arithmetic by hand (you still are taught how to do arithmetic by hand, right?), we first learned the concept of sum and carry. This bore value as we were applying this to place values of the number.
Little did you know then, but you were learning the basics of effective numerical and logical problem solving within the domain of Computer Science in grade school!
For example, in the case of the number 18, when dissecting the number into its place values, we have:
In single digit terminology, 18 is expressed as a sum of 8 with a carry of 1. We see this more clearly when producing the value, see the original equation of 9+9:
1 <-- carry (to be added to 10s position) 9 + 9 ---- 8 <-- sum (of 1s position)
See what is happening here? The basis for adding multiple-digit numbers. Perhaps it would make more sense if we showed how adding 9 + 9 was in fact adding two 2-digit numbers together:
1 <-- carry (to be added to 10s position) 09 +09 ---- 8 <-- sum (of 1s position)
Then we have the follow-up addition to determine the value of the 10s place:
1 0 +0 -- 1 <-- sum (of 10s position)
and we would technically have a resulting carry of 0 (but adding zero to any values gives us the value itself– the so-called additive identity property we learned in math class).
Once we are all said and done, we concatenate the tens and ones places together:
1 (ten) and 8 (ones): 18
In this case we merely extend the pattern from double digits, rippling through a series of comparing each set of two consecutive digits.
Let's look at 123 x 11:
123 x 11 = 1 (1 + 2) (2 + 3) 3 = 1 3 5 3 = 1353
And digit-based additions that generate a carry are similarly propagated.
567 x 11:
567 x 11 = 5 (5 + 6) (6 + 7) 7 = (5 + 1) (1 + 1) 3 7 = 6 2 3 7 = 6237
When doing this, we need to evaluate the number from right to left (just as we would do it if we were to compute it purely mathematically by hand):
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.
It is your task to write the program that will use the above method to compute the requested two- or three-digit value against a multiplicand of 11 (without using any multiplication to obtain your result).
Your program should:
Several operating behaviors are shown as examples.
A two digit value with no carries:
lab46:~/src/cprog/mbe0$ ./mbe0 Enter value: 32 32 x 11 = 3 (3+2) 2 = 3 5 2 = 352 lab46:~/src/cprog/mbe0$
A two digit value with carries:
lab46:~/src/cprog/mbe0$ ./mbe0 Enter value: 86 86 x 11 = 8 (8+6) 6 = 8 14 6 = (8+1) 4 6 = 9 4 6 = 946 lab46:~/src/cprog/mbe0$
A three digit value with no carries:
lab46:~/src/cprog/mbe0$ ./mbe0 Enter value: 123 123 x 11 = 1 (1+2) (2+3) 3 = 1 3 5 3 = 1353 lab46:~/src/cprog/mbe0$
A three digit value with carries:
lab46:~/src/cprog/mbe0$ ./mbe0 Enter value: 567 567 x 11 = 5 (5+6) (6+7) 7 = 5 11 13 7 = (5+1) (1+1) 3 7 = 6 2 3 7 = 6237 lab46:~/src/cprog/mbe0$
The maximum input value (three digit value with carries; note how it fills the spacing):
lab46:~/src/cprog/mbe0$ ./mbe0 Enter value: 999 999 x 11 = 9 (9+9) (9+9) 9 = 9 18 18 9 = (9+1) (8+1) 8 9 = 10 9 8 9 = (0+1) 0 9 8 9 = 1 0 9 8 9 = 10989 lab46:~/src/cprog/mbe0$
The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.
As you can see, there's some spacing at work in the program's output:
Enter value: 967 967 x 11 = 9 (9+6) (6+7) 7 = 9 15 13 7 = (9+1) (5+1) 3 7 = 10 6 3 7 = (0+1) 0 6 3 7 = 1 0 6 3 7 = 10637
With the exception of the final (packed together) 10637, everything is displayed to STDERR (that 10637 is the only thing to display to STDOUT).
Some important things of note:
You will probably find some application for selection statements, gaining further experience with them and likely deploying them with more sophisticated relational conditions (even compound ones).
Output formatting is still an important aspect to keep in mind. The computer needs to be told exactly what to do, and our default habits would likely be to do “whatever works”… so I am maintaining an exactness on my requirements for output to ensure we continue to establish these good habits.
Another aspect of the output requirements is that they will force a focus on the individual steps of processing using this algorithm. This should help add exposure to developing good habits of ceasing to automatically read between the lines, and to identify and focus on the discrete steps needed to accomplish the task at hand.
Following are some procedures you can follow to verify if your program's output is in conformance with overall project specifications.
As the final answer (and ONLY the answer) is to be output to STDOUT, your can run the following to check to see if this is the case with your program:
lab46:~/src/cprog/mbe0$ ./mbe0 2>/dev/null <<< 64 704 lab46:~/src/cprog/mbe0$
lab46:~/src/cprog/mbe0$ ./mbe0 2>/dev/null <<< 512 5632 lab46:~/src/cprog/mbe0$
lab46:~/src/cprog/mbe0$ ./mbe0 2>/dev/null <<< 927 10197 lab46:~/src/cprog/mbe0$
If you'd like to check if the entirety of your output is correct (especially in relation to spacing), you can do the following.
I have saved sample (correct) outputs on the system that you can check against. The following commands will let you do so:
I have saved program outputs for the following inputs:
If you run your program with one of these same inputs, you can compare your results for correctness.
In the below example, I do this for an input value of 37:
lab46:~/src/cprog/mbe0$ ./mbe0 <<< 37 2>output.37 1>>output.37 lab46:~/src/cprog/mbe0$
What we have done is fed in the input via a here string (form of STDIN redirect), and then output both STDERR and STDOUT into a common file (appending STDOUT, after the STDERR output).
You should now have a file called output.37 in your current directory.
I have these files (by the same names), saved in the CPROG Public Directory (under the mbe0 directory).
By using the diff command, you can see differences, if any. If there are no differences, the output matches (this is good, and what you want).
lab46:~/src/cprog/mbe0$ diff output.37 /var/public/cprog/mbe0/output.37 lab46:~/src/cprog/mbe0$
If you see output, that means there are differences, and that your output likely isn't in conformance with project specifications.
You can repeat this for the other data files (output.73 for an input of 73, etc.)
Additionally, you may want to specifically look at your program's STDOUT or STDERR independent of each other.
To do this, you can do the following.
To isolate STDOUT and STDERR into separate files, you can do the following:
lab46:~/src/cprog/mbe0$ ./mbe0 <<< 37 1>stdout.37 2>stderr.37 lab46:~/src/cprog/mbe0$
You can then compare those particular collections of information against my copies (located in the mbe0 subdirectory of the CPROG Public Directory, by the same file names).
I have rigged up pchk to work for this project; it will check for differences and compare MD5sum hashes for stderr, stdout, and total (combined) output.
Once you have everything complete, this is a good final check to do to ensure everything is in order.
lab46:~/src/cprog/mbe0$ pchk cprog mbe0 =================================================== = mbe0 output validation check = =================================================== stderr diff: MATCH stderr md5sum: MATCH [ 37] stdout diff: MATCH stdout md5sum: MATCH output diff: MATCH output md5sum: MATCH stderr diff: MATCH stderr md5sum: MATCH [ 73] stdout diff: MATCH stdout md5sum: MATCH output diff: MATCH output md5sum: MATCH stderr diff: MATCH stderr md5sum: MATCH [128] stdout diff: MATCH stdout md5sum: MATCH output diff: MATCH output md5sum: MATCH stderr diff: MATCH stderr md5sum: MATCH [480] stdout diff: MATCH stdout md5sum: MATCH output diff: MATCH output md5sum: MATCH stderr diff: MATCH stderr md5sum: MATCH [907] stdout diff: MATCH stdout md5sum: MATCH output diff: MATCH output md5sum: MATCH stderr diff: MATCH stderr md5sum: MATCH [933] stdout diff: MATCH stdout md5sum: MATCH output diff: MATCH output md5sum: MATCH =================================================== = matches: 36, mismatches: 0, total: 36 = =================================================== lab46:~/src/cprog/mbe0$
Since your project submission will be evaluated in part by compliance to output specifications, you probably want to check to see how you are doing before submitting.
Be sure to provide any commentary on your journal regarding realizations had and discoveries made during your pursuit of this project.
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 mbe0 mbe0.c Submitting cprog project "mbe0": -> mbe0.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.
What I'll be looking for:
52:mbe0:final tally of results (52/52) *:mbe0:adequate indentation and comments in code [4/4] *:mbe0:program correctly implements specified algorithm [8/8] *:mbe0:input obtained from STDIN as single unsigned short integer [4/4] *:mbe0:sum variables declared and used appropriately in processing [4/4] *:mbe0:carry variables declared and used appropriately in processing [4/4] *:mbe0:processing output properly spaced and displayed to STDERR [4/4] *:mbe0:final output displayed to STDOUT as packed individual digits [4/4] *:mbe0:effective usage of fprintf() and fscanf() [4/4] *:mbe0:runtime tests succeed [8/8] *:mbe0:no negative compiler messages for code [4/4] *:mbe0:code is pushed to lab46 repository [4/4]