Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-11-2010
BB89
Write a program which will ask for two integer numbers (a and b). The program will multiply these two numbers, a and b, using repeated addition (CANNOT use multiplication operation) and print out the result.

Code:

```#include <iostream> using namespace std; int main() { int A, B, result = 0;     cout << "Enter first number:" << endl;     cin >> A;     cout << "Enter second number:" << endl;     cin >> B;```
This is what I have so far.

I know that 2 * 5 = 10 and 2 + 2 + 2 + 2 + 2 = 10

I am not sure how to write the code.

Code:

`I DON'T want you to give me the answer unless I really really need it to understand.`
• 02-11-2010
_Mike
You should increase the result variable by A, B number of times.
• 02-11-2010
Flaug
You could use a 'for' loop.
• 02-11-2010
BB89
I cannot use a 'for loop' but I can use a 'do while loop'.
• 02-11-2010
_Mike
You can't use a for loop because you don't know how, or because you're not allowed to?
Anyways, if you want to use a while loop you can do
Code:

```add A to result decrease B by one```
while B is greater than 0
• 02-12-2010
iMalc
Quote:

Originally Posted by BB89
I cannot use a 'for loop' but I can use a 'do while loop'.

A do .. while loop is not appropriate here. They are for use when you always want the loop to run at least once. What if the user typed in 9 and 0 and your do .. while loop was controlled by the second number? As per the code _Mike posted, it would give the wrong answer!

Sure you can get around that by sorting the two values first, or by introducing an extra if-statement. In programming however, you're not supposed to look for the hardest or most convoluted way of solving a problem. Sorting the values first can be an optimisation, but that's not called for here.
Note that the original question is legitimate because you may be programming for a very basic device that has no hardware multiplication support.

The most appropriate thing to use is a for-loop. If you're not allowed to use that (in which case why did you not tell us this?), then a while loop is the next best thing.
• 02-12-2010
_Mike
Quote:

Originally Posted by iMalc
A do .. while loop is not appropriate here. They are for use when you always want the loop to run at least once. What if the user typed in 9 and 0 and your do .. while loop was controlled by the second number? As per the code _Mike posted, it would give the wrong answer!

Sorry, I was a bit unclear. I meant a while loop, not a do while loop.
• 02-12-2010
Elysia
It's simple. Write a flowchart, first of all. Then translate to code. This should always be your first step. The logic is not difficult.
• 02-12-2010
BB89
I did say that I couldn't use a 'for loop'.

Quote:

Originally Posted by BB89 View Post
I cannot use a 'for loop' but I can use a 'do while loop'.
I going to work on it, in our class we have to write algorithms before we can even think about code. I am in my first programming class.
• 02-12-2010
And we believe you! No one ever suggested you use a "for" loop.

Quote:

we have to write algorithms before we can even think about code
Which is more or less what Elysia was saying. Just write it out on paper. Forget any programming language, syntax, constructs, etc.

When you read a recipe it doesnt state you need a Brand X pot or Brand Y stove. Those are specific details that are ignored (or abstracted). Similarly, when you're working on this problem, just write it out on paper how you describe to someone how to do it. This general recipe/algorithm is much more useful and difficult than coding ever is. This is precisely why senior software engineers and architects get paid more money than "programmers".

For the actual coding, as someone asked already, a "do-while" loop isnt as appropriate as another method. Are you allowed to use a "while" loop instead?
• 02-12-2010
BB89
Yes, I can use a while loop.
• 02-12-2010
BB89
I think I am on the right track now.

Quote:

I wrote and Algorithm but it may not be the most efficient way to write it.

1. int A, B, result;
2. Ask for first number (A)
3. Ask for second number (B)
4. while B > 0
6. decrement B
7. cout << "The result is: " << result << endl;
8. STOP
Now my code
Code:

```#include <iostream> using namespace std; int main() { int A, B, result = 0;     cout << "Enter first number:" << endl;     cin >> A;     cout << "Enter second number:" << endl;     cin >> B;     while( B > 0 )     {     result += A;     B--;     cout << "The result is: " << result << endl;     } }```
Ok, I am getting the correct answer but is it supposed to do this?

Code:

```Say A = 2 and B = 5 The result is: 2 The result is: 4 The result is: 6 The result is: 8 The result is: 10```
• 02-12-2010
Elysia
Yes, you're printing out the result on every iteration.
• 02-12-2010
stumon
Get your cout statement out of the while loop. Put it right after it ends if you want just the final answer printed.
• 02-12-2010
BB89
Ahh, I have one last question.

When I input A as 0 it prints out "The result is: 0" as it should. But when I input B as 0 it stops. Should it print out "The result is: 0"?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last