For a function of the form.
bool contains_equal_rows(int height, int width, const int ar[height][width])
How do I find if a row is repeated? I can see how you can set a bool to true if a row is repeated, but how do you avoid returning false too early?
Problem description:
Write a function that gets a 2-d array of given dimensions of ints.
It returns true if the array contains two rows with exactly the same values in the same order, and returns false otherwise.bool contains_equal_rows( int height, int width, const int ar[height][width])Examples:
On input
{{1,2,3,4}, {2,3,4,1}, {1,2,3,4}}it returns true.
On input
{{1,2,3,4}, {2,3,4,5}, {3,4,5,6}}it returns false.
On input
{{1,1,1,1}, {2,2,2,2}, {1,1,1,6}}it returns false.
My attempt so far:
bool contains_equal_rows(int height, int width, const int array[height][width])
{
int w = width;
int h = height;
bool test = false;
bool matchedRow = false;
for(int m = 0; m < h; m++)
{
for(int n = 0; n < w; n++)
{
if( array[m][n] == array[m][n-1] )
{
test = true;
}
else
{
test = false;
}
}
if(test == true)
{
matchedRow = true;
}
}
return matchedRow;
}
The problem with my code is that I'm only comparing a row with the one before it, and can't figure out how to compare one row with all.
Expected results as shown in description.