function keeps crashing my program???

This topic is 4127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi. I'm trying to create a function that will return false if the argument passed to it is false. Here is what i have:
bool EvenNo(int value)
{
int val = value-1;
for (int i = 0; i <= value; i + 2)
{
if(i == val)
{
return false;
}
}
return true;
}

but when i call it the program crashes. Any idea why? hear is coppy and past friendly pice of code to compile:
#include <iostream>
using namespace std;
bool EvenNo(int value);

int main()
{
int x;
cin>>x;

if(EvenNo(x) == true)
{
cout<< x << " is an even number"<<endl;
}
else
{
cout<< x << " is an odd number"<<endl;
}
system("pause");
return 0;
}
bool EvenNo(int value)
{
int val = value-1;
for (int i = 0; i <= value; i + 2)
{
if(i == val)
{
return false;
}
}
return true;
}


Share on other sites
If the last bit is set, then the number is odd.

bool EvenNo(int value)
{
return (value & 1);
}

Actually, I think your logic would be:

bool EvenNo(int value)
{
return !(value & 1);
}

The reason it's locking up is that 'i + 2' should be 'i += 2'

Share on other sites
I assume, you do not mean crash, but your program hangs. (does not respond anymore)

The reason for that would be your "for" loop, because you do not increment your counter variable. The loop should better read like:

for (int i = 0; i <= value; i += 2)

But, on the other hand, there are much better ways to test for an even/odd number. Something like

cout << "number " << value << " is " << (0 != (value % 2) ? "odd" : "even") << endl;

Share on other sites
Quote:
 Original post by aaroncox_123I'm trying to create a function that will return false if the argument passed to it is false.

You mean if the arguement is even? There are much better ways (look up modulo if you want) but this is probably a good exercise on loops, so whatever.

for (int i = 0; i <= value; i + 2)

Will i ever change? If you go: w = i + 2, does that change i? Think about it.

You've probably got it already. i just stays 0, so it sits there forever checking if i is less than value. It always is. So your program never ends. what you meant was i += 2. Which makes your loop:
for (int i = 0; i <= value; i += 2)

EDIT: NatasDM, that's an evil thing to suggest in a beginners forum.

Share on other sites
Quote:
 Original post by RAZORUNREALEDIT: NatasDM, that's an evil thing to suggest in a beginners forum.

I find it fine - if one don't forget to link an appropriate tutorial about bitwise operators. This one can help, but he assume that you already know something about bit and bytes. Fortunately, wikipedia come to the rescue :) [very basic informations; infos about binary arithmetic].

Regards,

Share on other sites
Sorry, for some reason I thought it would only work on little-endian systems. Which doesn't make a whole lot of sense...

Share on other sites
Quote:
 Original post by rklaffehnI assume, you do not mean crash, but your program hangs. (does not respond anymore)The reason for that would be your "for" loop, because you do not increment your counter variable. The loop should better read like:for (int i = 0; i <= value; i += 2)But, on the other hand, there are much better ways to test for an even/odd number. Something likecout << "number " << value << " is " << (0 != (value % 2) ? "odd" : "even") << endl;

For an even faster solution than modulo 2, you can just bitwise AND (&) the number with 1. It will return 0 if even, 1 if odd.

(value & 1 ? "odd" : "even")

Share on other sites
Quote:
 For an even faster solution than modulo 2, you can just bitwise AND (&) the number with 1. It will return 0 if even, 1 if odd.

That's really just a micro-optimization. It isn't going to speed anything up; the compiler can trivially replace the operations, so it can pick whichever it decides is faster. Use the one that makes more sense.

Share on other sites
Thanks for posting...

I found that changing the for loop didnt make any difference ie: i += 2.

could someone please explanin return(value & 1);

thanks.

Share on other sites
Quote:
 Original post by aaroncox_123Thanks for posting...I found that changing the for loop didnt make any difference ie: i += 2.could someone please explanin return(value & 1);thanks.

You would really be better off understanding what you've done wrong now than moving on indiscriminantly. If you can't write a for loop, then you aren't quite ready to move onto things like bitwise arithmetic.

What is your modified code, and how does it behave? Be specific...if it crashes, how does it crash? When? What value did you provide the function?

CM

Share on other sites
Did you check out the links posted by Emmanuel Deloget?
Basically the lowest bit is anded with 1 (another way of saying is the lowest bit on)if this is true then its an odd number else its even.

But I would agree with Conner McCloud.

Share on other sites
Quote:
 Original post by aaroncox_123Thanks for posting...I found that changing the for loop didnt make any difference ie: i += 2.could someone please explanin return(value & 1);thanks.

Assuming thats all you changed from your original post:
#include <iostream>using namespace std;bool EvenNo(int value);int main(){	int x;	cout<<"Please enter a number -->";	cin>>x;	if(EvenNo(x) == true)	{		cout<< x << " is an even number"<<endl;	}	else	{		cout<< x << " is an odd number"<<endl;	}	system("pause");	return 0;}bool EvenNo(int value){	int val = value-1;	for (int i = 0; i <= value; i += 2)	{		if(i == val)		{			return false;		}	}	return true;}

This works fine for me in MSVS 2005.

Does your program hang (ie become irresposive) or crash? If it crashes, does it display an error message? Have you tried debugging through your program?

Edit:
I, too, agree with Conner McCloud that you should inderstand this code before learning things like bitwise arithmetic, but since you asked:

Computers represent numbers in binary, a base 2 number system (ie, consiting only of 0 and 1). Basically, a binary number is a string of 0's and 1's, where each consecutive digit represents the next highest power of two:
______________________________________| 1 | 2 | 4 | 8 | 16 | 32 | 64 | 128 |  <--powers of 2--------------------------------------| 1 | 0 | 1 | 0 | 1  | 1  | 0  |  0  |  <--a base-2 number--------------------------------------

Above would be the base-10 (decimal) number 53, as can be seen by adding together all the numbers above the '1' digits.

As you can see, since the first digit in binary represents a 1 in base ten, any binary number with that first digit set will be odd.

Now, we come to the bitwise AND (&) operator. A bitwise takes two binary numbers, and if the same bit is set in both, it sets that bit in the resulting number. By ANDing with 1, we basically are asking if the first bit is set in the number being ANDed, which, as explained above, will indicate if the number is odd or even.

Not the best explanation, but hopefully you understand. Check Emmanuel Deloget's links for a clearer explanation.