I have an Array of [3] [3] and by default the numbers will be in following format, which is not visible -
1 2 3
4 5 6
7 8 9
I need to get the numbers from User, example -
0 4 0
9 1 0
5 0 7
if a User fills random numbers like above, then my program needs to fill the '0's with the missing numbers from 1 to 9 in an ascending order as below -
2 4 3
9 1 6
5 8 7
This is my problem and i need a logic for this in C++. Please help me.
The first idea that come to my head is to use std::bitset<9> and when the user is entering some number between 1 and 9 to set corresponding bit. Then there is no any big difficulty to substitute zeroes for corresponding values of indexes of bits in the bitset.
Or when the second array is being entered you can substitute in the original array corresponding values for zeroes. And then you should copy non zero elements of the original array into the target array.
THANKS!!! for the logics you gave vlad from moscow : )
Since i am a beginner, i need some time to understand and study to use bitset.
I'll try to use the 2nd logic which you gave, but my problem is if copy the non zero elements of the original tray into the target tray, i may get the duplicate values.
If you check the above examples the number '1' will get repeated in my result. I need to avoid this duplication. Any help in this.
With your logic, i can able to get the missing numbers from the following code
int a [9]; /***Default Array***/
int b [9]; /***Target Array***/
for (i=0; i < 9; i++) {
if ( b[i] > 0){
j = b[i];
a[j-1] = 0;
}
Then as you said i can able to pass the non-zero elements of default 'a' array in zero-elements of the target 'b' array with the same 'for' and 'if' statements.
But i still can't understand how this logic works...
For example, i can able to get the following output