Basic but complicated problem....Help me Plz!!!

Discussion in 'C Programming' started by Freshman_in_C_Programming, Feb 1, 2006.

  1. Our teacher gave us a problem and it goes this way:

    Create a Turbo C Program that will let a user enter two
    integer numbers. The
    program must display the product of two numbers without
    using multiplication(*)
    operator.

    He also told us to use the "do-while loop."

    I don't know how to solve the problem because i'm just a freshman in C
    programming. I know there are lots of expert programmers in this group,
    so please help me.


    P.S. I can't proceed to sophomore years in programming if I can't solve
    the problem...

    hu hu hu...
    Freshman_in_C_Programming, Feb 1, 2006
    #1
    1. Advertising

  2. Freshman_in_C_Programming wrote:
    > Our teacher gave us a problem and it goes this way:


    Solving homework is not very popular in these parts...

    >
    > Create a Turbo C Program that will let a user enter two
    > integer numbers. The
    > program must display the product of two numbers without
    > using multiplication(*)
    > operator.
    >
    > He also told us to use the "do-while loop."


    Your question is off topic here until you have a specific question
    about standard C language (i.e. no Turbo C extensions). Once you give
    it a try, do come back and ask if you still have problems.

    >
    > I don't know how to solve the problem because i'm just a freshman in C
    > programming. I know there are lots of expert programmers in this group,
    > so please help me.


    It seems that you don't know how to solve this as you're even greener
    in maths. Go back and revise "multiplication". If I were you, I
    wouldn't bother with C until you've master this.

    > P.S. I can't proceed to sophomore years in programming if I can't solve
    > the problem...


    And well you shouldn't! How'd you pass maths in the first place?

    > hu hu hu...


    I bet!

    Cheers

    Vladimir
    Vladimir S. Oka, Feb 1, 2006
    #2
    1. Advertising

  3. Freshman_in_C_Programming

    Pedro Graca Guest

    Freshman_in_C_Programming wrote:
    > Our teacher gave us a problem and it goes this way:
    >
    > Create a Turbo C Program that will let a user enter two
    > integer numbers. The
    > program must display the product of two numbers without
    > using multiplication(*)
    > operator.


    Just remember multiplication is repeated addition:

    6 * 4

    is

    4 + 4 + 4 + 4 + 4 + 4 (6 times)


    For extra credit you might want to turn it around and do

    6 + 6 + 6 + 6 (4 times)


    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
    Pedro Graca, Feb 1, 2006
    #3
  4. Freshman_in_C_Programming

    Barry Guest

    "Vladimir S. Oka" <> wrote in message
    news:...
    > Freshman_in_C_Programming wrote:
    > > Our teacher gave us a problem and it goes this way:

    >
    > Solving homework is not very popular in these parts...
    >
    > >
    > > Create a Turbo C Program that will let a user enter two
    > > integer numbers. The
    > > program must display the product of two numbers without
    > > using multiplication(*)
    > > operator.
    > >
    > > He also told us to use the "do-while loop."

    >
    > Your question is off topic here until you have a specific question
    > about standard C language (i.e. no Turbo C extensions). Once you give
    > it a try, do come back and ask if you still have problems.
    >
    > >
    > > I don't know how to solve the problem because i'm just a freshman in C
    > > programming. I know there are lots of expert programmers in this group,
    > > so please help me.

    >
    > It seems that you don't know how to solve this as you're even greener
    > in maths. Go back and revise "multiplication". If I were you, I
    > wouldn't bother with C until you've master this.
    >
    > > P.S. I can't proceed to sophomore years in programming if I can't solve
    > > the problem...

    >
    > And well you shouldn't! How'd you pass maths in the first place?
    >
    > > hu hu hu...

    >
    > I bet!
    >
    > Cheers
    >
    > Vladimir
    >


    Whip us up a simple example program where

    3.14157 and 6.02*10^23

    are the numbers.
    Barry, Feb 1, 2006
    #4
  5. I tried it many times...but it seems not working.

    My program goes like this:

    _____________________________________________

    #include<stdio.h>
    main()
    {
    int x,y,product;
    product=0;
    clrscr();
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    do{
    product=a*b;
    printf("The product of two numbers is %d", product);
    } while(a==0&&b=0);
    getch();
    }

    _____________________________________________

    That was my original program and it ran correctly.
    Then our teacher told us not to use multiplication operator ( * )....
    and I thought for a moment..."God! can I do that?"
    Does it sound easy...?
    My problem is...how will I initialize the variable product.

    P.S. My first initialization is = product=a*b;
    Freshman_in_C_Programming, Feb 1, 2006
    #5
  6. Freshman_in_C_Programming

    santosh Guest

    scanf("%d %d", &a, &b) ;

    do
    {
    if(b == 0 )
    break ;
    a = a + a ;
    b-- ;
    }while(1) ;


    Enjoy...............
    santosh, Feb 1, 2006
    #6
  7. Freshman_in_C_Programming wrote:
    > I tried it many times...but it seems not working.
    >
    > My program goes like this:


    No it does not!

    Don't re-type your code here. Copy and paste _exact_ code you used.
    This one won't compile and is broken in other ways too. See below.

    BTW, since you re-typed it, you could have fixed that horrible
    indentation.

    > #include<stdio.h>
    > main()


    Avoid implicit `int` and do a proper

    int main(void)

    or

    int main(int argc, char *argv[])

    > {
    > int x,y,product;


    An `int` is not guaranteed to be able to hold the product of two
    `int`s. You'd have been better off declaring `product` as `long`, but
    that would not have been guaranteed to hold your product either (
    sizeof(int) <= sizeof(long) ). You'd really want to check `x` and `y`
    before you multiply them.

    I prefer declarations on separate lines, at least the ones that are
    logically separate:

    int x, y;
    long product;

    Would have been much clearer.

    > product=0;


    First initialisation of `product` (c.f. your own PS).

    > clrscr();


    This is not a standard function. You also don't need it anyway.

    > printf("Enter first number: ");


    You really want to terminate `printf`s with '\n', otherwise there's no
    obligation on the part of the system to output anything.

    > scanf("%d",&a);


    Don't use scanf(). Use fgets()/sscanf() combination instead. Search
    this group for detailed discussion as to why (also why fgets() and
    _not_ gets()).

    Also, you never declared `a` (or `b` for that matter).

    > printf("Enter second number: ");


    You don't seem to...

    > do{


    Here, I'd first check whether a*b can fit into product, and output a
    diagnostic message if it won't.

    > product=a*b;
    > printf("The product of two numbers is %d", product);
    > } while(a==0&&b=0);


    The comparison is broken. You probably meant `b==0`.

    For that matter, relying on specific input parameter values as an exit
    condition is generally not a good idea, and especially when what you do
    with them is actually _defined_.

    > getch();
    > }
    >
    > _____________________________________________
    >
    > That was my original program and it ran correctly.


    No it did not. What does it do if `a` and `b` are both == 0? Or was
    hammering the fact that 0*0 = 0 into the poor user part of the
    assignment? ;-)

    > Then our teacher told us not to use multiplication operator ( * )....
    > and I thought for a moment..."God! can I do that?"


    Oh dear, here we go again... :'-(
    Go back to where they taught you what multiplication is...

    > Does it sound easy...?


    It actually does!

    > My problem is...how will I initialize the variable product.


    No, that's /not/ what your problem is. The problems are listed above.

    > P.S. My first initialization is = product=a*b;


    No it is not. See above.

    Cheers

    Vladimir
    Vladimir S. Oka, Feb 1, 2006
    #7
  8. Vladimir S. Oka wrote:
    > Freshman_in_C_Programming wrote:

    <snip>
    >>printf("Enter first number: ");

    >
    >
    > You really want to terminate `printf`s with '\n', otherwise there's no
    > obligation on the part of the system to output anything.


    Or use the (standard) fflush function.


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Feb 1, 2006
    #8
  9. santosh wrote:
    <snip>
    > do
    > {
    > if(b == 0 )
    > break ;
    > a = a + a ;
    > b-- ;
    > }while(1) ;


    This is clearer:

    while (b > 0) {
    a = a + a;
    b--;
    }


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Feb 1, 2006
    #9
  10. Freshman_in_C_Programming

    Guest

    Repeated addition is a poor way to represent multiplcation. A
    combination of addition and bit shifting is far more efficient.

    long mult(int a,int b) {
    int c,p;
    p = 0;
    while(b > 0) {
    c = b & 0x1;
    if(c==1) { p = p + a; }
    a <<= 1;
    b >>= 1;
    }
    return p;
    }

    The above only works for nonnegative b, but is trivial to modify to
    take this into account.
    , Feb 1, 2006
    #10
  11. Freshman_in_C_Programming

    Stefan Krah Guest

    * santosh <> wrote:
    > scanf("%d %d", &a, &b) ;
    >
    > do
    > {
    > if(b == 0 )
    > break ;
    > a = a + a ;
    > b-- ;
    > }while(1) ;
    >
    > Enjoy...............


    .... while a is repeatedly doubled. Did you actually test your code?


    Stefan Krah
    Stefan Krah, Feb 1, 2006
    #11
  12. Freshman_in_C_Programming

    Jordan Abel Guest

    On 2006-02-01, Barry <> wrote:
    >
    > "Vladimir S. Oka" <> wrote in message
    > news:...
    >> Freshman_in_C_Programming wrote:
    >> > Our teacher gave us a problem and it goes this way:

    >>
    >> Solving homework is not very popular in these parts...
    >>
    >> >
    >> > Create a Turbo C Program that will let a user enter two
    >> > integer numbers. The
    >> > program must display the product of two numbers without
    >> > using multiplication(*)
    >> > operator.
    >> >
    >> > He also told us to use the "do-while loop."

    >>
    >> Your question is off topic here until you have a specific question
    >> about standard C language (i.e. no Turbo C extensions). Once you give
    >> it a try, do come back and ask if you still have problems.
    >>
    >> >
    >> > I don't know how to solve the problem because i'm just a freshman in C
    >> > programming. I know there are lots of expert programmers in this group,
    >> > so please help me.

    >>
    >> It seems that you don't know how to solve this as you're even greener
    >> in maths. Go back and revise "multiplication". If I were you, I
    >> wouldn't bother with C until you've master this.
    >>
    >> > P.S. I can't proceed to sophomore years in programming if I can't solve
    >> > the problem...

    >>
    >> And well you shouldn't! How'd you pass maths in the first place?
    >>
    >> > hu hu hu...

    >>
    >> I bet!
    >>
    >> Cheers
    >>
    >> Vladimir
    >>

    >
    > Whip us up a simple example program where
    >
    > 3.14157 and 6.02*10^23


    Those are some funny-looking "integer numbers" you've got there - one is
    only an integer for lack of precision, the other is certainly not one
    at all.
    Jordan Abel, Feb 1, 2006
    #12
  13. Barry wrote:

    >
    > "Vladimir S. Oka" <> wrote in message
    > news:...
    >> Freshman_in_C_Programming wrote:
    >> > Our teacher gave us a problem and it goes this way:

    >>
    >> Solving homework is not very popular in these parts...
    >>
    >> >
    >> > Create a Turbo C Program that will let a user enter
    >> > two integer numbers. The

    ^^^^^^^^^^^^^^^^^^^
    |
    NB! ---------------+

    >> > program must display the product of two numbers
    >> > without using multiplication(*)
    >> > operator.
    >> >
    >> > He also told us to use the "do-while loop."

    >>
    >> Your question is off topic here until you have a specific question
    >> about standard C language (i.e. no Turbo C extensions). Once you give
    >> it a try, do come back and ask if you still have problems.
    >>
    >> >
    >> > I don't know how to solve the problem because i'm just a freshman
    >> > in C programming. I know there are lots of expert programmers in
    >> > this group, so please help me.

    >>
    >> It seems that you don't know how to solve this as you're even greener
    >> in maths. Go back and revise "multiplication". If I were you, I
    >> wouldn't bother with C until you've master this.
    >>
    >> > P.S. I can't proceed to sophomore years in programming if I can't
    >> > solve the problem...

    >>
    >> And well you shouldn't! How'd you pass maths in the first place?
    >>
    >> > hu hu hu...

    >>
    >> I bet!
    >>
    >> Cheers
    >>
    >> Vladimir
    >>

    >
    > Whip us up a simple example program where


    For the first one I could, for the second one hardly...

    > 3.14157 and 6.02*10^23

    ^^^^^^^^^^ invalid operands to binary ^, says gcc

    ....unless you really meant 6.02e23 (or 6.02 * (10 ^ 23))?

    > are the numbers.


    Please read the OP before trying to be funny.

    Also, if you knew some /more/ maths you'd have known that this too can
    be done without multiplication. Finding out how is left as an exercise
    for the reader...

    Cheers

    Vladimir

    --
    I can read your mind, and you should be ashamed of yourself.
    Vladimir S. Oka, Feb 1, 2006
    #13
  14. Freshman_in_C_Programming

    Default User Guest

    Barry wrote:


    > Whip us up a simple example program where
    >
    > 3.14157 and 6.02*10^23
    >
    > are the numbers.


    I guess you missed the part where the OP said:

    > > > Create a Turbo C Program that will let a user enter
    > > > two integer numbers.



    Last I checked, those numbers you reference aren't integers.




    Brian
    Default User, Feb 1, 2006
    #14
  15. "Freshman_in_C_Programming" <> writes:
    > Our teacher gave us a problem and it goes this way:
    >
    > Create a Turbo C Program that will let a user enter two
    > integer numbers. The
    > program must display the product of two numbers without
    > using multiplication(*)
    > operator.
    >
    > He also told us to use the "do-while loop."
    >
    > I don't know how to solve the problem because i'm just a freshman in C
    > programming. I know there are lots of expert programmers in this group,
    > so please help me.
    >
    >
    > P.S. I can't proceed to sophomore years in programming if I can't solve
    > the problem...


    Give us your instructor's e-mail address so we can submit the solution
    directly. Give us your real name so we can tell him whose homework
    we're doing.

    Or, if you're having trouble, you might consider talking to your
    instructor, or to a teaching assistant, or opening a book.

    > hu hu hu...


    Indeed.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Feb 1, 2006
    #15
  16. Freshman_in_C_Programming wrote:
    >
    > I tried it many times...but it seems not working.
    >
    > My program goes like this:
    >


    Several people have told you to go back and review some maths, in
    particular the definition of multiplication. Basically multiplication
    is repeated addition. In fact, that's exactly how a digital computer
    does it (except it does many operations in parallel to save time).

    You might want to consult Knuth, "The Art of Computer Programming".
    There is no law that you must confine yourself to yout course text.
    (Of course, your course text may well have defined and explained
    multiplication. Have you skipped that part?)

    --
    Julian V. Noble
    Professor Emeritus of Physics

    http://galileo.phys.virginia.edu/~jvn/

    "For there was never yet philosopher that could endure the
    toothache patiently."

    -- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
    Julian V. Noble, Feb 1, 2006
    #16
  17. Julian V. Noble said:

    > Freshman_in_C_Programming wrote:
    >>
    >> I tried it many times...but it seems not working.
    >>
    >> My program goes like this:
    >>

    >
    > Several people have told you to go back and review some maths, in
    > particular the definition of multiplication. Basically multiplication
    > is repeated addition. In fact, that's exactly how a digital computer
    > does it (except it does many operations in parallel to save time).


    I'd be very disappointed in a digital computer that did multiplication by
    repeated addition.

    Consider multiplying 123 by 456. Repeated addition is going to require a
    minimum of 123 operations (a mov, followed by 122 adds). But you can do it
    this way instead:

    101100100
    x 1111011
    ---------
    101100100
    1011001000
    00000000000
    101100100000
    1011001000000
    10110010000000
    101100100000000
    ---------------

    That's a mov, a handful of tests of a bit against 0, and half a dozen
    shifts, plus one add per set bit in one of the multiplicands.

    No doubt a Real Programmer could bum a few more cycles out to make the whole
    thing even more tense, but even without that, there's absolutely no need
    for repeated addition.


    (The posting of this article was somewhat delayed whilst I deleted over a
    kilobyte of junk, typed by a cat!)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Feb 2, 2006
    #17
  18. Freshman_in_C_Programming

    Barry Guest

    "Barry" <> wrote in message
    news:...
    >
    > "Vladimir S. Oka" <> wrote in message
    > news:...
    > > Freshman_in_C_Programming wrote:
    > > > Our teacher gave us a problem and it goes this way:

    > >
    > > Solving homework is not very popular in these parts...
    > >
    > > >
    > > > Create a Turbo C Program that will let a user enter two
    > > > integer numbers. The
    > > > program must display the product of two numbers without
    > > > using multiplication(*)
    > > > operator.
    > > >
    > > > He also told us to use the "do-while loop."

    > >
    > > Your question is off topic here until you have a specific question
    > > about standard C language (i.e. no Turbo C extensions). Once you give
    > > it a try, do come back and ask if you still have problems.
    > >
    > > >
    > > > I don't know how to solve the problem because i'm just a freshman in C
    > > > programming. I know there are lots of expert programmers in this

    group,
    > > > so please help me.

    > >
    > > It seems that you don't know how to solve this as you're even greener
    > > in maths. Go back and revise "multiplication". If I were you, I
    > > wouldn't bother with C until you've master this.
    > >
    > > > P.S. I can't proceed to sophomore years in programming if I can't

    solve
    > > > the problem...

    > >
    > > And well you shouldn't! How'd you pass maths in the first place?
    > >
    > > > hu hu hu...

    > >
    > > I bet!
    > >
    > > Cheers
    > >
    > > Vladimir
    > >

    >
    > Whip us up a simple example program where
    >
    > 3.14157 and 6.02*10^23
    >
    > are the numbers.
    >
    >


    I missed the "integer", sorry.
    Barry, Feb 2, 2006
    #18
  19. Richard Heathfield wrote:
    >
    > Julian V. Noble said:
    >
    > > Freshman_in_C_Programming wrote:
    > >>
    > >> I tried it many times...but it seems not working.
    > >>
    > >> My program goes like this:
    > >>

    > >
    > > Several people have told you to go back and review some maths, in
    > > particular the definition of multiplication. Basically multiplication
    > > is repeated addition. In fact, that's exactly how a digital computer
    > > does it (except it does many operations in parallel to save time).

    >
    > I'd be very disappointed in a digital computer that did multiplication by
    > repeated addition.
    >
    > Consider multiplying 123 by 456. Repeated addition is going to require a
    > minimum of 123 operations (a mov, followed by 122 adds). But you can do it
    > this way instead:
    >
    > 101100100
    > x 1111011
    > ---------
    > 101100100
    > 1011001000
    > 00000000000
    > 101100100000
    > 1011001000000
    > 10110010000000
    > 101100100000000
    > ---------------
    >
    > That's a mov, a handful of tests of a bit against 0, and half a dozen
    > shifts, plus one add per set bit in one of the multiplicands.
    >
    > No doubt a Real Programmer could bum a few more cycles out to make the whole
    > thing even more tense, but even without that, there's absolutely no need
    > for repeated addition.
    >
    > (The posting of this article was somewhat delayed whilst I deleted over a
    > kilobyte of junk, typed by a cat!)
    >


    Of course. That, in fact, is how we learn to do it in grade school (except
    not in binary, most places). If that is not repeated addition, however,
    how would YOUR cat describe it?

    (Said cat sounds intelligent--has it begun writing Shakespearean sonnets
    yet?)


    --
    Julian V. Noble
    Professor Emeritus of Physics

    http://galileo.phys.virginia.edu/~jvn/

    "For there was never yet philosopher that could endure the
    toothache patiently."

    -- Wm. Shakespeare, Much Ado about Nothing. Act v. Sc. 1.
    Julian V. Noble, Feb 2, 2006
    #19
  20. Freshman_in_C_Programming

    Ed Prochak Guest

    Stefan Krah wrote:
    > * santosh <> wrote:
    > > scanf("%d %d", &a, &b) ;
    > >
    > > do
    > > {
    > > if(b == 0 )
    > > break ;
    > > a = a + a ;
    > > b-- ;
    > > }while(1) ;
    > >
    > > Enjoy...............

    >
    > ... while a is repeatedly doubled. Did you actually test your code?
    >
    >
    > Stefan Krah


    SShhh...

    <in a hushed voice...>
    he is trying to hint at the correct answer with out giving it away.
    Ed Prochak, Feb 3, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. brian

    Size of my Struct? PLZ PLZ reply

    brian, Nov 23, 2004, in forum: C Programming
    Replies:
    7
    Views:
    417
    -berlin.de
    Nov 25, 2004
  2. Replies:
    1
    Views:
    382
  3. Replies:
    2
    Views:
    335
    Nick Keighley
    Nov 24, 2006
  4. Replies:
    1
    Views:
    340
  5. nocturnal
    Replies:
    1
    Views:
    523
    nocturnal
    Jul 10, 2009
Loading...

Share This Page