1. ## Repeated Addition

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!

2. Recursion is not necessary for this.
Use a for loop.

3. 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?)

4. Edited: hindsight is 20/20

5. A for loop repeats a number of specified times.
How would you multiplication in the real world with only using +?

6. 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?

7. And what does that do? Not a darn thing but an infinite loop.
What does the code look to you?

8. 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?

9. 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?

10. Originally Posted by rocksteady
Code:
`    for (sum=0; x<y; sum+y)`
this part has no effect.

11. 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;

}```
Of course if you steal that, you'd be a theif.

12. Are we going to give out the answer again?

13. 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.

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

15. > 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).

Popular pages Recent additions