ARRAYS ???

Hello guys, I have some problems with storing and extracting data from Arrays.

I have this matrix B[][]:
1 2 3 4 5 6
1 2 3 4 0 0
5 6 0 0 0 0
1 2 0 0 0 0
3 4 5 6 0 0
1 3 5 0 0 0
2 4 6 0 0 0
1 2 0 0 0 0
3 4 0 0 0 0
0 0 0 0 0 0
5 6 0 0 0 0
1 3 0 0 0 0
2 4 0 0 0 0
5 0 0 0 0 0
6 0 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
3 5 0 0 0 0
4 6 0 0 0 0
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
4 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
5 0 0 0 0 0
6 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
and this Arrays:
S1[6],S1[6],S1[6],S1[6]:

S1={ 1 2 3 4 5 6 }

S2={ 1 2 5 6 0 0 }

S3={ 1 2 6 0 0 0 }

S4={ 2 3 5 6 0 0 }

What I need to do is to compare the maximum possible length of elements from S1,2,3,4 in B.For example: S2 {1 2 5 6 0 0 } check in B if there is element 1 2 5 6 if not check if 1 2 5 or 1 2 it's yes then write it in SMAX, then continue to check , because remain 5 6 check them if yes then again write it to SMAX. We should find 1 2 in line 4 or 8 and 5 6 in line 3 or 11.
The result should be one answer per line:

SMAX[][]:
1 2 3 4 5 6
1 2 0 0 0 0
5 6 0 0 0 0
1 2 0 0 0 0
6 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
5 6 0 0 0 0
Last edited on

You could use memcmp (memory compare) and check in blocks of 6 bytes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>
using namespace std;

int b [6][6] = {
	1, 2, 3, 4, 5, 6,		// row 0
	1, 2, 3, 4, 0, 0,		// row 1 
	5, 6, 0, 0, 0, 0,		// row 2 etc..
	1, 2, 0, 0, 0, 0,
	3, 4, 5, 6, 0, 0,
	1, 3, 5, 0, 0, 0,
};


int main()
{
	int S1[6] = { 1, 2, 3, 4, 5, 6 };
	//... etc

	if (memcmp(b, S1, sizeof(int) * 6) == 0)
		cout << "We have a match... ";
	else
		cout << "We dont have a match..";
	
	return 0;

}
Last edited on
The description is not clear.

What is written to SMAX? "it" -- A row from B, a matching subsequence, or length of match? In which element? What determines the size of SMAX?

Does the subsequence have to match whole row in B, or is it enough that row in B contains it?

What are the rules to enumerate subsequences from Sx? You give example
{1 2 5 6} => {1 2 5 6}, {1 2 5}, {1 2}, {5 6} but that does not cover all possibilities.
Furthermore, the last three rows in your SMAX have (unexplained) single values.

How results from Sx are combined?
I'm sorry for confusion .
For example if we write all S1,S2,S3,S4 in one matrixe:
S_temp[4][6]
1 2 3 4 5 6
1 2 5 6 0 0
1 2 6 0 0 0
2 3 5 6 0 0
I need to find all possible matching sequences like this:
from S_temp {1 2 3 4 5 6} found in B and it will be row one -> SMAX {1 2 3 4 5 6}
second row from S_temp { 1 2 5 6} found in B, because there are no possible cases we make minus one value { 1 2 5 } look for them in B, again noting, again minus one {1 2} now we found it in row 4 write it in second row of SMAX{ 1 2 3 4 5 6}
{1 2 0 0 0 0} and we stop here looking for { 1 2 } and look for rest of this number { 5 6} and we found it in 3 row again stop and write to SMAX {1 2 3 4 5 6}
{1 2 0 0 0 0}
{5 6 0 0 0 0}, continue with {1 2 6} no such value in B, minus one {1 2 } in row 4 stop write to SMAX {1 2 3 4 5 6}
{1 2 0 0 0 0}
{5 6 0 0 0 0}
{1 2 0 0 0 0} and continue with rest of { 6 } and find it in row 15 and write it to SMAX{1 2 3 4 5 6}
{1 2 0 0 0 0}
{5 6 0 0 0 0}
{1 2 0 0 0 0}
{6 0 0 0 0 0}, then {2 3 5 6 0 0} no such number minus one {2 3 5}, again nothin, minus one {2 3},nothing,minus one { 2 } yes in row 17 write to SMAX {1 2 3 4 5 6}
{1 2 0 0 0 0}
{5 6 0 0 0 0}
{1 2 0 0 0 0}
{6 0 0 0 0 0}
{2 0 0 0 0 0}, now looking for { 3 5 6}, nothing,{3 5}, again nothing, { 3 } yes in row 18 and write to SMAX in ner row, continue with { 5 6 } yes in row 3 write to SMAX and the final result should be:
{1 2 3 4 5 6}
{1 2 0 0 0 0}
{5 6 0 0 0 0}
{1 2 0 0 0 0}
{6 0 0 0 0 0}
{2 0 0 0 0 0}
{3 0 0 0 0 0}
{5 6 0 0 0 0}.
1
2
3
4
5
6
7
std::vector<std::array<int,6>> B = ...;
std::vector<std::array<int,6>> SMAX;
std::array<int,6> S1 = {1, 2, 5, 6, 0, 0};

auto token = S1;
auto match = std::find( std::begin(B), std::end(B), token );
if ( std::end(B) != match ) SMAX.push_back( token );

Could you explain in details please how it works and what libraries do I need?
C++ standard library.

This website has reference documentation for it: http://www.cplusplus.com/reference/
This website has a search function.

The code bits simply show sensible types for your "arrays" and one way to search a row from B that these types make possible. It is up to you to flesh out the rest.
Topic archived. No new replies allowed.