While loop with boolean function

Using a while loop that terminates by a boolean function

Page 1 of 1

7 Replies - 21297 Views - Last Post: 08 March 2009 - 05:13 PM Rate Topic: -----

#1 Jud202  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 02-March 09

While loop with boolean function

Posted 08 March 2009 - 01:02 AM

OK. This is probably a really simple problem for a lot of people out there.
I just can't seem to get it to work.

I'm trying to make a while loop where the user would input 3 integers,
and if any of the integers are the same, then the loop repeats the body again.
To terminate the loop, a Boolean function will take the integers as arguments, and
will not end until it is true, where it would then go to step 2, which is simply ordering the integers, highest to lowest.
I have that step worked out fine.

I just can't seem to get the boolean to work the way asked.
For that matter, I'm not really sure how to put it into a while loop without getting errors

	bool is_different(int x, int y, int z)
{ 
	if (x != y && y != z)
	{
		return true;
	}
	else
	{
		return false;
	}


That is my boolean function. Not even sure if that's right or not...
Anyways. Any help would be greatly appreciative.
Even just a step in the right direction. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: While loop with boolean function

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,340
  • Joined: 20-August 07

Re: While loop with boolean function

Posted 08 March 2009 - 01:45 AM

Your function seems fine; Where's the rest of the code which is giving you errors? and what are the errors you're seeing? In this case it would help to see your complete program
Was This Post Helpful? 0
  • +
  • -

#3 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: While loop with boolean function

Posted 08 March 2009 - 01:58 AM

I disagree that the fuction is OK.

First, it's missing the ending bracket.

Second, the logic seems to be the opposite of the conditions.

He said, only if the numbers are the same, should the loop exit, however, his function will cause the loop to repeat if x is not equal to y AND y is not equal to z, but he forgot the condition if x is not equal to y, i.e., the loop will continue if all three numbers are different which he said should mean that it exits.

This is more like it:

#include <iostream>

bool is_different(int, int, int);



int main()
{
	using namespace std;	
	bool sentry = true;
	
	int x, y, z;
	
	while ( sentry)
	{
	
	cout << "Enter three numbers: " << endl;
	cin >> x >> y >> z;
	
	sentry = is_different(x, y, z);
	
	
	}
	
	cout << "\nHave a nice day";
	


 return 0;
}



 bool is_different(int x, int y, int z)
{ 
	if (x == y || y == z)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}



However, if he means what he says, then it should be:

#include <iostream>

bool is_different(int, int, int);



int main()
{
	using namespace std;	
	bool sentry = true;
	
	int x, y, z;
	
	while ( sentry)
	{
	
	cout << "Enter three numbers: " << endl;
	cin >> x >> y >> z;
	
	sentry = is_different(x, y, z);
	
	
	}
	
	cout << "\nHave a nice day";
	


 return 0;
}



 bool is_different(int x, int y, int z)
{ 
	if (x == y || y == z || z == x)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}

This post has been edited by kidicarus: 08 March 2009 - 02:04 AM

Was This Post Helpful? 1
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,340
  • Joined: 20-August 07

Re: While loop with boolean function

Posted 08 March 2009 - 04:10 AM

View Postkidicarus, on 8 Mar, 2009 - 08:58 AM, said:

I disagree that the fuction is OK.

First, it's missing the ending bracket.
That looked like it was probably a copy/paste issue; hence why I suggested that the OP submit the complete code along with errors (I don't see the point in guessing what the errors might be)

View Postkidicarus, on 8 Mar, 2009 - 08:58 AM, said:

Second, the logic seems to be the opposite of the conditions.
Unless the function is very poorly named, then I disagree on that point; The OP hasn't indicated in any way whatsoever how the function is going to be used. a function named "is different" would suggest that it returns true when all 3 of its parameters are different. having a function named as such when it actually does the opposite is very confusing to read. -

View Postkidicarus, on 8 Mar, 2009 - 08:58 AM, said:

bool is_different(int x, int y, int z)
{
    if (x == y || y == z)
    {
        return true; 
While its behaviour might fit with what your example does, names are important for human readers - and that's just completely counter-intuitive. you'd have been better off renaming to something along the lines of is_equal.

This post has been edited by Bench: 08 March 2009 - 04:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: While loop with boolean function

Posted 08 March 2009 - 04:11 AM

I agree with Bench.
Was This Post Helpful? 0
  • +
  • -

#6 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: While loop with boolean function

Posted 08 March 2009 - 04:44 AM

Quote

Unless the function is very poorly named, then I disagree on that point; The OP hasn't indicated in any way whatsoever how the function is going to be used.


I think he made it pretty clear:

"and if any of the integers are the same, then the loop repeats the body again. To terminate the loop, a Boolean function will take the integers as arguments, and
will not end until it is true"

If he means what he says, to terminate the while loop the function he has written should return true only if all three numbers are different.

His code would not do this even if his function was properly formatted.

Further clarification of this point is provided by the fact that he wants to organize the numbers from highest to lowest - so they should all be different.

Quote

While its behaviour might fit with what your example does, names are important for human readers - and that's just completely counter-intuitive. you'd have been better off renaming to something along the lines of is_equal.


I wrote it that way so as the OP could still have a reasonable share of work left to do.

However, this (above) is just an opinion. You could think of it as a function that is checking whether the _values themselves_ are different or the same - that is the objective, regardless of what it returns, and that is what my function does.

This post has been edited by kidicarus: 08 March 2009 - 04:45 AM

Was This Post Helpful? 0
  • +
  • -

#7 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: While loop with boolean function

Posted 08 March 2009 - 04:57 AM

View Postbsaunders, on 8 Mar, 2009 - 03:11 AM, said:

I agree with Bench.


I had no idea you two were smarter than my compiler.

Here is his original function:



bool is_different(int x, int y, int z)
{
if (x != y && y != z)
{
return true;
}
else
{
return false;
}

If you put in 2, 2, 1, it repeats the loop, just as the original post had indicated.

However, if somebody decided to be cute and put in 2, 1, 2, the loop exits, preventing him from organizing from greatest to smallest without repeating digits.

But again, probably there is an error in compiliation as it should "work correctly." Could you tell me what that is?

While you're at it (no pun intended) could you explain how the conditions are not clear when the purpose of the program is to organize three different numbers from greatest to smallest?

This is the way I was able to write the program that meets all of his conditions, requiring a rewrite of the function:


 bool is_different(int x, int y, int z)
{ 
	if (x != y && y != z && x != z)
	{
		return true;
	}
	else
	{
		return false;
	}
	
}	




Better yet, rewrite the code with the original goal using that function: "if any of the integers are the same, then the loop repeats the body again.
"

That way, I will KNOW that it is my computer that is the problem, and not your logic. Otherwise, your post was not helpful.
Was This Post Helpful? 0
  • +
  • -

#8 Jud202  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 02-March 09

Re: While loop with boolean function

Posted 08 March 2009 - 05:13 PM

Sorry about the confusing writing.

What is suppose to happen, is I ask for 3 numbers.
The user puts in 3 numbers.
If those numbers are NOT ALL DIFFERENT then I will not move on, and continue to ask for 3 numbers.

kidicarus's 1st program he wrote in his post with 2 codes works for what I need.

I dont remember the exact errors I was getting, but they were mostly to do with not being defined, or can not be used this way.
And every time I had the while statement, it would say expected ' '. Here is the full working code.

#include <iostream>  

using namespace std;

bool different = true; // initialization 
bool is_different(int x, int y, int z); // declaration of our functions
void print_result ();
int greater_of_two(int x, int y);
int max_of_three(int x, int y, int z);
int smaller_of_two(int x, int y);
int min_of_three(int x, int y, int z);

int x, y, z; //variables

int main()
{
	while ( different )
	{
		cout << "Enter three numbers: " << endl;
		cin >> x >> y >> z;
		different = is_different(x, y, z);
	}
	print_result();
	return 0;
}

int greater_of_two(int x, int y)
{
	if (x > y)
	{
		return x;
	}
	else
	{
		return y;
	}
}

int max_of_three(int x, int y, int z)
{
	int max = greater_of_two(greater_of_two(x, y), z);
	return max;
}

int smaller_of_two(int x, int y)
{
	if (x < y)
	{
		return x;
	}
	else
	{
		return y;
	}
}

int min_of_three(int x, int y, int z)
{
	int min = smaller_of_two(smaller_of_two(x, y), z);
	return min;
}

void print_result ()
{
	int result = min_of_three(x, y, z);
	int result2 = max_of_three(x, y, z);
	cout << "The Minimum is " << result << endl;
	cout << "The Maximum is " << result2 << endl;
}

 bool is_different(int x, int y, int z)
{
	if (x == y || y == z)
	{
		return true;
	}
	else
	{
		return false;
	}
   
}



I'm a noob when it comes to programming. Only been doing it for about a month now. So any tips would be greatly appreciated.
Thanks for your help on this on. The Loop makes more sense now
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1