-1

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; 
} 
jowwyss79
  • 33
  • 6
  • What is `number`? – AlterV May 24 '19 at 00:22
  • @AlterV number with 3 digits that will be eliminated, generate from cartesian product of the number list (2,5,6,7), like 222,255,256,257,...777 – jowwyss79 May 24 '19 at 00:27
  • I'm sorry, I confused with that part so I don't know where I should write the integer. – jowwyss79 May 24 '19 at 00:30
  • 1
    Be very cautious with `#include ` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and `using namespace std;` ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Together they turn the global namespace into a minefield where common terms like `reverse`, `find`, `copy`, `count`, and `lexicographical_compare_3way` are already defined and just itching to blow the out of your program. – user4581301 May 24 '19 at 00:34
  • By the way, if your compiler is up to date you can use `std::size(arr1)` instead of `sizeof(arr1) / sizeof(arr1[0])`. – user4581301 May 24 '19 at 00:37
  • @user4581301 You're right. Thank you for remind me. – jowwyss79 May 24 '19 at 00:41
  • You use a variable `number` but it is not declared or defined in your code. You declare a function `findCart` with two parameters, but later try to call one with three. [Edit] your question to include a [mcve]. – 1201ProgramAlarm May 24 '19 at 04:39

1 Answers1

1

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; 
} 
the boy
  • 235
  • 1
  • 6