题目:在一个二维数组中,行从左到右递增,列从上到下递增。请完成一个函数进行查找



#define _CRT_SECURE_NO_WARNINGS 1


#include<iostream>
using namespace std;
bool find(int *array, int rows, int columns, int goal)
{
bool found = false;
if (array != NULL&&rows > 0 && columns > 0)
{
int row = 0;
int col = columns - 1;
while (row < rows&&col >= 0)
{
if (array[row*columns + col] == goal)
{
found = true;
break;
}
else if (array[row*columns + col]>goal)
--col;
else
++row;
}
}
return found;
}

int main()
{
int array[][4] = { { 1, 2, 8, 9 }, { 2, 2, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
int goal = 7;
cout << find((int*)array, 4, 4, 7) << endl;
cout << find((int*)array, 4, 4, 20) << endl;
cout << find((int*)array, 4, 4, 0) << endl;

system("pause");
return 0;
}