I have to create a program that prints all odd or even integers between any two integers(inclusive) entered by the user.

I'm sure I have to use some type of function for this, but which one? usually my teacher notes which funtion i have to use, but not this time.

08-12-2003

nickname_changed

I think in this case your supposed to use ur own. Have you heard of the modulous (%) operator? You use this to find the 'remainder' of a number divided by another number. For example:
5/3 is 1, with 2 left over (remainder). Doing 5%3 will give you 2 in the same way (I'm sure you know all about remainders etc, but im just saying it so you know what modulous does.

So, if you take a number like 30, and do 30%2, it will give 0 because theres 0 remainders. If you did 31, it would give you 1 (remainder 1).

If I was you, I'd ask for a start number, and end number, check that the end number is greater than the start number (or else there will be a never-ending loop), and then do the number %2, if its 1 I wont print, 0 i will print it (because its even).

Is that clear or would you like an example?

08-12-2003

Guti14

Yes, wev'e come across the modulus function already before. So I'm kind of familiar with it and understand the concept.

However, I don't really fully understand your last paragraph. How do I check that the end number is greater than the starting number?
In this case the user must decide what the start and ending number is.
Could you provide me with an example to make things more clearer please?

Now, I never checked any of this so there might be an error, but thats basically what I meant. It loops while the current number is less than or equal to the end number, and if its even it prints it.

You have to check that EndNumber is greater that StartNumber, because if its not, can you see that X (since it starts at the StartNumber) will always be greater than the EndNumber, so the loop just wont run.

However, It all depends how you do it. If the loop had said (X != EndNumber), it would work just the same if EndNumber was greater than startNumber, but if EndNumber was less, X would never equal EndNumber, and so it would be a never ending loop :) Hope I havent made it worse for you.

Now, I never checked any of this so there might be an error, but thats basically what I meant. It loops while the current number is less than or equal to the end number, and if its even it prints it.

You have to check that EndNumber is greater that StartNumber, because if its not, can you see that X (since it starts at the StartNumber) will always be greater than the EndNumber, so the loop just wont run.

However, It all depends how you do it. If the loop had said (X != EndNumber), it would work just the same if EndNumber was greater than startNumber, but if EndNumber was less, X would never equal EndNumber, and so it would be a never ending loop :) Hope I havent made it worse for you.

~ Paul

i feel your solution is inefficient.. instead of using % you can run a loop and incremeting the variable by 2 every time.. Thats a better alternative

if (EndNumber < StartNumber)
{
cout << "End number is less than start number. Please try again";
// either return here, or do something to allow them to chose the end number again. Up to you
}

for(int i=StartNumber;i<=EndNumber;i+=2)
cout << i << endl;

return 0;
}

this code will print all odd numbers if the start number is odd or even if the start number is even... this is more efficient way of looping since the lopp needs to run just half the number of times when compared to stovellp solution and does not need exta calculations to do the %(mod) stuff...

08-12-2003

Tada

Another method assuming a calculation will need to be made, which i don't believe has ben mentioned yet is to check if the first bit of the valut is set, which is equvilant to decimal 1, which causes a number to be odd. I expect this method would have some portability problems, and the correct mathematical method would indeed be the one already mentioned, but in a computing situation, this method may prove quicker in the context of integers.

eg.

8421

1000 = 8+0+0+0 = 8
1001 = 9+0+0+1 = 9

In C, a simple (number & 1) statement using bitwise And to check the first bit would suffice.

I felt this was worth mentioning, and also to serve as my first post here :p

08-12-2003

JaWiB

you can also check to see if a number is odd:

Code:

if (num&1)
//is odd

if (!(num&1))
//is even

:p

08-13-2003

Guti14

Thank you, your examples were very helpful, only, the user must decide wether only odd or only even numbers should be printed.

I suppose I must use cout, but for example if the user presses 1 to display just even numbers and 2 for only odd, how do I connect their choice to the loop that will display only the even or odd numbers?

08-13-2003

7stud

Nice job people. I'm sure the poster learned alot about programming by having you post the solutions to his/her homework.

08-13-2003

bennyandthejets

Hopefully Guti14 understands everything people have posted, and has used it to further his/her programming abilities. Hopefully.

In my opinion efficiency should not be considered here, because it's such a simple program. The most logical way to go about it is to start with the first number, check if it's odd/even as you want, then print, adding two until the limit is reached. Checking bit states is a waste of time, because adding two to an integer is surely quicker under the hood then doing bitwise operations. Plus, why increment a number AND do a bitwise operation? And for that matter, a modulus operation?

Just speculating here, but wouldn't modulus be the slowest method, as it involves dvision?

08-13-2003

RoD

Quote:

Originally posted by 7stud Nice job people. I'm sure the poster learned alot about programming by having you post the solutions to his/her homework.

Seeing it helps people learn, you cant just tell them what they need sometimes, u have to guide. Lighten up.

08-13-2003

FillYourBrain

RoD, you can guide without giving answers. you have to answer questions here with some care in detecting homework questions. Doing someone's homework isn't acceptable.

08-13-2003

Guti14

I am not expecting people to do my homework here for me. Just some tips or hints as in what function I could/should use for instance, or give me some pseudocode. That's what my teacher does too sometimes, never the code, just some pseudocode.
Occasionally I would like an example, sometimes it gets really difficult. Iv'e never done any programming before, and I had to dive straight into C++.

08-13-2003

7stud

"I am not expecting people to do my homework here for me. Just some tips or hints"

It's not your fault, and that's a good approach--unfortunately some posters just don't get it.