The method of the function parameters of the array masterpieces in C + + programming _c language

Source: Internet
Author: User

The concept of C + + arrays
In a nutshell, an array is a collection of ordered data. To find an element in an array, you must give two elements, namely the array name and subscript. The array name and subscript uniquely identify an element in an array.

Array is of type attribute. Each element in the same array must belong to the same data type. An array occupies a contiguous storage unit in memory. If you have an integer array A, assuming that the starting address of the array is 2000, the array is stored in memory as shown in the figure.


The introduction of an array does not need to define a large number of variables in the program, greatly reducing the number of variables in the program, so that the program refining, and the meaning of the array is clear, easy to use, clearly reflects the relationship between the data. Many good algorithms are related to arrays. Skilled use of arrays, can greatly improve the efficiency of programming and solving problems, enhance the readability of the program.

C + + uses square brackets to denote subscript, such as s[1],s[2],s[3] representing S1,S2,S3 respectively.

C + + with array masterpieces function parameters
Constants and variables can be used as function arguments, and the same array elements can also be function arguments, with the same usage as variables. An array name can also be an argument and a formal parameter, passing the starting address of an array.
Using array elements as function arguments

Because an argument can be an expression, and an array element can be part of an expression, an array element can, of course, act as an argument to a function, passing the value of an array element to a parameter variable as an argument to a variable.

"Example" is handled by a function.

This is a function max_value, which is used to compare and return results. The program can be written as follows:

#include <iostream>
using namespace std;
int main ()
{
  int max_value (int x,int max);//function declaration
  int I,j,row=0,colum=0,max
  int a[3][4]={{5,12,23,56} , {19,28,37,46},{-12,-34,6,8}}; Array initialization
  max=a[0][0];
  for (i=0;i<=2;i++) for
   (j=0;j<=3;j++)
   {
     max=max_value (A[i][j],max);//Call max_value function
     if ( MAX==A[I][J]//If the function returns the value of A[i][j] {row=i//Note that the element
      row number I
      colum=j//write down the element column number J
     }
   }
  cout << "max= <<max<<", row= "<<row<<", colum= "<<colum<<endl;}"
int max_value (int x,int max)//define Max_value function
{if
  (X>max) return x;//if X>max, the function returns the value x
  else returned max;//if X≤max, function return value is Max
}

function parameters with array masterpieces

You can use the array name function arguments, at which point the actual participation parameters are in array names (or you can use pointer variables, see chap. 6th).

"Example" uses the selection method to sort 10 integers in the array from small to large.

The so-called selection method is the first 10 number of the smallest number and a[0] swap, and then a[1] to a[9] the smallest number and a[1] swap ... For each comparison round, find the smallest of an unsorted number. A total of 9 rounds were compared. According to this idea the program is written as follows:

#include <iostream>
using namespace std;
int main ()
{
  void select_sort (int array[],int n);//function declaration
  int a[10],i;
  cout<< "Enter the ORIGINL array:" <<endl;
  for (i=0;i<10;i++)//input 10 number
   cin>>a[i];
  cout<<endl;
  Select_sort (a,10);//function call, array masterpiece argument
  cout<< "the sorted array:" <<endl;
  for (i=0;i<10;i++)//Output 10 ordered number of
   cout<<a[i]<< "";
  cout<<endl;
  return 0;
}
void Select_sort (int array[],int N)//parameter array is the array name
{
  int i,j,k,t;
  for (i=0;i<n-1;i++)
  {
   k=i;
   for (j=i+1;j<n;j++)
     if (array[j]<array[k]) k=j;
   T=array[k];array[k]=array[i];array[i]=t
  }
}

The operating conditions are as follows:

Enter the ORIGINL array:
6 9-2 56 87 11-54 3 0 77↙//Enter 10 number the
sorted array:-54-2 0 3 6 9 11-56 77 87

There are two points to note about using array masterpieces function parameters:
1 if the function argument is an array name, the formal parameter should also be a group name (or pointer variable), and the formal parameter cannot be declared as a normal variable (such as int array;). The real parameter group and the parameter group type should be consistent (now all int), if inconsistent, the result will be an error.

2 What needs to be specifically stated is that the array name represents the address of the first element of the array and does not represent all the elements in the array. Therefore, when using an array masterpiece function argument, instead of passing the value of the real parameter group to the formal parameter, the address of the first element of the real argument group is passed to the formal parameter.

A formal parameter can be an array name or a pointer variable, which is used to receive an address from an argument. If the parameter is an array name, it represents the address of the first element of the shape parameter group. When the function is called, the address of the first element of the real parameter group is passed to the parameter group name. In this way, the real parameter group and the parameter Group share the same section of the deposit. See figure.


When using a variable as a function parameter, the value of the argument variable can only be passed to the parameter variable, and if the value of the parameter is changed in the call function, the argument has no effect, that is, the value of the argument is not changed by the value of the parameter. In the case of an array masterpiece function argument, changing the value of the parameter group element will change the value of the real parameter group element at the same time. It is often consciously used in programming to change the value of real parameter group elements.

In fact, declaring a parameter group does not mean actually creating an array that contains several elements, nor allocating a storage unit to it when calling a function, but in the form of array[] that the array is a one-dimensional array name to receive the address from the argument. Therefore array[] The values in parentheses are not practical, and the compilation system does not handle the contents of the square brackets of the one-dimensional array. The number of elements can be written or not written in the declaration of a one-dimensional array of parameters.

The following types of letters in the first part of a function are valid and have the same effect:

  void Select_sort (int array[10],int N)//specifying the number of elements is the same as the real argument group
  void select_sort (int array[],int N)//unspecified element number
  void Select _sort (int array[5],int N)//Specify the number of elements is different from the real argument group

C + + actually handles only the shape parameter group name as a pointer variable to receive the address from the actual argument. Some of the phenomena mentioned above are the result of this.
function parameters with multidimensional array masterpieces

If you use a two-dimensional array masterpiece as an argument and a formal parameter, you must specify the size of the second dimension (that is, the column) when declaring the parameter group, and should be the same size as the second dimension of the argument. The size of the first dimension may or may not be specified. Such as:

  int array[3][10]; The two dimensions of a parameter group are specified


Or

  int array[][10]; First dimensional size omitted


Both are legal and equivalent. But you can't omit the size of the second dimension. The following form parameter group is not valid:

  int array[][]; Cannot determine how many column elements int array[3][] per row of an array
  ;//cannot determine the structure of an array without specifying the number of columns


With the same size as the second dimension, the first dimension of the form parameter group can be different from the real parameter group. For example, the actual parameter group is defined as:
int score[5][10];
The form parameter group can be declared as:

  int array[3][10]; The number of columns is the same as the real parameter group, and the number of rows is different
  int array[8][10];


In this case, the two-dimensional array of parameters and the two dimensional array of arguments are all composed of the same type and the size of one-dimensional array, the real parameter group name score represents the starting address of its first element (that is, the first row), the system does not check the size of the first dimension.

If you are an array of three or more dimensions, the processing method is similar.

"Example" has a 3x4 matrix to find the maximum value in all elements of the matrix. The request is handled by a function.

The procedure for solving this problem is as follows:

#include <iostream>
using namespace std;
int main ()
{
  int max_value (int array[][4]);
  int a[3][4]={{11,32,45,67},{22,44,66,88},{15,72,43,37}};
  cout<< "Max value is" <<max_value (a) <<endl;
  return 0;
}
int max_value (int array[][4])
{
  int i,j,max;
  MAX=ARRAY[0][0];
  for (i=0;i<3;i++) for
   (j=0;j<4;j++)
     if (Array[i][j]>max) max=array[i][j];
  return max;
}

The results of the operation are as follows:

Max value is 88

The reader can change the header of the Max_value function to the following situations and observe the compilation:

int max_value (int array[][])
int max_value (int array[3][])
int max_value (int array[3][4])
int Max_ Value (int array[10][10])
int max_value (int array[12])

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.