Sword Point--two-dimensional array search

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.