You are here:

# C++/Arrays

Question
QUESTION: Sorry Slatko what i am trying to do if to check if the numbers of a row in the matrix formed by selecting the number 2 ( whisheNumber in the code of the program ) in column (0) are inside or outside of this matrix formed by the number 2, where the user is going to choose its size, (numberOfRow2 in the code of the program)

Rows    (0)   (1)   (2)   (3)   (4)          IN          OUT
---------------------------------------------------------------------------
2) |    2    12    13    27    51  |    2  12  51        | 13  27
4) |    2    18    45    47    50
7) |    2    12    14    23    51
8) |    2    10    11    23    54
14) |    2    16    21    43    54
15) |    2    17    18    20    53

In this example the matrix (5x5) is formed by the numbers in rows 4, 7, 8, 14, 15 and columns always are 5. This matrix (5x5) is formed because the user chose numberOfRow2 = 5
This is the matrix (5x5):

Rows    (0)   (1)   (2)   (3)   (4)
------------------------------------
4) |    2    18    45    47    50
7) |    2    12    14    23    51
8) |    2    10    11    23    54
14) |    2    16    21    43    54
15) |    2    17    18    20    53

If you see the row 2) or first array:

2) |    2    12    13    27    51

number 2 is inside the matrix (5x5) position array[4][0]

number 12 is inside the matrix (5x5) position array[7][1]

number 13 is outside the matrix (5x5)

number 27 is outside the matrix (5x5)

number 51 is inside the matrix (5x5) position array[7][4]

Thanks.

I think I understand now. Of all the rows displayed in the second matrix, you want to analyze the first numberOfRow2 rows. Lets call the first numberOfRow2 M. For each row being analyzed in M, you want to see if the elements of that row are IN or OUT of the other rows of M.

The reported[] array shows which rows are present in the second matrix, and you want to analyze the first numberOfRow2 which have been reported.

You need a loop to go through reported[]. Every time you find a reported row, that becomes the control_row. Then you need a nested loop to go through the reported rows again. In the inner loop, every time you find a reported row, that becomes the test_row. Then you must check each element in the control_row to see if it is present in the test_row. Use the isInArray function we had earlier. Of course you want to skip the testing if the test_row is the same as the control row. Each loop, the inner and outer loop, goes through reported[] until numberOfRow2 true elements have been found. So, for each loop you will need a counter to keep track of how many true reported elements have been seen. You will need another array, similar to reported to keep track of which elements in the control_row have been found in other rows of the matrix. Lets call it in_out. For each new control_row, you will need to clear the in_out array.

Here is what I mean in pseudo code.
control_count = 0
declare an in_out array of 5 elements
for control_row = 0; control_row < rows && control_count < numberOfRow2; ++control_row
{
if reported[control_row]
{
++control_count
test_count = 0;
clear the in_out array
for test_row = 0; test_row < rows && test_count < numberOfRow2; ++test_row
{
if reported[test_row]
{
++test_count
if test_row == control_row continue to next test_row

for(col = 0; col < 5; ++col)
{
if array[control_row][col] is in array[test_row] then in_out[col] = true;
}
}
}
use the in_out array to print out which numbers from the control_row are in or out
}
}

Try to implement that in your code between lines 122 and 172 inside if (foundTheNumber).
The key point here which you are missing in your code is that you need to go through all of the array, from 0 to 17, or whatever the end is. The reported rows may be all at the end, say the last 5, so you cannot do at line 128:
for(int i = 0; i <= numberOfRow - 1 - numberOfRow2; i++)
You must go through all of the array, keeping in mind that of the reported rows, you are interested only in the first numberOfRow2.

Really Raul, these puzzles are getting quite complicated.

Good luck.

---------- FOLLOW-UP ----------

QUESTION: Hi Zlatko.
I can not implement your pseudo code, i understood everything but i can not encode it; the only thing I could see clearly was that when the rest of the rows is added sometimes it doesn't. Could you help me please THANKS.

Hi Raul, Here is the code I think you want. It goes between the call to secondPrintHeading(), and the comment // print out the rest of the rows
<font face=monospace>

It produces this output
Rows     (0)   (1)   (2)   (3)   (4)          IN          OUT          IN     OUT
----------------------------------------------------------------------------------------------------
0) |     2    10    11    23    54          2 10 11 23 54 |          5 |   0
2) |     2    12    13    27    51          2 12 51 |          13 27          3 |   2
4) |     2    18    45    47    50          2 |     18 45 47 50          1 |   4
7) |     2    12    14    23    51          2 12 23 51 |          14          4 |   1
8) |     2    10    11    23    54          2 10 11 23 54 |          5 |   0

if(foundTheNumber)
{
int control_count = 0;
bool in_out[columns];
for (int control_row = 0;
control_row < rows && control_count < numberOfRow2;
++control_row)
{
if (reported[control_row])
{
++control_count;
int test_count = 0;
memset(in_out, 0, sizeof(in_out));
for (int test_row = 0;
test_row < rows && test_count < numberOfRow2;
++test_row)
{
if (reported[test_row])
{
++test_count;
if (test_row == control_row) continue;

for(int col = 0; col < columns; ++col)
{
if (isInArray(array[control_row][col], array, test_row, test_row))
{
in_out[col] = true;
}
}
}
}
cout<<setw(5)<< control_row <<") |";
displayRowMatch2(array[control_row], columns);
stringstream ssIn;
stringstream ssOut;
int inCount=0;
int outCount=0;
for(int col = 0; col < columns; ++col)
{
if (in_out[col])
{
ssIn << setw(3) << array[control_row][col];
++inCount;
}
else
{
ssOut << setw(3) << array[control_row][col];
++outCount;
}
}
cout << setw(10) << " "
<< setw(columns*3) << ssIn.str()
<< " | "
<< setw(columns*3) << ssOut.str()
<< setw(10) << " "
<< setw(3) << inCount
<< " | "
<< setw(3) << outCount
<< endl;
}
}
}

</font>

C++

Volunteer

#### Zlatko

##### Expertise

No longer taking questions.

##### Experience

No longer taking questions.

Education/Credentials
No longer taking questions.