Recently contacted by a book called The Sword refers to the offer, here is prepared in this 2 months or so to finish this book, of course, do not need to how many days a day what, not more, one day to understand a good, hope to become a habit, in addition, ready to sync on GitHub also share.
Today's first question:
Question 3: Finding in a two-bit array
When we need to solve a complex problem, a very effective way is to get from specific problems, through the analysis of specific examples, to obtain the law.
In another two-dimensional array, each row is ordered in ascending order from left to right, and each column is arranged in ascending order from top to bottom. Complete a function, enter a two-dimensional array and an integer to determine if the array contains the integer.
First of all, we must develop good habits, first of all, for a problem, a step-by-step analysis.
Such as:
The subsequent narrowing of the scope through the while statement, and finally can be all compared with NUM, if found, return 1 is good, if not found, return 0 is good.
#include <stdio.h>#include <stdlib.h>intFind_arr (intarr[][3],intRowsintColsintNUM) {if(arr! = null&& rows >0&& cols >0) {introw =0;intCol = cols-1; while(Row < Rows&&col >=0) {if(Arr[row][col] = = num) {return 1; }Else if(Arr[row][col]>num) row--;Elsecol++; } }return 0;}intMain () {intarr[][3] = {1,2,3,2,3,4,5,6,7};intnum =0;printf("Please enter the element you are looking for:"); scanf"%d", &num);intRet=find_arr (arr,3,3, num);if(ret = =0)printf(" %dnot found", num);Else printf("element %dexists", num);system("Pause");return 0;}
Sword Point--two-dimensional array search