数组需要注意的一些东西:数组名称为指针,在C/C++中,当数组作为函数的参数传递时,数组就自动退化同类型的指针。
下面是一个二维数组的相关操作,题目为:
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
思想:
首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除这个数字所在的列
/******************二维数组中的查找************************/
/*在一个二维数组中,每一行都按照从左到右递增的顺序排序,*/
/*每一列都按照从上到下递增的顺序排序。请完成一个函数,输*/
/*入这样的一个二维数组和一个整数,判断数组中是否含有该整数*/
#include<stdio.h>
#include<stdlib.h>
bool find(int* matrix,int rows,int columns,int number){
bool find_result = false;
if(matrix!=NULL && rows>0 && columns>0){
int row=0;
int column = columns-1;
while(rows>row && columns>0){
if(number == matrix[row*columns + column]){
find_result = true;
break;
}else if(number<matrix[row*columns +column]){
column--;
}else{
row++;
}
}
}
return find_result;
}
int main(){
int array[2][2]={1,2,3,4};
int *p;
p = &array[0][0];
bool found1;
found1 = find(p,2,2,5);
printf("%d\n",found1);
bool found2;
found2= find(p,2,2,3);
printf("%d\n",found2);
system("pause");
return 0 ;
}