Not Yet Answered # Finding the product without the (*) multiplication sign

JamesCherrill 2,785 invisal 381 JamesCherrill 2,785 invisal 381 rproffitt 320 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

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?

-1

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 1 Year Ago by invisal*

0

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 1 Year Ago by JamesCherrill*

0

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 1 Year Ago by invisal*

0

@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?

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Sir I have these codes

```
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>
<link rel="stylesheet" href="css/jquery-ui.css">
<script type="text/javascript">
$(function()
{
$( "#coding_language" ).autocomplete({
source: 'autocomplete.php'
});
});
</script>
</head>
<body>
<div id="wrapper">
<div class="ui-widget">
<p>Code <input type="text" id="PT"></p>
<p>Party <input type="text" size="40" id="coding_language"></p>
</div>
</div>
</body>
</html>
```

and autocomplete.php has ...

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```