-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayFind.c
44 lines (39 loc) · 904 Bytes
/
ArrayFind.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<stdlib.h>
/*Search a num in a A[m][n] Array
* Each line of the array is increase from left to right
* Each colum of the array is increase from top to down
* */
#define N 5
void search(int **A, int num) {
int i=0, j=0;
int startRow = 0,startCol = 4;
int nodeVal = A[startRow][startCol];
if(!A) {
printf("Error Msg");
return;
}
for(i=startRow,j=startCol; j>=0 && i < N;) {
nodeVal = A[i][j];
if(num < nodeVal) {
j--;
} else if(num == nodeVal) {
printf("%d %d", i,j);
return;
} else { //num > nodeVal
i++;
}
}
}
int main(void) {
int A[N][N] = {{1,2,4,7,10},
{2,3,5,8,11},
{4,5,8,9,12},
{6,9,10,11,13},
{9,10,11,12,14}};
int *pA[5] = {A[0], A[1], A[2], A[3], A[4]};
int num;
scanf("%d", &num);
search(pA,num);
return 0;
}