Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-10-2007
Hello everyone,

I'm trying to write pseudocode for a function that uses repeated addition instead of multiplication. In other words, inputting 2 and 5 should return 10, using only addition.

So far, I've thought of using one of the inputted numbers as a counter, but I don't know how this would be useful without the multiplication operator. I have some code, but it doesn't address how I can get the sum. I really need help getting direction with this program. If it helps, I think we are expected to use a recursive function.

Code:

```#include <stdio.h> float sum (float number1, float number2) {     float sum;            /* sum of the two input numbers */         sum = ??;         return (sum); }```
Thank you all in advance!
• 12-10-2007
Elysia
Recursion is not necessary for this.
Use a for loop.
• 12-10-2007
tabstop
Quote:

Originally Posted by rocksteady
Hello everyone,

I'm trying to write pseudocode for a function that uses repeated addition instead of multiplication. In other words, inputting 2 and 5 should return 10, using only addition.

So far, I've thought of using one of the inputted numbers as a counter, but I don't know how this would be useful without the multiplication operator. I have some code, but it doesn't address how I can get the sum. I really need help getting direction with this program. If it helps, I think we are expected to use a recursive function.

Good ideas, as far as the first sentence goes. I suppose you could use a recursive function for this. You could also use a circular saw to cut your fingernails. (In other words: way more tool than you actually need, and I suggest you only look that way if the assignment explicitly asks for it.)

Code:

```#include <stdio.h> float sum (float number1, float number2) {     float sum;            /* sum of the two input numbers */         sum = ??;         return (sum); }```
So, why sum? You don't want to return the sum (2+5=7, but you're supposed to return 10). Think back, way back, to the first time you ever saw multiplication. What is multiplication, and how does it relate to adding? When you answer that, you'll be much farther.

(PS: floats? Are you sure we aren't talking (unsigned) integers?)
• 12-10-2007
Edited: hindsight is 20/20 :(
• 12-10-2007
Elysia
A for loop repeats a number of specified times.
How would you multiplication in the real world with only using +?
• 12-10-2007
Here is my revised code:

Code:

``` int sum (int x, int y) {         int sum;        /* sum of the two input numbers */         for (sum=0; x<y; sum+y)                 x--;         return (sum); }```
How does it look?
• 12-10-2007
Elysia
And what does that do? Not a darn thing but an infinite loop.
What does the code look to you?
• 12-10-2007
I meant for it to add an input value to a variable initialized to 0, multiple times. Should I change the condition so that x>=0?
• 12-10-2007
kcpilot
if x=2 and y=5, how would you code that in a for loop to make the sum = 10?

Start with this: outside the for loop, sum = 0

for x times you are going to add y to sum, right?
• 12-10-2007
robwhit
Quote:

Originally Posted by rocksteady
Code:

`    for (sum=0; x<y; sum+y)`

this part has no effect.
• 12-10-2007
zacs7
Don't name variables the same as function names, it's silly.

Keep track of the total, it might look something like:
Code:

```#include <math.h> float sumf(float a, float b) {     float total = 0.0f;     int cycles =    (int) ceil(b),                     i = 0;         for(i = 0; i < cycles; i++)         total += a;         return total; }```
Of course if you steal that, you'd be a theif.
• 12-10-2007
Elysia
Are we going to give out the answer again? :)
• 12-11-2007
zacs7
Yes because I cbf, I'm not giving it away -- you can find things like that everywhere on the net. Unless he understands it, stealing will do him more harm than good.
• 12-11-2007
Elysia
Quote:

Originally Posted by zacs7
Unless he understands it, stealing will do him more harm than good.

So it will... So it will...
• 12-11-2007
Salem
> Of course if you steal that, you'd be a theif.
On the plus side, at least it isn't recursive (that's apparently a requirement).
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last