C + + two-dimensional array search problem

Source: Internet
Author: User

Title: In a two-dimensional array, each row is incremented from left to right, and each column is sorted in ascending order from top to bottom, completing a function, entering a two-dimensional array and an integer to determine if the function is in the array.

such as two-dimensional arrays:

1 2 8 9
2 4 9 12
4 7 10 13
6 8 11 15

Find number 7, presence returns true; Find number 5, not present, return false;

Processing method:

from at the top right of the number start, if the number of lookups is less than that number, the lookup number must be located to the left of the number, excluding the column, continue to find the left side of the matrix;

If the number of lookups is greater than this number, the lookup number must be at the lower side of the number, exclude the row, continue to find the remaining matrix on the lower side, and so on, and continue to search from the top right corner of the remaining matrix;

Returns true if the lookup number equals the number;

Returns False if no matrix is left and is still not found.

Code:

BOOLFind (int*matrix,intRowsintColumnsintNumber ) {    BOOLFound =false; introw =0, column = columns-1; if(Matrix! = NULL && rows >0&&columns >0) {         while(Row < rows && column >=0) {            if(Matrix[row*columns+column] = =Number ) {Found=true;  Break; } Else if(Matrix[row*columns+column] >Number ) {                --column; } Else {                ++Row; }        }    }    returnfound;}

Complete test Code:

#include <iostream>#include<stdio.h>using namespacestd;BOOLFind (int*matrix,intRowsintColumnsintNumber ) {    BOOLFound =false; introw =0, column = columns-1; if(Matrix! = NULL && rows >0&&columns >0) {         while(Row < rows && column >=0) {            if(Matrix[row*columns+column] = =Number ) {Found=true;  Break; } Else if(Matrix[row*columns+column] >Number ) {                --column; } Else {                ++Row; }        }    }    if(found) cout<<"The number found in row:"<< row+1<<", column:"<< column+1<<'.'<<Endl; Elsecout<<"The number is not found."<<Endl; cout<<"------------------------------"<<Endl; returnfound;}voidTest (int* Matrix,intRowsintColumnsintNumber ) {cout<<"-------The input matrix is:------"<<Endl;  for(intR =0; r< rows; r++) {         for(intc =0; c < columns; C++) {            if(r==rows) Break; cout<< Matrix[r*columns+c] <<'\ t'; if(c==columns-1) {C= -1; ++R; cout<<Endl; } }} cout<< Endl <<"The number to find:"<< number <<'.'<<Endl; Find (Matrix, rows, columns, number);}intMain () {intmatrix[][4] = {{1,2,8,9}, {2,4,9, A}, {4,7,Ten, -}, {6,8, One, the}}; Test (int*) Matrix,4,4,7); Test (int*) Matrix,4,4,5); Test (int*) Matrix,4,4,1); Test (int*) Matrix,4,4, the); Test (int*) Matrix,4,4,0); Test (int*) Matrix,4,4, -); Test (int*) Matrix,0,0, -); return 0; }

Reference: "Sword refers to the name of the Enterprise interview officer explaining typical programming problem"

C + + two-dimensional array search problem

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.