Two-dimensional arrays are passed as parameters

Source: Internet
Author: User

The thinking harvest of the two-dimensional array problem

This algorithm has nothing to do with data structure, it is mainly logical thinking to use the relationship between the data, more examples of concrete attempts to concrete complex problems, analysis of that situation, time complexity is low. Think first, then write the code. Code close to your own thinking.

L code harvesting for two-dimensional array processing

When you treat a two-dimensional array as an argument in C + +, you must indicate all dimension sizes or omit the first dimension. However, when we write the program often encounter each dimension is not fixed, we can not think of it as a two-dimensional array, but instead of it as a normal pointer, in addition to two parameters to indicate the number of dimensions, and then we are two-dimensional array manually addressed, so that the two-dimensional array as a parameter to pass the purpose

#include <iostream>using namespacestd;//The difference between int * Matrix and int matrix[][] when passing parameters;//sometimes recursion makes simple problems more complicated;//Sometimes we write code to define variables to be close to thinking, but also pay attention to the code speed; intFind (int* Matrix,intRowsintColumnsintNumber ) {    introw =0, column = Columns-1;  while(column>=0&& row<rows) {        if(* (matrix+row*columns+column) = =Number ) {cout<<* (Matrix+row*columns+column) <<"Find"<<Endl; return 1; }        if(* (Matrix+row*columns+column) < number)//In addition to having a return, be sure to note the If else match{cout<<* (Matrix+row*columns+column) <<"it's small."<<Endl; Row++; }Else{cout<<* (Matrix+row*columns+column) <<"big."<<Endl; Column--; }                }    return 0; }intMain () {intdata[4][4]=    {        1,2,8,9,        2,4,9, A,        4,7,Ten, -,        6,8, One, the    }; intnum[4][5]    {        1,2,8,9,Ten,        2,4,9, A, -,        4,7,Ten, -, -,        6,8, One, the, -    }; cout<<find (& (data[0][0]),4,4,0) <<Endl; cout<<find (& (num[0][0]),4,5, -) <<Endl;}

Two-dimensional arrays are passed as parameters

Related Article

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.