Thread: while

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    25

    while

    hi guys. im having a hard time figuring this out. could someone help me please?
    How could I write the code for

    Product of a & b with the use of addition operator only.


    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by laserlight View Post
    What have you tried?
    Honestly, I dont know what to do.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Have you not learned anything from posting here in the past? You need to show some effort, this seems to be a recurring problem. Did you learn how to count yet?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I presume a and b are only expected to be non-negative integers. So, choose some such values, substitute and then figure out how you would express that product as addition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Thank you.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by bummielove
    Product of a & b with the use of addition operator only.
    Quote Originally Posted by laserlight
    So, choose some such values, substitute and then figure out how you would express that product as addition.
    Quote Originally Posted by bummielove
    Thank you.
    Laser: Your problem solving skills continue to amaze me.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by laserlight View Post
    I presume a and b are only expected to be non-negative integers. So, choose some such values, substitute and then figure out how you would express that product as addition.
    Code:
    while(x<=b)
    	{
    		e1=e1+a;
    		x++;
    	}
    
    	for(x=1;x<=a;x++)
    	e2=e2*x;
    	for(x=1;x<=b;x++)
    	e3=e3*a;
    	if(b>0)

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I see '*' in that code, which you were forbidden to use, no?

    You need to regress back to when you were eight years old and learning what multiplication was for the first time.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So, where did you take that code from? I would like to point out a couple of things:

    1. This code has nothing to do with your problem.
    2. Do not just post random code and then expect someone to post the correct code for you.
    3. Read the homework policy.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    He is likely hoping Common Tator posts a good answer again.

    Tim S.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @OP: Try this. Write down, in words, the steps you would use to solve this problem. Then share it with us. Then some of us might begin to help you turn it into code.

    If you can't write it down in words for us, then this is actually a math-related problem and not a program-related problem.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by stahta01 View Post
    He is likely hoping Common Tator posts a good answer again.
    Tim S.
    Lol......yeah he probably is. Well, let's see:

    Common Tater: DO NOT POST A SOLUTION

    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    25
    Quote Originally Posted by Matticus View Post
    @OP: Try this. Write down, in words, the steps you would use to solve this problem. Then share it with us. Then some of us might begin to help you turn it into code.

    If you can't write it down in words for us, then this is actually a math-related problem and not a program-related problem.
    so for example, a=3 and b =2
    ans = 3+3.
    My problem is how to begin the loop.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You have a concrete example, but an example is not an algorithm. Go back to a and b. How would you express a * b using addition, in words?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed