You are here:

# C#/Find rows that matches into a matrix

Question
QUESTION: Hi Murat
I am trying to do in this program the following:

1) Display a matrix data (4x5)
2) Find inside this matrix, equal rows
3) Display the row that match in this way:

1, 4       02  15  18  20  39

where 1, 4 are the numbers of rows that matches

I wrote some comments, that way you can see where are my problems. This is the program:

private void button1_Click(object sender, EventArgs e)
{
bool rowMatch = false;
int numelements = 5;
int index = 5;
bool[] reported = new bool[4];
string row;
string firstRow = "";
string secondRow = "";

int[,] myArray = new int[4, 5] {{ 02, 15, 18, 20, 39 },
{ 07, 11, 14, 15, 34 },
{ 12, 17, 18, 29, 45 },
{ 02, 15, 18, 20, 39 }};
listBox1.Items.Clear();
listBox1.Items.Add("Let's check in the following matrix if there's equal rows ");

// First it is displayed the matrix
for (int i = 0; i < 4; i++)
{
row = i + 1 + " )   ";

for (int j = 0; j < 5; j++)
{
row += myArray[i, j] + "     ";
}
}
// It is compared myArray's three rows
for (int i = 0; i < 4; i++)
{
for (int j = i + 1; j < 5; j++)
{
if (ArrayCompare(ref myArray, 0, 1, ref numelements) == true)
{
rowMatch = true;
}
}
}
if (rowMatch)
{
listBox1.Items.Add("These are the rows that match:");

for (int i = 0; i < 4; i++)
{
if (!reported[i])
{
for (int j = i + 1; j < 5; j++)
{
if (ArrayCompare(ref myArray, 0, 1, ref numelements))
{
if (!reported[i])
{
// I use this string variable to record the number of the first row that match
// but it does not work
firstRow = firstRow + (i + 1) + "    ";
//I use this reported array to record the equal row
reported[i] = true;
}
// I use this string variable to record the number of the second row that match
// but it does not work
secondRow = secondRow + j + " )   ";
// I got an exception here
reported[j] = true;
}
}
if (reported[i])
{
displayRowMatch(ref myArray, i, ref index);
}
}
}
}
else
{
listBox1.Items.Add("There are not equal rows into this matrix");
}
}

// You have to modify your code slightly. Since in C# you cannot split dimensions
// the way you do in C++, change your ArrayCompare method as following:

static bool ArrayCompare(ref int[,] array, int firstDimensionIndex, int secondDimensionIndex, ref int numelements)
{
for (int i = 0; i < numelements; i++)
{
if (array[firstDimensionIndex, i] != array[secondDimensionIndex, i])
return false;
}
return true;
}
// I created this method to display the row that match

void displayRowMatch(ref int[,] array, int firstDimensionIndex, ref int index)
{
string lines = " ";
for (int i = 0; i < index; i++)
{
lines = lines + array[firstDimensionIndex, i] + "     ";
}
}

THANKS

You just made it too complicated. Use following code:
``` for (int i = 0; i < 3; i++)   {       for (int j = i + 1; j < 4; j++)       {                    if (ArrayCompare(ref myArray, i, j, ref numelements) == true)         {             //we write array indexes to line             string lineToDisplay = i + "," + j;                          //write array items to line             for (int k = 0; k < 5; k++)                 lineToDisplay = lineToDisplay + " " + myArray[i,k];                          //display line             listBox1.Items.Add(lineToDisplay);         }          }    } ```

Hope that helps,

Murat

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

QUESTION: Hello Murat,

Thanks for your explanation, you make programming be much easier that it really is,  thanks again. The code you sent me it works and also i can understand it, but if i try to display three rows that match, i get the following:

1, 3       2    15    18    20   39
1, 4       2    15    18    20   39
3, 4       2    15    18    20   39

But, i really i would like to display it this way:

1, 3, 4       2    15    18    20   39

but i have no idea how to obtain it. Could you help please, thanks again

You have to use some memorize arrays to achieve this. Its much easier with List<> of System.Collections but you still have time for that. Try yourself by just using arrays and post me the code so I can help you.

Murat

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

QUESTION: Hi Murat,

The following program, works, but i got the output in just one line, i tried to use the enviroment.NewLine in several places to get this:

1, 3,      2  16  18  20  39
2, 5,      7  11  14  15  34
4, 6, 7,   2  15  18  20  39

but it does not works, could help me, thanks.

private void button1_Click(object sender, EventArgs e)
{
bool rowMatch = false;
int numelements = 5;
bool[] reported = new bool[7];
string row;
string lineToDisplay = "";

int[,] myArray = new int[7, 5] {{ 02, 16, 18, 20, 39 },
{ 07, 11, 14, 15, 34 },
{ 02, 16, 18, 20, 39 },
{ 02, 15, 18, 20, 39 },
{ 07, 11, 14, 15, 34 },
{ 02, 15, 18, 20, 39 },
{ 02, 15, 18, 20, 39 }};
listBox1.Items.Clear();
listBox1.Items.Add("Let's check in the following matrix if there's equal rows ");

// It is displayed the matrix
for (int i = 0; i < 7; i++)
{
row = (i + 1) + " )   ";

for (int j = 0; j < 5; j++)
{
row += myArray[i, j] + "     ";
}
}
// It is compared myArray's three rows
for (int i = 0; i < 6; i++)
{
for (int j = i + 1; j < 7; j++)
{
if (ArrayCompare(ref myArray, i, j, ref numelements) == true)
{
rowMatch = true;
}
}
}
if (rowMatch)
{
listBox1.Items.Add("These are the rows that match:");

for (int i = 0; i < 6; i++)
{
if (!reported[i])
{
for (int j = i + 1; j < 7; j++)
{
if (ArrayCompare(ref myArray, i, j, ref numelements))
{
if (!reported[i])
{
lineToDisplay = lineToDisplay + (i + 1) + ",  ";
reported[i] = true;
}
lineToDisplay = lineToDisplay + (j + 1) + ",  ";
reported[j] = true;
}

}
if (reported[i])
{
for (int k = 0; k < 5; k++)
{
//lineToDisplay = Environment.NewLine;
lineToDisplay = lineToDisplay + " " + myArray[i, k] + "       ";
}
}
}
}
}
}
static bool ArrayCompare(ref int[,] array, int firstDimensionIndex, int secondDimensionIndex, ref int numelements)
{
for (int i = 0; i < numelements; i++)
{
if (array[firstDimensionIndex, i] != array[secondDimensionIndex, i])
return false;
}
return true;
}

Indeed works. You have done a great job, congrats. In C#, to add a new line its as simple as adding "\r\n" to a string.
``` for (int k = 0; k < 5; k++)         {         //lineToDisplay = Environment.NewLine;         lineToDisplay = lineToDisplay + " " + myArray[i, k] + "       ";         } ```
with this
``` for (int k = 0; k < 5; k++)         {         lineToDisplay = lineToDisplay + " " + myArray[i, k];         } lineToDisplay = lineToDisplay + "\r\n"; ```

Hope that helps,

Murat
Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Thanks for all your help

C#

Volunteer

#### Murat Mehmet

##### Expertise

I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.

##### Experience

I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Organizations
Was in R & D for 2 years in a popular Turkish technology website: cyber-warrior.com

Education/Credentials
2011 Computer Engineering graduation in University of Trakia in Turkey.