hailstones program, help please
Author 
Message 
Ivan Poo #1 / 13

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 


Daniel Barke #2 / 13

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 nonzero, 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 


David Hoelz #3 / 13

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 


Martin Ambuh #4 / 13

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 


Dann Corbi #5 / 13

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 CFAQ: http://www.eskimo.com/~scs/Cfaq/top.html CFAQ ftp: ftp://rtfm.mit.edu, CFAQ Book: ISBN 0201845199 Try "C Programming: A Modern Approach" ISBN 0393969452 Want Software? Algorithms? Pubs? http://www.infoseek.com

Sat, 26 May 2001 03:00:00 GMT 


Jim Clin #6 / 13

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 


Ivan Poo #7 / 13

hailstones program, help please
Oops, heh heh , fixed my email 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 


Martin Ambuh #8 / 13

hailstones program, help please
Quote:
> Oops, heh heh , fixed my email 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 


Greg Come #9 / 13

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, 9134 120th Street, Richmond Hill, NY, 114183214 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 


Igor Rafienk #10 / 13

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 


Jack Kle #11 / 13

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> 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 


Lawrence Kir #12 / 13

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 crossposted to comp.lang.c, not comp.lang.c++.  


Sun, 27 May 2001 03:00:00 GMT 


William Roede #13 / 13

hailstones program, help please
Quote:
> > Oops, heh heh , fixed my email 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 


