Hello I have received this question from my lecturer, however, he is unwilling to teach me how to solve this question. This quiz is long over, however, I just wish to know how to solve it using C++, hope someone could help me along.

Question:

Given a non-negative integer n, we wish to list down those missing digits in n and
display them in increasing order.
You are suppose to provide k sets of integers for testing where k must be at least 5
and maximum 10. Data validation is required for k.
The following shows some possible interactions and displays:
_______________________________________________
Enter number of sets for testing: 55
Enter number of sets for testing: 8
Enter number of sets for testing:
Enter number of sets for testing:

Enter integer 1: 87240
Missing digits: 1 3 5 6 9

Enter integer 2: 0
Missing digits: 1 2 3 4 5 6 7 8 9

etc..

---------------

Thank you

What I wrote is only checking once. You can always just add for / while loop, and reset the array for multiple checks. There is probably a better way to write this program, and I am also intrigued how anyone else would write this. You mentioned that this was for a quiz, so I think my solution wouldn't be the way you would write it down.

 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; int main() { const int size = 10; string numbers[size] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; string myNumbers[size]; string numberString = ""; cout << "Enter number for testing: "; cin >> numberString; // Place number inside an array for(int i = 0; i < numberString.length(); i++){ myNumbers[i] = numberString.substr(i,1); } // Compare the numbers with your numbers for(int i = 0; i < size; i++){ for(int j = 0; j < numberString.length(); j++){ // Sets equivalent numbers as "-1" if(numbers[i] == myNumbers[j]){ numbers[i] = "-1"; } } } // Prints out all numbers that aren't -1 for(int i = 0; i < size; i++){ if(numbers[i] != "-1"){ cout << numbers[i] << " "; } } }``````
Last edited on
Thank you very much for helping! I really appreciate it! :)
 I am also intrigued how anyone else would write this

Lets see what you do:
 ```FOR EACH digit in [0..9] IF input CONTAINS digit THEN disable digit FOR EACH digit in [0..9] IF digit IS enabled THEN PRINT digit```

Do note that if input if 555, then the
IF 555 CONTAINS 5 (which you have on lines 19--24)
will on first iteration change the digit into -1
and on the other two iterations you will compare "-1" with "5".
There is no need to do those two iterations; we already know that input contains at least one 5.
There is statement `break` that helps us to cut the loop short.

There is a shorter path too:
 ```FOR EACH digit in [0..9] IF input DOES NOT CONTAIN digit THEN PRINT digit```

Implementation-wise do note that a std::string has an array of characters and a digit is a single character. You treat digits as strings (probably to accomodate the "-1"). You use arrays of strings.
Less is more.

std::string has member function `find()`. You can use it for the CONTAINS test.
Library has `std::find()`, if you want to operate with arrays (or containers).
closed account (48T7M4Gy)
 ``123456789101112131415161718192021222324252627282930`` ``````#include int main() { int digit[10]{0}; int number{0}; std::cout << "Please enter an integer: "; std::cin >> number; if(number != 0) { while(number > 0) { digit[number % 10]++; number /= 10; } } else digit[0] = 1; for(int i = 0; i < 10; ++i) { if(digit[i] == 0) std::cout << i << ' '; } std::cout << '\n'; return 0; }``````
So simple and effective, kemort! I worked out a solution based on std::strings, but it looks so stupid compared with yours...
Thank you Keskiverto, Kemort and also Enoizat! :)
 ``12345678910111213`` ``````#include #include using namespace std; int main() { bool used[10] = { 0 }; string input; cout << "Enter a positive integer: "; cin >> input; for ( char c : input ) used[c-'0'] = true; for ( int i = 0; i < 10; i++ ) if ( !used[i] ) cout << i << " "; }``````

 ```Enter a positive integer: 87240 1 3 5 6 9```
Topic archived. No new replies allowed.