Click here to Skip to main content
15,897,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
char arraystart[16][4] ={
{'0','0','0','0'},
{'0','0','0','1'},
{'0','0','1','0'},
{'0','0','1','1'},
{'0','1','0','0'},
{'0','1','0','1'},
{'0','1','1','0'},
{'0','1','1','1'},
{'1','0','0','0'},
{'1','0','0','1'},
{'1','0','1','0'},
{'1','0','1','1'},
{'1','1','0','0'},
{'1','1','0','1'},
{'1','1','1','0'},
{'1','1','1','1'},
};
I Need help understanding how to compare 2 rows of this array. I need to pick 2 rows that have three of the same 4 numbers in common such as row 1 and row 2 and make it output{'0','0','0','-'} or like in rows 15 and 16 and make it output{'1','1','1','-'}. Another one would be row 6 and row 14 and make that output{'-','1','0','1'}. I am trying to create a function to do this but i am having trouble. Could someone please help. Also if you have a better way to do this without using 2d arrays let me know.

What I have tried:

Ive tried for loops with i and j but it doesnt seem to be working because i cant et a constant way to check if 3 are right.
Posted
Updated 26-Apr-16 22:28pm
Comments
Member 12487065 26-Apr-16 19:09pm    
If you have a question about what i am trying to accomplish please comment and i will be happy to clarify.
Garth J Lancaster 26-Apr-16 19:42pm    
you would get a 'better response' if you updated the 'what have I tried' section of your original question - ie show the code - you may be close, or far away - from what I see its solvable with 2d arrays btw

I would have written
1) a routine to generate indexes for all possible match comparisons
2) a separate 'row comparer' function that takes two indexes or 'rows' and returns (maybe) the count of numbers/matching elements by position or even a bool true to say 3 matches were found
3) a routine to generate the output row from the indexes/rows identified from (2)

learn how to break a problem down into bite-size chunks, 'maybe' the chunks are functions - if you can solve it for one case, then you can solve it for many

Sergey Alexandrovich Kryukov 26-Apr-16 21:28pm    
Good suggestions. It would make a fair formal answer.
I've done some part of it, provided the general idea and my favorite reference on this topic in Solution 1.
—SA
Mohibur Rashid 26-Apr-16 19:27pm    
I don't see the try.
Sergey Alexandrovich Kryukov 26-Apr-16 21:16pm    
You call it "2D array"? :-) Hm... At best, it could be consider as a metaphor...
—SA

The building block could be a function comparing two character sequences, returning true if three matches happen (as well returning the output character sequence):

C++
// making the code robust is left as an exercise... :-)
bool same3( const char a[], const char b[], char o[4])
{
  int matches = 0;
  for ( int n=0; n<4; ++n)
  {
    if ( a[n] == b[n] )
    {
      o[n] = a[n];
      matches++;
    }
    else
    {
      o[n] = '-';
    }
  }
  return (matches == 3);
}
 
Share this answer
 
Comments
Mohibur Rashid 27-Apr-16 4:35am    
I would do different trick. I would add all the numbers :)
sum+=o[n]-'0'
if sum >= 3 then replace the 0
if sum <= 1 then replace the 1
You did not define what is "comparison". Anyway, whatever it is, the problem if fairly simple.

But first of all, you should better get rid of "2D array" delusion — please see my comment to the question. In all cases, there is nothing but continuous range of memory, in your case, of the length 16 * 4. All your "rows" and "columns" is nothing but semantic interpretation of the locations in this memory. If you realize that, you will be able to write your function (too bad you did not write what you have tried, only wrote something in the "What I have tried" section, something which means nothing).

Please see how C/C++ arrays work and what "multidimensional" really means: Arrays — C++ Tutorials[^].

This is all you really need. Please learn it and understand; using this knowledge, you should be able to write the function as required.

—SA
 
Share this answer
 
v2
Typedef is your friend :-)
// Define an array of 4 characters
typedef char FourCharArray[4];

// Now build the compare function
void CompareFourArrayRow (FourCharArray* myArray,   // Full sample array  
                          int firstRow2Cmp,         // First row to compare
                          int secondRow2Cmp,        // Second row to compare
                          FourCharArray* cmpResult) // Pointer to result array
{
	// first element compare
	if ( myArray[firstRow2Cmp][0] == myArray[secondRow2Cmp][0]) {
		(*cmpResult)[0] = myArray[firstRow2Cmp][0];
	} else (*cmpResult)[0] = '-';
	// second element compare
	if (myArray[firstRow2Cmp][1] == myArray[secondRow2Cmp][1]) {
		(*cmpResult)[1] = myArray[firstRow2Cmp][1];
	} else (*cmpResult)[1] = '-';
	// third element compare
	if (myArray[firstRow2Cmp][2] == myArray[secondRow2Cmp][2]) {
		(*cmpResult)[2] = myArray[firstRow2Cmp][2];
	} else (*cmpResult)[2] = '-';
	// forth element compare
	if (myArray[firstRow2Cmp][3] == myArray[secondRow2Cmp][3]) {
		(*cmpResult)[3] = myArray[firstRow2Cmp][3];
	} else (*cmpResult)[3] = '-';
}

// ... Ok we want to use the function in our code

// Define a result array  ... you could use char cmpRes[4];  but for tighter use
FourCharArray cmpRes;
// Now call the function .. in example here row 0 & 1
// Result will be put into cmpRes
CompareFourArrayRow((FourCharArray*)arraystart, 0, 1, &cmpRes);


Really char arraystart[16][4] should actually be defined as a FourCharArray arraystart[16] and that gives away what the typedef does.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900