Like Tree17Likes

while

This is a discussion on while within the C Programming forums, part of the General Programming Boards category; hi guys. im having a hard time figuring this out. could someone help me please? How could I write the ...

  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
    22,271
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,866
    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
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,866
    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,185
    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,866
    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.
    stahta01 likes this.
    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
    2,768
    He is likely hoping Common Tator posts a good answer again.

    Tim S.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    @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,866
    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

    CommonTater and Char*Pntr like this.
    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
    22,271
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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