I have nothing to start with.. *sigh&
- 4 Contributors
- forum5 Replies
- 131 Views
- 9 Hours Discussion Span
- comment Latest Post by rproffitt
JamesCherrill 4,045
2 times 3 is 2+2+2
5 times 4 is 5+5+5+5
see a pattern?
see how that can be implemented with a loop?
invisal 381
Like @JamesCherril has stated that the multiplication can easily be expressed with continuous of addition.
So basically a x b = b + b .... b
(continously add b
, a
times).
For example: 3 x 1002 = 1002 + 1002 + 1002.
unsigned int multiply(unsigned int a, unsigned int b) {
unsigned int result = 0;
for (unsigned int i = 0; i < a; i++) {
result += b;
}
return result;
}
Can we be more efficient than this one? We can slightly improve this function further. Noticed that multiply(3, 1002)
run faster than multiply(1002, 3)
. So, we now know that our code run faster if a
is small and b
is large. So, we can swap the b
with a
if b
is smaller than a
unsigned int multiply(unsigned int a, unsigned int b) {
if (a > b) return multiply(b, a);
unsigned int result = 0;
for (unsigned int i = 0; i < a; i++) {
result += b;
}
return result;
}
Everything look good now, but can we do better? Of course, we can do even better. We know that we can compute a x 2^k
very fast without using any multiplication operation using only shift left. So basically, a x 2^k
is the same as a << k
. Using this fact, we can easy compute 100 x 5
as following 100 << 2 + 100
unsigned int multiply2(unsigned int a, unsigned int b) {
unsigned int result = 0;
unsigned int bits = sizeof(unsigned int) * 8;
for (unsigned int i = 0; i < bits; i++) {
if (a & (1 << i)) result += b << i;
}
return result;
}
Sorry that I use C code while the tag specified Pascal. But, I believe you can use the concept and re-implement in your language of choice.
Edited
by invisal
JamesCherrill 4,045
Yeah, well done invisal.
I carefully gave the OP just enough hint so he could benfit from working it out himself, then you went ahead and posted the whole thing, code included. Congratulations, you just replaced a learning opportunity with a cheating opportunity.
Edited
by JamesCherrill
invisal 381
You are making strong assumptions here
* You are assuming that OP asking us to do his homework. (which 99% is more likely to be true).
* You are assuming that giving some explanation along with the code does not create a learning opportunity.
I can agree with the first assumption, but I don't totally agree with the seocnd assumption. There is time code can speak a thousand words. English can be ambiguous while code remove the ambiguity.
Furthermore, assuming that the OP truly cheating and has no motivation to learn, he won't even able to use my code to submit his assignment. The tag clearly stated that he want it in Pascal. I gave him in C/C++. If he is truly beginner with no programming passion, he won't be able to convert the C/C++ to Pascal. If he can convert C/C++ to Pascal, that's mean he at leaat capable of learning.
Lastly, as online forum, we are archiving the resource so that other people can find answer when they are interesting. I hope that, we are not only just a forum with no actual answer and full of hints.
Edited
by invisal
rproffitt 1,523
@all. The debate of whether to be a fish store or a store that has tackle and bait has been kicked around for some time.
So while sharing a C code to a Pascal question is like the OP asking for salmon and getting carp. Maybe it will do fine but it would be funny to read the OP complain with the gifts so far.
I've heard the wails about not supplying the answers over time and for coding forums the usual is to lead the person to find the answer or code the project.
If everyone supplied code then what happens when those that code move on? The forums would have to turn back to teaching how to fish?
Maybe "no actual answer" is the answer. How else will they learn?