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>
usingnamespace std;
bool ins(int set [], constint length);
int main(){
return 0;
}
bool ins(int set [], constint length){
constint length = 10;
int set[length] = {0,1,2,3,4,5,6,7,8,9,};
}
#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;
}
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( constint set[], constint length)
{
int digitCount[10] = { 0 };
for(int i = 0; i < length; ++i) {
if(set[i] < 0 || set[i] > 9)
returnfalse; //Encountered something other than 0..9
++digitCount[ set[i] ];
}
//Rest
}
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.
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?