Boolean Function

Pages: 12
A mathematical set is a collection of non repeating elements. Write a function to test whether an array of arbitrary size containing only digits from 0-9 is a set. The function should return true when the array is not a set. Use the prototype bool ins(int set [], const int length);
{1,2,3} returns false
{1,2,3} returns true

Not sure how to start this because I am really not good with boolean functions but I know I must initialize the prototype after the int main. Given digits from 0-9 I know it must be an array of 10 elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
using namespace std;

bool ins(int set [], const int length);

int main(){


return 0;
}

bool ins(int set [], const int length){
     const int length = 10;
     int set[length] = {0,1,2,3,4,5,6,7,8,9,};
     
    
    }
   
remove lines 14 and 15. They are not needed as you are passed those values in your function already.

Instead move tham into main() and pass them into your function.

I am really not good with boolean functions
You do not how to return value from function? There is no difference between functions returning integers and functions returning booleans


Given digits from 0-9 I know it must be an array of 10 elements.
No it can be smaller or larger. it can be size of 3 containing say 0, 5, 6; or size 90 containing all zeroes.


Not sure how to start
First of all decide how would you decide if given array is a set? Harsh limit on containing values is a hint.
Boolean functions have to return a true or false as I was told but we have not been given enough examples for it to be clear.

For the array I would then make the user set the amount of elements they want in the array?

Boolean functions have to return a true or false as I was told but we have not been given enough examples for it to be clear.
1
2
3
return true;
//or
return false;


For the array I would then make the user set the amount of elements they want in the array?
No. Amount of elements is passed into function alongside array.
The question is confusing to me since it states that it wants to return true or false so the same things?

Very confused about this question overall. I am not sure exactly what it is asking.
There is probably an error.
From your assigment:
{1, 1, 2} → false
{1, 3, 2} → true

Basically: if all array elements are in range 0-9 and unique, you should return true. Else you return false.
So if any number repeats I should return false ?

#include <iostream>
#include <cmath>
using namespace std;

bool ins(int set [], const int length);

int main(){

const int length = 10;

return 0;
}

bool ins(int set [], const int length){

}

I am not even sure how to being to write this program.
I know in the bool function i will need two statements
if(no repetition){
return true;
}
else{(repetition)
return false;
}
You can start by counting how many times some number appeared in the array.
bool ins( const int set[], const int length)
{
Int check = 0;
for( int i = 0 ; i < size ; ++i )
if( set[i] ==check )
return true ;
}else{
return false ;
}
there you are checking if there is number 0 in the array. You are not counting number of occurences in array.

Basically you should do: for each array element increase number of occurences of said element by one.
After that check number of occurences. If any one is larger than one, it is not a set.

I will write first part for you. Think about second yourself.
1
2
3
4
5
6
7
8
9
10
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
}
So the second part would be including this in the int main?

Oh wait i have to return true now



else{
for(int i=0;i<length;i++){
if(set[i] >=0 && set[i]<=9)
return true;
++digitCount[set[i]];
}
No you still need to do that:
After that check number of occurences. If any one is larger than one, it is not a set.
Not sure what you mean..?
Function I wrote is incomplete you need to finish it.
I calculated how many times each digit is encountered in set and stored results in digitCount array.
Now you need to iterate over it and check if any digit is encountered more than once.
okay so now i check for repetition?

for(int i = 0;i < 20; i++) {
for(int j = i; j < 20; j++)
{
if(j != i)
{
if(set[i] ==digitCount[j])
{
digitCount++;
}
}
Did you check you code? Like compiling it and giving it a try?

I repeat again: I done with the set. You do not need to touch it. You just need to iterate over digitCount and check if any value is larger than 1;

Example
1
2
{1, 0 ,1, 1, 1, 0, 0, 2, 0, 1}
//                    ↑This is larger than obne therefore it is not a set 
Last edited on
But in the question isn't it okay for it to be larger than one?
No. as your assigment states: A mathematical set is a collection of non repeating elements
If some element encountered more than once it is by definition repeating and therefore it is not a set.
So the little portion of the code you have written just checks for the numbers to be between 0-9 and now I have to figure out how to check if they are repeating?
Pages: 12