Passing the missing numbers to an Array of 3 x 3

Hi,

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.

- Latchu
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.
Last edited on
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.
No, you are mistaken.

Consider the process step by step. So you have

1 2 3
4 5 6
7 8 9



Using casting you can consider it as a one dimensional array

1 2 3 4 5 6 7 8 9

Let name this array as 'a'.

Now let assume that you are entering elements of the second array.

You have entered 0. You do nothing with the original array.
You have enterd 4. So you do

a[4 - 1] = 0;

You have entered 0. Let skip it. When you have entered 9. So you do

a[9 - 1] = 0;

and so on. You will get array 'a' looking as

0 2 3 0 0 6 0 8 0

So you need to copy non-zero elements of this array in zero-elements of the target array.
Last edited on
Vlad your logic works great!

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

Values entered > 3 5 0 0 8 6 0 1 7
Output > 0 2 0 4 0 0 0 0 9

I am eager to know how the values are assigned & what actually the program does : )

Topic archived. No new replies allowed.