I'm trying to eliminate some number from range (170-2500) and then calculate the remaining numbers after the elimination. The numbers composed of 3 digits from number's list (2,5,6,7). I have tried using Cartesian Product
to generate the numbers, but I confused how to eliminate the numbers from the range. The cartesian product code is obtained from geeksforgeeks. I know how to calculate the remaining numbers but I confused with the numbers that will be eliminated.
void findCart(int arr1[], int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
printf("{%d%d%d}, ", arr1[i], arr1[j], arr1[k]);
}
int main()
{
min=170;
max=2500;
int arr1[] = {2,5,6,7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
findCart(arr1, n1, number);
int count=0;
if (number>=min && number<=max){
count++;
}
int total=max-min+1;
int result=total-count;
cout<<result;
return 0;
}
Not sure that I understand your question, but from your code, i think you are trying to do this: create an integer number from list of digits then check if it is from min to max, then print number of remaining in range. try this code, hope you understand my idea:
// return total number which are meet conditions
int findCart(int arr1[], int n, int min, int max)
{
int totalNumbers = 0;
int number = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++){
number = arr1[i]*100 + arr1[j]*10 + arr1[k];
if ( number >= min && number <= max )
++totalNumbers;
}
return totalNumbers;
}
int main()
{
int min=170;
int max=2500;
int arr1[] = {2,5,6,7};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int totalNumberFound = findCart(arr1, n1, min, max);
int total=max-min+1;
int result=total- totalNumberFound;
cout<<result;
return 0;
}