Home>

C++ array concept

In a nutshell:An array is a collection of ordered data.To find an element in an array, two elements must be given,That is, the array name and index.The array name and index uniquely identify an element in an array.

Arrays have type attributes.Every element in the same array must be of the same data type.An array occupies a continuous storage unit in memory.If there is an integer array a, assuming that the starting address of the array is 2000, the storage condition of the array in memory is as shown in the figure.

The introduction of arrays eliminates the need to define a large number of variables in the program.Greatly reduce the number of variables in the program,Refine the program,And the meaning of the array is clear,Easy to use,Clearly reflects the connection between the data.Many good algorithms are related to arrays.Use arrays skillfully,Can greatly improve the efficiency of programming and problem solving,Improved program readability.

C++ uses square brackets to indicate subscripts,For example, s [1], s [2], and s [3] respectively represent s1, s2, and s3.

c++ use array name as function parameter

Constants and variables can be used as function arguments,Similarly, array elements can also be used as function arguments.Its usage is the same as for variables.Array names can also be used as arguments and parameters.The starting address of the array is passed.

Using array elements as function arguments

Since the argument can be an expression,Array elements can be part of an expression,So array elements can of course be used as arguments to functions,As with variables as arguments,Pass the value of an array element to a formal parameter variable.

[Example] Use function processing.

Let's set a function max_value to compare and return the result.Programs 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 the element row number i
      colum=j;//Note the element column number j
     }
   }
  cout<<"max="<<max<" ;, row ="<&row;<", column =<<colum<endl;
}
int max_value (int x, int max) //define the max_value function
{
  if (x>max) return x;//If x>max, the function returns x
  else return max;//If x≤max, the function return value is max
}

Use array name as function parameter

You can use array names as function parameters,At this time, the actual parameter parameters use the array name (you can also use pointer variables,(See Chapter 6).

[Example] Use the selection method to sort 10 integers in the array from small to large.

The so-called selection method is to first exchange the smallest number of 10 numbers with a [0], and then exchange the smallest number of a [1] to a [9] with a [1] ... every round of comparisonFind the smallest of an unsorted number.Compare 9 rounds. Write a program based on this idea:

#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 ++) //Enter 10 numbers
   cin>>a [i];
  cout<endl;
  select_sort (a, 10);//function call,Array name as argument
  cout<"the sorted array:"<endl;
  for (i=0;i<10;i ++) //Output 10 sorted numbers
   cout<a [i]<<""
  cout<endl;
  return 0;
}
void select_sort (int array [], int n) //The parameter array is the name of the array
{
  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 operation is as follows:

enter the originl array:
6 9 -2 56 87 11 -54 3 0 77↙ //Enter 10 numbers
the sorted array:
-54 -2 0 3 6 9 11 56 77 87

There are two points about using array names as function parameters:

1) If the function argument is an array name,The parameter should also be an array name (or pointer variable). The parameter cannot be declared as an ordinary variable (such as int array;). The type of the real parameter group and the parameter group should be the same (both are now int). If they are not the same,The result will be wrong.

2) It should be noted that:the array name represents the address of the first element of the array,It does not represent all elements in the array.So when using array names as function arguments,Instead of passing the value of the real parameter group to the formal parameter,Instead, the address of the first element of the real parameter group is passed to the formal parameter.

The parameter can be an array name,Can also be a pointer variable,They are used to receive the address passed as an argument.If the parameter is an array name,It represents the address of the first element of the parameter group.When calling a function,Pass the address of the first element of the real parameter group to the formal parameter group name.In this way, the real parameter group and the formal parameter group occupy the same memory unit.See picture.

When using variables as function parameters,You can only pass the value of an argument variable to a formal variable,If the value of a formal parameter is changed during the function call,Has no effect on the actual parameters,That is, the value of the actual parameter is not changed by the value of the formal parameter.When using array names as function arguments,Changing the value of a formal parameter group element will also change the value of a real parameter group element.This feature is often used consciously in program design to change the values ​​of real parameter group elements.

In fact, declaring a parameter group does not mean actually creating an array of several elements,It does not allocate storage units when calling the function,Just use array [] to indicate that array is a one-dimensional array name.To receive the address passed in the actual parameters.Therefore, the values ​​in square brackets in array [] have no practical effect.The compilation system does not process the contents in square brackets of the one-dimensional array.The number of elements can be written in the declaration of the one-dimensional array of formal parameters.It is not necessary to write.

The following expressions of the function header are legal,Same effect:

 void select_sort (int array [10], int n) //The number of specified elements is the same as the real parameter group
  void select_sort (int array [], int n) //do not specify the number of elements
  void select_sort (int array [5], int n) //The number of specified elements is different from the real parameter group

C++ actually only treats the parameter group name as a pointer variable,Used to receive the address passed from the actual parameters.Some of the aforementioned phenomena are caused by this.

Multi-dimensional array name as function parameter

If you use two-dimensional array names as arguments and parameters,When declaring a parameter group,The size of the second dimension (that is, the column) must be specified and should be the same as the size of the second dimension of the argument.The size of the first dimension can be specified,It may not be specified.Such as:

 int array [3] [10];//both dimensions of the parameter group are specified

or

 int array [] [10];//The first dimension is omitted

Both are legal and equivalent.But the size of the second dimension cannot be omitted.The following formal parameter group is illegal:

 int array [] [];//Can't determine how many column elements are in each row of the array
  int array [3] [];//The structure of the array cannot be determined without specifying the number of columns

Under the premise that the second dimension is the same size,The first dimension of the formal parameter group may be different from the real parameter group.For example, a real parameter group is defined as:

int score [5] [10];

The formal parameter group can be declared as:

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

At this time, the two-dimensional array of formal parameters and the two-dimensional array of actual parameters are both composed of one-dimensional arrays of the same type and size.The actual parameter group score represents the starting address of its first element (that is, the first line),The system does not check the size of the first dimension.

If it is a three- or more-dimensional array,The treatment is similar.

[Example] There is a 3 × 4 matrix. Find the maximum value of all the elements in the matrix.Requires function processing.

The procedure to solve 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 are as follows:

max value is 88

The reader can change the header of the max_value function to the following situations,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])
c
  • Previous js view the execution time of a function instance code
  • Next JavaScript study notes (3): JavaScript also has an entry Main function