Reputation:
I'm having trouble with the following assignment, mostly because I don't understand how a Boolean function works. "Write a function called Divisible that takes in two numbers as parameters. Return True if the first number is evenly divisible (no remainder) by the second number. Otherwise return False. Hint: Use %"
Currently what I have is:
int Divisible()
{
int firstNum;
int secondNum;
int result;
cout << "Please enter any integer: ";
cin >> firstNum;
cout << "Please enter another integer: ";
cin >> secondNum;
result == firstNum%secondNum;
}
I'm not sure what to do beyond that. I thought I could assign bool = 0 as true but that doesn't appear to be the case. I'm still very new to C++ so any help would be appreciated.
Upvotes: 6
Views: 166626
Reputation:
When creating functions or using them always remember to start with the signature.
Think what will this function need to work with, and what will it return. You need to return if something is true or false, which are values of data type bool
. Just like in a conditional such an if statement you may use Boolean operators like ==
, !=
and etc.
So you need to return a bool and check if two numbers are divisible. Therefore:
bool Divisible(int a, int b){
// == boolean operator that will return true if a%b evaluates to 0
// false if not
return (a % b) == 0
}
Once you start thinking of functions this way, every program becomes one great puzzle!
Upvotes: 3
Reputation: 43098
In methods that return boolean, you want to first determine what the value of the result will be when the method returns true, and then use the ==
operator to evaluate any result you get against the acceptable result.
So in your case, you are trying to determine whether to return true or false depending on if the first number is evenly divisible by the second.
First thing you do is you take a case that should work, ex:
4, 2
How do you know 4
is divisible by 2
? Well this means that if I divide 4 by 2, then the remainder should be zero. This is what the %
operator returns. If you do 4 % 2
the value is zero.
Ok so now you have the correct result so what you simply do now is to evaluate any result you get against the accepted result like so:
int isDivisible(int a, int b)
{
const int acceptedAnswer = 4 % 2;
if ( a % b == acceptedAnswer )
return 1;
return 0;
}
And there you have it, any value you get that does not equal your accepted answer will return 0
or not equal (!=)
and any other answer will return 1
or equal (==)
Upvotes: 1
Reputation: 1840
The question asks you to write a method that takes the numbers as parameters, not let's you input them from standard input.
Boolean is a type of its own in c++, so you want the method to return bool and not int. An easy to read solution:
bool Divisible(int a, int b) {
int remainder = a % b; // Calculate the remainder of a and b.
if(remainder == 0) {
return true; //If the remainder is 0, the numbers are divisible.
} else {
return false; // Otherwise, they aren't.
}
}
Or more concise:
bool Divisible(int a, int b) {
return (a % b) == 0;
}
Even more concise:
bool Divisible(int a, int b) {
return !(a % b);
}
Upvotes: 14
Reputation: 310980
In the assignment there is clear written that the function must have two parameters. So the function could look the following way (C++)
inline bool Divisible( int n, int m ) { return ( n % m == 0 ); }
or (C)
inline _Bool Divisible( int n, int m ) { return ( n % m == 0 ); }
In the last case you may substitute return type _Bool for int. In the firt case that is when C++ is used it is better to use return type bool. The request to enter two numbers shall be outside the function. The function only allows to determine whether one number is divisible by other number.
Upvotes: 0
Reputation:
bool
is a type that can hold only two values: true
and false
. You use it for expressing truth values, as whether a number divides another or not.
In your case, the function could have been implemented as follows:
bool is_divisible(int a, int b)
{
return a % b == 0;
}
Upvotes: 0
Reputation: 1633
what you have done is correct but in last line you are comparing an uninitialized integer with result (remainder of division).
set int result=0;
and then do this:
return result==firstNum%secondNum; // checks if remainder is zero (true if zero)
you could also simply do return (firstNum%secondNum)==0
Furthermore, your assignment asks for a function that takes in two numbers as parameters (input arguments). So your function prototype should be something like:
bool Divisible(int a, int b);
and you should use a and b in the function instead of taking input from stdin as you are doing right now.
Upvotes: 0
Reputation: 201447
You don't return; or assign a result...
result = firstNum%secondNum;
return (result == 0); // Assuming you want 0 as true.
In general, the value 0
is false and any value that is not (!0
) is true. By convention, that is 1
.
Upvotes: 0