This is an old revision of the document!
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 subtracting certain potentially large numbers from left to right.
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 subtracting certain numbers from left to right– seemingly against the grain from everything we've been taught.
This trick's full name is “All from Nine, the Last from Ten”, and refers to a central detail of the process we will be undertaking.
For starters, let us try it out:
1000 - 941 ==== 059
The thing is, we aren't going to be solving this the usual way. We're going to do it from left to right. So, a walkthrough of what just happened:
Clearly, there are some prerequisites that need to be met in order to use this trick:
Try your hand at another:
1000000000000000 - 928573956013453 ================
Go ahead and try it (work it out on paper).
The answer is: 071426043986547
The other important component of our mbe1 project will involve the effective use of arrays to further aid us in having an efficient (and very importantly: scalable) solution to our problem.
An array is basically just a collection of variables. Where before we'd declare a separate variable for each number we'd wish to store, an array lets us have but one variable name, but multiple storage location (like PO Boxes– they're all located at the same “place”, the post office, but there are multiple boxes for storing individual values). Addressing/location of information can be greatly simplified through the use of arrays.
An array is what is known as a homogeneous composite data type. It technically is a modifier (or adjective, if you will) of any valid variable data type. We basically use it to say “I'll take X of these”, and slap one name on all of them.
homogeneous means all of the same, indicating it can ONLY contain variables of the exact same type (such as only integers, only short integers, only floats, only chars, etc.)… composite indicates “made up of various parts or elements”, or that it is a “container”… it is like a pack of Necco wafers… when you think about it, a pack of Necco wafers ONLY contains necco wafers. You don't see a necco wafer, a pez, an M&M, etc. you ONLY see Necco wafers in a pack of Necco wafers… therefore, a pack of Necco wafers is the same as our array being described (in concept).
An array has a few other requirements:
Let us see our array in action:
int numbers[4];
This code will declare a new variable, of type int, called numbers… the brackets indicate we are allocating four consecutive int-sized units that will be associated with this variable called numbers… that is, numbers is a post office with 4 mailboxes, addressed 0, 1, 2, 3 (0-3 is 4 distinct values).
To access the first box, we access the 0 offset; the second box is right next to the first, at offset 1 (it is one int away from the first one). Similar with the third and fourth.
To place a 17 in the first (position 0) element of our numbers array, we'd say the following:
numbers[0] = 17;
To place a 36 in the third (position 2) element of our numbers array, we'd say:
numbers[2] = 36;
Because the array index is a number, and things like ints are numbers, we can also specify the array location via a variable. To wit, we will assign a 52 to the fourth array element, but do so via an index variable we set up:
int index = 3; numbers[index] = 52;
Because index contains a 3, we're telling the computer we wish to put a 52 in the array element indicated in the index variable (the fourth element).
As well, because we are putting values in our array elements that conform to particular data types, we can use variables there as well (in this case, put a 96 into the second array element– using variables to store both the index and the value):
int value = 96; int index = 1; numbers[index] = value;
Hopefully these examples have proved useful with respect to basic concepts and syntactic usage of arrays.
We now explore the productive collaboration of arrays and loops:
To really make the most out of arrays in scaling our algorithms, using them in conjunction with loops gives us the most bang for our buck. The advantage of arrays+loops is that with the ONE consistent variable name, representing many NUMERICALLY-identifiable elements, we can work with ranges of data sets without the need to make tons of exceptions for each possible use case (worst case we just make an array of the maximum expected size, and only use what we need).
To illustrate, here we will declare an 11 element array (called data), and fill each element with the value 42 using a for loop:
int data[11], position = 0; for(position = 0; position < 11; position=position+1) // see, using long form, could have done "position++" { data[position] = 42; }
What if we wanted to print the contents of the array? Once again, we use a loop, and print out each value, one at a time.
Important considerations:
… therefore, we have all the ingredients for a for loop:
for (position = 0; position < 11; position++) { fprintf(stdout, "%d ", data[position]); } fprintf(stdout, "\n"); // what important role does this line play?
This should result in the following program output:
42 42 42 42 42 42 42 42 42 42 42
What if we wanted to display the contents of our array in reverse (from position 10 to position 9, to 8, down to 0)?
We'd still want to use a loop, but look at how we structure it:
for (position = 10; position >= 0; position--) { fprintf(stdout, "%d ", data[position]); } fprintf(stdout, "\n"); // what important role does this line play?
Notice how the loop-terminating relational statements differ (comparing the two– for forward and backward, does it make sense?), and also how we progress between individual elements (in one we are incrementing, in this recent one we are decrementing).
That should make sense before you try to proceed.
Using arrays in your algorithms represents a potential barrier you have to overcome. Up until this point, we've been getting used to labelling all our variables with unique, individual names.
Now, with arrays, we have one common name, distinguishable by its element offset. That has been known to cause some conceptual problems due to the mildly abstract nature it creates. It would certainly not hurt to draw some pictures and manually work through some examples, step-by-step… it may be confusing at first, but the more you play with it, ask questions, play, read, etc., the sooner things will start to click.
As some of you have started to realize with mbe0, the actual core work of the project wasn't actually that hard, once you saw through the illusion of complexity we had placed in front of it. By using arrays, we can make our solutions even easier (and code even simpler)… but, we will initially have to eliminate self-imposed mental obstacles making the problem appear significantly more difficult than it actually is.
In mbe0, we specifically looked at 3 usage cases for our mental math problem: 1-, 2-, and 3-digit number. I limited it to those because, lacking arrays and loops for that project, the code would have gotten impossibly long and complex, plus: I wanted you to focus on the basics of variable usage and if-statements.
Now that we have those down, we can now apply arrays and loops to optimize and enhance a solution, and to allow it to scale to a wider range of possibilities (why limit ourselves to just 1-, 2-, and 3-digit values? Once we see the pattern, we can apply this to 4-, 5-, 6-digit numbers and beyond).
Again, to review, let's look at a 3-digit example. 123 x 11:
123 x 11 = 1 (1 + 2) (2 + 3) 3 = (1 + 0) (3 + 0) 5 3 (what are those + 0's? Carry values.) = 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 (11)+1 (13)+0 7 the outside numbers are the carry values = 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.
Now let us process a 4-digit example (look for similarities to the 3-digit process, specifically how this is merely an expansion, or an additional step– due to the additional digit):
4567 x 11:
4567 x 11 = 4 (4 + 5) (5 + 6) (6 + 7) 7 = (4)+1 (9)+1 (11)+1 (13)+0 7 the numbers outside are the carry = 5 0 2 3 7 = 50237
Remember, we are processing this from right to left (so that the carry values can properly propagate). While there is no initial carry coming in, we'll add one anyway (0), so we see 13+0 (which is simply 13)… but because we're interested in place values, this is actually a sum of 3, carry of 1… and that one gets sent over to the next place (which has an 11)… so 11+1 will be 12, or sum of 2, carry 1… that carry will propagate to the next position to the left (the 9)… so there's a rippling effect taking place (math in action).
Can you see how “the same” this process for 4-digit numbers is when comparing to the process for 3-digit numbers? And how the same comparison can be made for 2-digit, and 5-digit, 6-digit, etc.? Please take some time, working through some examples (by hand) to identify and notice the pattern, or essence, of this process. You need to see how it doesn't matter in the long run how many digits- because you're doing the same thing (just a different number of times).
That “different number of times” will be based on the length of the number… could that be used to help us?
(Also, the potential exception here would possibly be 1-digit values… if you cannot easily find a way to make 1-digit numbers work with greater-than-1-digit numbers, that's where an if-statement would come into play– if 1-digit, do this specific process, else do the regular process). I'm not saying one universal solution isn't possible, but at this stage of your structured programming development, such solutions may take a bit more work (and that's okay).
It is your task to write an optimized version of your multiply by eleven program that will use arrays and loops to enable you to enhance and expand the functional capabilities of your program. No longer will you be limited by 1-, 2-, or 3-digit numbers, but you will be able to input up to 8-digit numbers and have your program successfully determine the result (and 8 is merely an arbitrary value I picked, you should easily be able to up it to the tens of thousands and experience no change in functionality) – actually, our 8-digit limit is considering a data type limitation… the maximum size of an int: signed ints can have a maximum value of 2.4 billion, so unless we change to a different data type (or different method of inputting the source number), this will be our limitation.
Your program should:
Several operating behaviors are shown as examples.
An eight digit value:
lab46:~/src/cprog/mbe1$ ./mbe1 Enter value: 31415926 Digits detected: 8 Obtaining unique digits, storing in array... digit[0] = 6 digit[1] = 2 digit[2] = 9 digit[3] = 5 digit[4] = 1 digit[5] = 4 digit[6] = 1 digit[7] = 3 Applying process... result[0] = 6 + 0 + 0 (sum of 6, carry out of 0) result[1] = 2 + 6 + 0 (sum of 8, carry out of 0) result[2] = 9 + 2 + 0 (sum of 1, carry out of 1) result[3] = 5 + 9 + 1 (sum of 5, carry out of 1) result[4] = 1 + 5 + 1 (sum of 7, carry out of 0) result[5] = 4 + 1 + 0 (sum of 5, carry out of 0) result[6] = 1 + 4 + 0 (sum of 5, carry out of 0) result[7] = 3 + 1 + 0 (sum of 4, carry out of 0) result[8] = 3 + 0 + 0 (sum of 3, carry out of 0) Displaying result... 31415926 x 11 = 345575186 lab46:~/src/cprog/mbe1$
Next, a four digit value:
lab46:~/src/cprog/mbe1$ ./mbe1 Enter value: 7104 Digits detected: 4 Obtaining unique digits, storing in array... digit[0] = 4 digit[1] = 0 digit[2] = 1 digit[3] = 7 Applying process... result[0] = 4 + 0 + 0 (sum of 4, carry out of 0) result[1] = 0 + 4 + 0 (sum of 4, carry out of 0) result[2] = 1 + 0 + 0 (sum of 1, carry out of 0) result[3] = 7 + 1 + 0 (sum of 8, carry out of 0) result[4] = 7 + 0 + 0 (sum of 7, carry out of 0) Displaying result... 7104 x 11 = 78144 lab46:~/src/cprog/mbe1$
Finally, a five digit value:
lab46:~/src/cprog/mbe1$ ./mbe1 Enter value: 56789 Digits detected: 5 Obtaining unique digits, storing in array... digit[0] = 9 digit[1] = 8 digit[2] = 7 digit[3] = 6 digit[4] = 5 Applying process... result[0] = 9 + 0 + 0 (sum of 9, carry out of 0) result[1] = 8 + 9 + 0 (sum of 7, carry out of 1) result[2] = 7 + 8 + 1 (sum of 6, carry out of 1) result[3] = 6 + 7 + 1 (sum of 4, carry out of 1) result[4] = 5 + 6 + 1 (sum of 2, carry out of 1) result[5] = 5 + 1 + 0 (sum of 6, carry out of 0) Displaying result... 56789 x 11 = 624679 lab46:~/src/cprog/mbe1$
The execution of the program is short and simple- obtain the input, do the processing, produce the output, and then terminate.
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 mbe1 mbe1.c Submitting cprog project "mbe1": -> mbe1.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.