Repeated addition

This is a discussion on Repeated addition within the C++ Programming forums, part of the General Programming Boards category; Write a program which will ask for two integer numbers (a and b). The program will multiply these two numbers, ...

  1. #1
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72

    Repeated addition

    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.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You should increase the result variable by A, B number of times.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    You could use a 'for' loop.

  4. #4
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I cannot use a 'for loop' but I can use a 'do while loop'.
    Last edited by BB89; 02-11-2010 at 08:36 PM.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    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

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by BB89 View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by iMalc View Post
    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.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I did say that I couldn't use a 'for loop'.

    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.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    And we believe you! No one ever suggested you use a "for" loop.

    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?

  11. #11
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Yes, I can use a while loop.

  12. #12
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    I think I am on the right track now.

    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
    5. Add result to A
    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

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    Yes, you're printing out the result on every iteration.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Get your cout statement out of the while loop. Put it right after it ends if you want just the final answer printed.
    The keyboard is the standard device used to cause computer errors!

  15. #15
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    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"?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Repeated Addition
    By rocksteady in forum C Programming
    Replies: 15
    Last Post: 12-11-2007, 06:05 AM
  2. PC Issue: Repeated Long Beeps
    By alphaoide in forum Tech Board
    Replies: 8
    Last Post: 07-19-2005, 08:46 AM
  3. need some help with binary addition.
    By InvariantLoop in forum C++ Programming
    Replies: 21
    Last Post: 01-27-2005, 05:52 AM
  4. Check for repeated digits!
    By CrackerJack in forum C Programming
    Replies: 5
    Last Post: 11-08-2003, 10:37 PM
  5. Problems with an addition operator
    By jamjar in forum C++ Programming
    Replies: 12
    Last Post: 03-28-2003, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21