Boolean Function

Pages: 12
little portion of the code you have written just checks for the numbers to be between 0-9
It check that all elements are in range 0..9 and counst how often they are encountered. YOu just have to LOOK in digitsCount array and see if any digit count is larger than 1. That is all. I don't know how to make it clearer.
and once the digitcount array has a number larger than one we would just return false?
bool ins( const int set[], const int length)
{
int digitCount[10] = { 0 };
for(int i = 0; i < length; ++i) {
if(set[i] < 0 || set[i] > 9)
return false; //Encountered something other than 0..9
++digitCount[ set[i] ];
}
//Rest
}



You've written the above code. What i dont understand is that we are just returning a false. What happens with the return true?
I have come across this code when searching to figure out the answer. Would it be similar to this at all?


bool Foo( unsigned const int &number){

int temp = number;
int digitTable[10]={0};

while(temp > 0)
{
digitTable[temp % 10]++; // Last digit's respective index.
temp /= 10; // Move to next digit
}

for (int i=0; i < 10; i++)
{
if (digitTable [i] > 1)
{
return false;
}
}
return true;
}
What i dont understand is that we are just returning a false
It is a early return. Like when you asked to check if everything is fine at home and you see a burning building, you are not going to check everything: it is obvious that it is WRONG.

Actual return part is what you need to write.

Would it be similar to this at all?
Yes. Second part is prety much is what you need to write.
This part

for (int i=0; i < 10; i++)
{
if (digitTable [i] > 1)
{
return false;
}
}
return true;
}
So to complete the code we would have:


bool ins( const int set[], const int length){
int digitCount[10] = { 0 };
for(int i = 0; i < length; ++i) {
if(set[i] < 0 || set[i] > 9)
return false; //Encountered something other than 0..9
++digitCount[ set[i] ];
}
//Rest
}
for (int i=0; i < 10; i++)
{
if (digitTable [i] > 1)
{
return false;
}
}
return true;
}



A little confused if this is correct can you please explain what it is doing
Yes. But notice that you have digitTable array by different name. So you need to chage that in your snippet.
Yes I have noticed but can you please breakdown whats going on. As stated before your portion of the code checks to see if the numbers are between 0-9. Then this second part checking to see if any number is greater than 1?
So calling this function in my int main would i declare a constant int length of 10?
then just call the function directly?
DTrey wrote:
As stated before your portion of the code checks to see if the numbers are between 0-9.
MiiNiPaa wrote:
It check that all elements are in range 0..9 and counst how often they are encountered
MiiNiPaa wrote:
I calculated how many times each digit is encountered in set and stored results in digitCount array.
MiiNiPaa wrote:
Basically you should do: for each array element increase number of occurences of said element by one.
[...]
I did first part for you
It is not like I told what that code does several times.

DTrey wrote:
Then this second part checking to see if any number is greater than 1?
MiiNiPaa wrote:
YOu just have to LOOK in digitsCount array and see if any digit count is larger than 1.
MiiNiPaa wrote:
Now you need to iterate over it and check if any digit is encountered more than once.
It checks if number of each digit occurences (which are conviniently stored in digitCount) in no larger than one.
Thank you for the breakdown really appreciate it. So calling this function in my int main would I have to set a constant int length and call the function by its name. This is what I have trouble with. I did an earlier program containing a type double and when testing. I just had to call the function by its name directly and it was that simple but for bool functions I believe its a bit different.
It is exactly like that. No difference between functions returning bool and, say, integer.

How you call your function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    const int array_size = 5;
    int arr1[array_size] = {1, 4, 7, 2, 0};
    int arr1[array_size] = {2, 2, 5, 3, 9};
    bool is_set1 = ins(arr1, array_size);
    bool is_set2 = ins(arr2, array_size);
    if(is_set1)
        std::cout << "array 1 is a set\n";
    else
        std::cout << "array 1 is not a set\n";
    if(is_set2)
        std::cout << "array 2 is a set\n";
    else
        std::cout << "array 2 is not a set\n";
}
Okay thank let me test it out and see if I have any problems.
Worked Great Thanks A Ton. Really appreciate the help.
Topic archived. No new replies allowed.
Pages: 12