fuction to divide by three

Discussion in 'C Programming' started by rajm2019@gmail.com, May 26, 2007.

  1. Guest

    Without using /,% and * operators. write a function to divide a
    number by 3.
    itoa() function is available
    , May 26, 2007
    #1
    1. Advertising

  2. writes:
    > Without using /,% and * operators. write a function to divide a
    > number by 3.


    Do your own homework.

    --
    Regards,
    Hallvard
    Hallvard B Furuseth, May 26, 2007
    #2
    1. Advertising

  3. Don Bruder Guest

    In article <>,
    wrote:

    > Without using /,% and * operators. write a function to divide a
    > number by 3.
    > itoa() function is available


    Who needs itoa()? Assuming integer division and only positive inputs,
    it's trivial, although not precisely efficient, or necessarily fast, to
    simply do repetitive subtraction.

    --
    Don Bruder - - If your "From:" address isn't on my whitelist,
    or the subject of the message doesn't contain the exact text "PopperAndShadow"
    somewhere, any message sent to this address will go in the garbage without my
    ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info
    Don Bruder, May 26, 2007
    #3
  4. Don Bruder Guest

    In article <>,
    Hallvard B Furuseth <> wrote:

    > writes:
    > > Without using /,% and * operators. write a function to divide a
    > > number by 3.

    >
    > Do your own homework.


    Hey! I just had a great idea... Let's see if this one gets him dinged:

    int DivBy3(int x)
    {
    int q;

    for (q=0;x>2;x-=3,q++);
    return q;
    }





    (ROT-13)
    Bs pbhefr, guvf yvggyr "dhvpx-a-qvegl" jvyy bayl jbex sbe cbfvgvir
    vachgf, naq vg'f tbvat gb or (pbzcnengviryl fcrnxvat) fybjre guna gur
    frpbaq pbzvat sbe ovt vachgf, ohg vg'yy qb gur wbo. Naq vg fubhyq or
    "nqinaprq" rabhtu gung gur grnpure jvyy fync uvz sbe boivbhf purngvat vs
    ur npghnyyl hfrf vg - n pynff ng gur yriry guvf bar frrzf gb or onfrq ba
    uvf "qb zl ubzrjbex sbe zr" erdhrfgf jba'g unir gur svefg pyhr nobhg gur
    "snapl" gevpx bs penzzvat vg nyy vagb n sbe () ybbc (vs gurl'ir rira
    yrnearq nobhg gurz ng nyy lrg) naq jvgu uvf qvfcynlrq yriry bs
    vtabenapr, V unir ab qbhog ur'yy or hanoyr gb rira ORTVA rkcynvavat ubj
    vg jbexf jura gur grnpure nfxf uvz. Ubcrshyyl rafhevat ng yrnfg na S ba
    guvf nffvtazrag sbe purngvat.

    --
    Don Bruder - - If your "From:" address isn't on my whitelist,
    or the subject of the message doesn't contain the exact text "PopperAndShadow"
    somewhere, any message sent to this address will go in the garbage without my
    ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info
    Don Bruder, May 26, 2007
    #4
  5. Don Bruder Guest

    In article <>,
    CBFalconer <> wrote:

    > wrote:
    > >
    > > Without using /,% and * operators. write a function to divide a
    > > number by 3.
    > > itoa() function is available

    >
    > d = 0;
    > while ((a -= 3) > 0) d++;
    >
    > (I think this is such as not to be assumed the poor students work)


    Heh... GMTA :)

    --
    Don Bruder - - If your "From:" address isn't on my whitelist,
    or the subject of the message doesn't contain the exact text "PopperAndShadow"
    somewhere, any message sent to this address will go in the garbage without my
    ever knowing it arrived. Sorry... <http://www.sonic.net/~dakidd> for more info
    Don Bruder, May 26, 2007
    #5
  6. CBFalconer said:

    > wrote:
    >>
    >> Without using /,% and * operators. write a function to divide a
    >> number by 3.
    >> itoa() function is available

    >
    > d = 0;
    > while ((a -= 3) > 0) d++;
    >
    > (I think this is such as not to be assumed the poor students work)


    Since the itoa function is available, wouldn't it be simpler to use:

    result_of_division_by_3 = itoa(n);

    Of course, this assumes that itoa divides its numeric input by 3, but
    that's not an unreasonable assumption, given the question text.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, May 26, 2007
    #6
  7. Clark Cox Guest

    On 2007-05-26 07:30:18 -0700, said:

    > Without using /,% and * operators. write a function to divide a
    > number by 3.
    > itoa() function is available


    Here ya' go; enjoy:

    #include <stdbool.h>
    #include <stdio.h>
    #include <complex.h>

    int divide(int n, unsigned d)
    {
    int j,l;
    for(j=0;j<d;j-=(int)cpow(I,2));
    {
    int k=0;
    if(n>=0) for(k=n,l=0;k>=j;k-=j,++l);
    else for(k=-n,l=0;k>=j;k-=j,--l);
    }

    return l;
    }

    int divide_by_3(int i)
    {
    return divide(i, 3);
    }

    int main()
    {
    int i;

    while(true)
    {
    printf("Enter a number (enter a non-number to quit): ");
    fflush(stdout);

    if(scanf("%d", &i) != 1)
    {
    return 0;
    }
    else
    {
    printf("%d / 3 == %d\n", i, divide_by_3(i));
    }
    }
    }

    --
    Clark S. Cox III
    Clark Cox, May 26, 2007
    #7
  8. On May 26, 3:30 pm, wrote:
    > Without using /,% and * operators. write a function to divide a
    > number by 3.
    > itoa() function is available


    double divide_by_3 (double x) {
    return x > 0.0 ? exp (log (x) - log (3.0)) :
    x < 0.0 ? -exp (log (-x) - log (3.0)) : x;
    }
    christian.bau, May 28, 2007
    #8
  9. Eric Sosman Guest

    wrote On 05/26/07 10:30,:
    > Without using /,% and * operators. write a function to divide a
    > number by 3.
    > itoa() function is available


    #include <stdlib.h>
    int divideBy3(int aNumber) {
    div_t d = div(aNumber, 3);
    return d.quot;
    }

    --
    Eric Sosman, May 29, 2007
    #9
  10. wrote:
    >
    > Without using /,% and * operators. write a function to divide a
    > number by 3.
    > itoa() function is available


    Untested:

    int DivideByThree(int number)
    {
    char mybuf[64];
    sprintf(mybuf,"exit `expr %d / 3`",number);
    return( system(mybuf) );
    }

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, May 29, 2007
    #10
  11. user923005 Guest

    On May 28, 3:15 pm, "christian.bau" <>
    wrote:
    > On May 26, 3:30 pm, wrote:
    >
    > > Without using /,% and * operators. write a function to divide a
    > > number by 3.
    > > itoa() function is available

    >
    > double divide_by_3 (double x) {
    > return x > 0.0 ? exp (log (x) - log (3.0)) :
    > x < 0.0 ? -exp (log (-x) - log (3.0)) : x;
    > }


    I like this one. I do see a minor problem for x == 0.
    user923005, May 29, 2007
    #11
  12. user923005 Guest

    On May 29, 11:21 am, user923005 <> wrote:
    > On May 28, 3:15 pm, "christian.bau" <>
    > wrote:
    >
    > > On May 26, 3:30 pm, wrote:

    >
    > > > Without using /,% and * operators. write a function to divide a
    > > > number by 3.
    > > > itoa() function is available

    >
    > > double divide_by_3 (double x) {
    > > return x > 0.0 ? exp (log (x) - log (3.0)) :
    > > x < 0.0 ? -exp (log (-x) - log (3.0)) : x;
    > > }

    >
    > I like this one. I do see a minor problem for x == 0.


    As the wise baboon in "The Lion King" or Ben Pfaff might say:
    "Look Harder."

    Indeed, the matter is cared for.
    user923005, May 29, 2007
    #12
    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. mike
    Replies:
    3
    Views:
    364
    Chris Botha
    Nov 16, 2005
  2. somez72
    Replies:
    4
    Views:
    4,348
    Roedy Green
    May 3, 2004
  3. MiniDisc_2k2
    Replies:
    2
    Views:
    385
    MiniDisc_2k2
    Jul 3, 2003
  4. orion30
    Replies:
    2
    Views:
    304
    Alf P. Steinbach
    Jul 14, 2003
  5. Cat
    Replies:
    2
    Views:
    394
    llewelly
    Aug 20, 2003
Loading...

Share This Page