hailstones program, help please 
Author Message
 hailstones program, help please

Hello,
        I'm trying to write the hailstones program where it takes an initial
integer value, then goes through a number of stages until it reaches the
value 1.  Given the current stage, n, the number for the next stage is
calculated by this: if n is odd, replace it with 3n+1, if n is even,
replace it with n/2.  The hardest part for me is the odd/even part.  Is
there any such function that determines of a value is odd or even and
returns a value based on that (or something similar)?

Thanks in advance.

-Ivan



Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

Quote:

> Is there any such function that determines of a value is odd or even and
> returns a value based on that (or something similar)?

Use the modulus operator, %. This gives the remainder when a value is
divided by another. (It doesn't work with float of double types.)

        if ((i % 2) != 0)       /* remainder is non-zero, i is odd */
        {
                ...
        }
        else                    /* remainder is zero, i is even */
        {
                ...
        }

Daniel Barker,
Institute of Cell and Molecular Biology,
Swann Building,
King's Buildings,
Mayfield Road,
Edinburgh
EH9 3JR
UK



Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please
Do it yourself..

#define Even_Odd(x) (x%2 ? 0 : 1)



Quote:
>Hello,
>    I'm trying to write the hailstones program where it takes an initial
>integer value, then goes through a number of stages until it reaches the
>value 1.  Given the current stage, n, the number for the next stage is
>calculated by this: if n is odd, replace it with 3n+1, if n is even,
>replace it with n/2.  The hardest part for me is the odd/even part.  Is
>there any such function that determines of a value is odd or even and
>returns a value based on that (or something similar)?

>Thanks in advance.

>-Ivan



Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

Quote:

> Hello,
>         I'm trying to write the hailstones program where it takes an initial
> integer value, then goes through a number of stages until it reaches the
> value 1.  Given the current stage, n, the number for the next stage is
> calculated by this: if n is odd, replace it with 3n+1, if n is even,
> replace it with n/2.  The hardest part for me is the odd/even part.  Is
> there any such function that determines of a value is odd or even and
> returns a value based on that (or something similar)?

Hint 1: fix your email address if you want personal replies
Hint 2: An even number is divisable by 2
     2a: the '%' operator

--




Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

Quote:


>| Do it yourself..
>|
>| #define Even_Odd(x) (x%2 ? 0 : 1)

>Will

>#define Even_Odd(x) ((x) & 1) ? 0 : 1)

>be as portable?

No.  This gets rehashed at least once every three or four months.

Consider this recent message by Lawrence Kirby:
Re: even and odd?  more options
Author:   Lawrence Kirby

Date: 1998/11/10
Forums: comp.lang.c


writes:
...

Quote:
>#define ODD(n)        ((n)&1)

This isn't portable. For example on 1's complement systems bit 0 of negative
odd numbers is 0. Try this:

 #define ODD(n)        ((n)%2 != 0)

or

 #define ODD(n)        ((unsigned)(n) & 1)

Quote:

>#define EVEN(n)        (!ODD(n))

--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please
There is a function to do it and here it is

int odd_even(int number)
{
    if ((number % 2) != 0)
        return 1; // is odd
    else
        return 0; // is even

Quote:
}

well that is all it takes.  Hope it helps

jim

Quote:

> Hello,
>         I'm trying to write the hailstones program where it takes an initial
> integer value, then goes through a number of stages until it reaches the
> value 1.  Given the current stage, n, the number for the next stage is
> calculated by this: if n is odd, replace it with 3n+1, if n is even,
> replace it with n/2.  The hardest part for me is the odd/even part.  Is
> there any such function that determines of a value is odd or even and
> returns a value based on that (or something similar)?

> Thanks in advance.

> -Ivan



Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

Oops, heh heh , fixed my e-mail address, don't know why it got changed.
Well thanks for teh replys, i'm going to try it out (coulda sworn there
was an even/odd function, oh well) :).

-Ivan

Quote:

> Hello,
>         I'm trying to write the hailstones program where it takes an initial
> integer value, then goes through a number of stages until it reaches the
> value 1.  Given the current stage, n, the number for the next stage is
> calculated by this: if n is odd, replace it with 3n+1, if n is even,
> replace it with n/2.  The hardest part for me is the odd/even part.  Is
> there any such function that determines of a value is odd or even and
> returns a value based on that (or something similar)?

> Thanks in advance.

> -Ivan



Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

Quote:

> Oops, heh heh , fixed my e-mail address, don't know why it got changed.
> Well thanks for teh replys, i'm going to try it out (coulda sworn there
> was an even/odd function, oh well) :).

You probably come from Pascal, then.  It is easy to do.  You probably
want this code to be inline, so the approach is

/* C */
#define is_odd(x) ((x) % 2)
#define is_even(x) !(is_odd(x))

/* C++ */
inline bool is_odd(x)
{
    return (x % 2) != 0;

Quote:
}

inline book is_even(x)
{
    return (x %2) == 0;

Quote:
}

--




Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

Quote:



>> > Hello,
>> >         I'm trying to write the hailstones program where it takes an initial
>> > integer value, then goes through a number of stages until it reaches the
>> > value 1.  Given the current stage, n, the number for the next stage is
>> > calculated by this: if n is odd, replace it with 3n+1, if n is even,
>> > replace it with n/2.  The hardest part for me is the odd/even part.  Is
>> > there any such function that determines of a value is odd or even and
>> > returns a value based on that (or something similar)?
>> There is a function to do it and here it is

>> int odd_even(int number)
>> {
>>     if ((number % 2) != 0)
>>         return 1; // is odd
>>     else
>>         return 0; // is even
>> }

>> well that is all it takes.  Hope it helps

>Exactly the same result:

>int odd_even(int number)
>{
>    return number % 2;
>}

BTW, in C++ one would make this function inline.
Also sounds like a good candidate to return a bool and not an int.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
     Producers of Comeau C/C++ 4.2.38 -- New Release!  We now do Windows too.

                *** WEB: http://www.comeaucomputing.com ***



Sat, 26 May 2001 03:00:00 GMT  
 hailstones program, help please

| Do it yourself..
|
| #define Even_Odd(x) (x%2 ? 0 : 1)

Will

#define Even_Odd(x) ((x) & 1) ? 0 : 1)

be as portable?

igorR
--
Vy vstaete s posteli, a lezhashaya ryadom devushka hvataet vas za ruku i
govorit:
- Save changes? Yes or No?



Sun, 27 May 2001 03:00:00 GMT  
 hailstones program, help please

Quote:

> There is a function to do it and here it is

> int odd_even(int number)
> {
>     if ((number % 2) != 0)
>         return 1; // is odd
>     else
>         return 0; // is even
> }

> well that is all it takes.  Hope it helps

> jim

<Jack>

Exactly the same result:

int odd_even(int number)
{
    return number % 2;

Quote:
}

</Jack>

- Show quoted text -

Quote:

> > Hello,
> >         I'm trying to write the hailstones program where it takes an initial
> > integer value, then goes through a number of stages until it reaches the
> > value 1.  Given the current stage, n, the number for the next stage is
> > calculated by this: if n is odd, replace it with 3n+1, if n is even,
> > replace it with n/2.  The hardest part for me is the odd/even part.  Is
> > there any such function that determines of a value is odd or even and
> > returns a value based on that (or something similar)?

> > Thanks in advance.

> > -Ivan



Sun, 27 May 2001 03:00:00 GMT  
 hailstones program, help please


Quote:

> (Jack Klein) writes:

...
>>> int odd_even(int number)
>>> {
>>>     if ((number % 2) != 0)
>>>         return 1; // is odd
>>>     else
>>>         return 0; // is even
>>> }

>>> well that is all it takes.  Hope it helps

>>Exactly the same result:

>>int odd_even(int number)
>>{
>>    return number % 2;
>>}

Except that it could return -1 when number is odd and negative. If you're
just comparing the result against zero that of course won't make any
difference.

Quote:
>BTW, in C++ one would make this function inline.
>Also sounds like a good candidate to return a bool and not an int.

Ah, but this was cross-posted to comp.lang.c, not comp.lang.c++.

--
-----------------------------------------


-----------------------------------------



Sun, 27 May 2001 03:00:00 GMT  
 hailstones program, help please
Quote:


> > Oops, heh heh , fixed my e-mail address, don't know why it got changed.
> > Well thanks for teh replys, i'm going to try it out (coulda sworn there
> > was an even/odd function, oh well) :).

> You probably come from Pascal, then.  It is easy to do.  You probably
> want this code to be inline, so the approach is

> /* C */
> #define is_odd(x) ((x) % 2)
> #define is_even(x) !(is_odd(x))

> /* C++ */
> inline bool is_odd(x)

template<class T>
inline bool is_odd(T x)
Quote:
> {
>     return (x % 2) != 0;
> }

> inline book is_even(x)

template<class T>
inline book is_even(T x)

Quote:
> {
>     return (x %2) == 0;
> }

> --



--
Bill Roeder
My opinions are my own and do not reflect those of my employer.
Why do people put the above line in their signature?
If they reflected my employer then he would have sent the message!


Mon, 28 May 2001 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

2. Please help!!!!Please help!!!!Please help!!!!

3. Someone Please Help Me - I Help You in Any Software Queries/Programming/Coding

4. Need help on this programming problem ( PLEASE HELP!!!)

5. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

6. PLEASE PLEASE HELP HELP...question on interleaving C functions

7. help: Guru needed please please please

8. C++ .Net error on simple program PLEASE HELP!!

9. I need some help with this program, please!!!!!!!!!!

10. please help with this program

11. PLEASE help UDP client programming

12. PLEASE READ: Help in writing a program (fwd)

 

 
Powered by phpBB® Forum Software