C language diary 22 Arrays as function parameters

Example 5-5 Find the average of three numbers. (same as normal variable usage)

Source program:

#include <iostream>
using namespace std;
int main()
{     float fun(float a, float b, float c);//Function declaration     static float a[5] = { 87.5, 90, 100 };     //Define a float type array and initialize     float ave;     ave = fun(a[0], a[1], a[2]);//call fun() function     cout << ave << endl; } float fun(float a, float b, float c)//function definition {     float sum, aver;     sum = a + b + c;     aver = sum / 3.0;     return(aver); }













#include <iostream>
using namespace std;
int main()
{
	float fun(float a, float b, float c);//函数声明
	static float a[5] = { 87.5, 90, 100 };
	//定义一个 float 型数组并初始化
	float ave;
	ave = fun(a[0], a[1], a[2]);//调用 fun()函数
	cout << ave << endl;
}
float fun(float a, float b, float c)//函数定义
{
	float sum, aver;
	sum = a + b + c;
	aver = sum / 3.0;
	return(aver);
}

result:
 

In fact, changing a[5] to a[3] will not affect the result whether you do not write static or write the range so large .

It’s just that we usually write like this for the sake of insurance, and sometimes we will naturally avoid unnecessary troubles

At the same time, the previous "sum = a + b + c; aver = sum / 3.0;" can also be abbreviated as "aver = (a + b + c) / 3.0;" in one step.

Practical examples are as follows:

#include <iostream>
using namespace std;
int main()
{
	float fun(float a, float b, float c);//函数声明
	float a[3] = { 87.5, 90, 100 };
	//定义一个 float 型数组并初始化
	float ave;
	ave = fun(a[0], a[1], a[2]);//调用 fun()函数
	cout << ave << endl;
}
float fun(float a, float b, float c)//函数定义
{
	float  aver;
	aver =( a + b + c) / 3.0;
	return(aver);
}

result: 

 Example 5-6 Find the average of three numbers.

#include <iostream>
using namespace std;
int main()
{     float fun2(float x[3]);//Function declaration     float a[3] = { 87.5,90,100 }; //Define array and initialize*/     float ave ;     ave = fun2(a);//Call the function and use the array name as a parameter*/     cout << ave << endl; }     float fun2(float x[3]) //Function definition, the formal parameter is an array*/






    {
        float sum, aver;
        sum = x[0] + x[1] + x[2];
        aver = sum / 3.0;
        return(aver);
    }

#include <iostream>
using namespace std;
int main()
{
	float fun2(float x[3]);//函数声明
	float a[3] = { 87.5,90,100 }; //定义数组并初始化*/
	float ave;
	ave = fun2(a);//调用函数并将数组名作为参数*/
	cout << ave << endl;
}
	float fun2(float x[3])	//函数定义,形式参数为数组*/

	{
		float sum, aver;
		sum = x[0] + x[1] + x[2];
		aver = sum / 3.0;
		return(aver);
	}

 The transfer process is as follows (for the description process, refer to (follow) the paragraph in front of P87 Introduction Example 5-6):

The result is the same as above:

Book P87:

The first address of the array coincides, and the following elements are stored in the memory in the same order as the same data storage address.

Therefore, the number of elements of the actual parameter array should not be less than that of the formal parameter

oh? This seems doubtful (to be verified):

When the actual parameter set has fewer elements than the formal parameters:

#include <iostream>
using namespace std;
int main()
{
	float fun2(float x[3]);//函数声明
	float a[3] = { 87.5,90 }; //定义数组并初始化*/
	float ave;
	ave = fun2(a);//调用函数并将数组名作为参数*/
	cout << ave << endl;
}
float fun2(float x[3])	//函数定义,形式参数为数组*/

{
	float sum, aver;
	sum = x[0] + x[1] + x[2];
	aver = sum / 3.0;
	return(aver);
}

 If you just delete a number, then the final result is equivalent to treating a[2] as 0 to find the average of three numbers.

In other words, nothing will happen like this, and the program will still not go wrong.

#include <iostream>
using namespace std;
int main()
{
	float fun2(float x[3]);//函数声明
	float a[2] = { 87.5,90 }; //定义数组并初始化*/
	float ave;
	ave = fun2(a);//调用函数并将数组名作为参数*/
	cout << ave << endl;
}
float fun2(float x[3])	//函数定义,形式参数为数组*/

{
	float sum, aver;
	sum = x[0] + x[1] + x[2];
	aver = sum / 3.0;
	return(aver);
}

 But if we change the specific number of elements of the actual parameter to a[2], then the result looks a bit strange:

According to student Yang Huan:

The most probable situation is:

Because the memory space of the actual parameter is only 2 bits at this time

At this time, the system will assign a random value to the third formal parameter , and it is not uncommon for any results to pop up at this time.

And when the actual parameter set has more elements than the formal parameters:

#include <iostream>
using namespace std;
int main()
{
	float fun2(float x[3]);//函数声明
	float a[5] = { 5,6,7,8,9}; //定义数组并初始化*/
	float ave;
	ave = fun2(a);//调用函数并将数组名作为参数*/
	cout << ave << endl;
}
float fun2(float x[3])	//函数定义,形式参数为数组*/

{
	float sum, aver;
	sum = x[0] + x[1] + x[2];
	aver = sum / 3.0;
	return(aver);
}

Result: 6

That is to say, when the number of elements in the actual parameter group is more than that of the formal parameters, the default value of the function calls the first few parameters (here, only the average value of the first three actual parameters is calculated)

If the value of an array element is changed in the sub-function, the corresponding element value of the actual parameter group in the calling function will also be changed accordingly.

The design modification on the above program proves the conclusion as follows:

#include <iostream>
using namespace std;
int main()
{     float fun2(float x[3]);//Function declaration     float a[3] = { 87.5,90,100 }; //Define array and initialize*/     float ave ;     ave = fun2(a);//Call the function and use the array name as a parameter*/     cout << ave << endl;     cout << a[1] << endl; } float fun2(float x[3]) / /Function definition, the formal parameter is an array*/







{
    float sum, aver;
    x[1] = 0;
    sum = x[0] + x[1] + x[2];
    aver = sum / 3.0;
    return(aver);
}

result:

Example 5-7 Use a function to solve the maximum value in Example 5-4, and use an array as a parameter.

Source program:

#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;
}

#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;
}

I write:

Use array elements as function parameters: I have to declare 12 formal parameters in this way, which is too troublesome, so we will temporarily omit it here.

Use array names as function arguments:

Originally written:

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

According to the example 5-6 in the book, follow the cat and draw the tiger, and finally modify the above program to:

#include <iostream>
using namespace std;
int main()
{
	int max(int a[3][4]);
	int a[3][4] = { (11,32,45,67),(22,44,66,88),(15,72,43,37) };
	cout << "最大值为:" << max(a[3][4]) << endl;
	return 0;
}
int max(int a[3][4])
{
	int x,y,max=0;
	for (x = 0; x < 3; x++)
		for (y = 0; y < 4; y++)
			if  (a[x][y] > max) 
				max = a[x][y];
	return max;
}

But the result is still wrong: it should end up being:

#include <iostream>
using namespace std;
int main()
{
    int max(int a[3][4]);
    int a[3][4] = { (11,32,45,67),(22,44,66,88),(15,72,43,37) };
    cout << "最大值为:" << max(a) << endl;
    return 0;
}
int max(int a[3][4])
{
    int x, y, max=0;
    for (x = 0; x < 3; x++)
        for (y = 0; y < 4; y++)
            if (a[x][y] > max)
                max = a[x][y];
    return max;
}

The main problems in the whole process are:

Neither the length nor the data structure of the actual parameter is defined at the time of declaration and definition.

My questions (doubts) during modification:

1. Didn't the book say that it doesn't matter to define the length of the formal parameter? What I have not defined here are all formal parameters, what is wrong?

2. When it comes to the length of the formal parameter, since the length is required, why:

cout << "The maximum value is:" << max(a) << endl;
this does not declare the length, but it is

cout << "The maximum value is: " << max(a[3][4]) << endl;
not possible?

Answers on why:

1.  The length of the formal parameter is indeed not required to be written, but this is not the reason for writing (int a[int x][int y]) in the function definition and declaration:

x and y are used to auto-increment in each cycle to process the next bit

It is not here to break the requirement limit of each row and column of the formal parameter group

Even though the array structure of this formal parameter group can be flexible and changeable, the book says that "the formal parameter group may not define the length"

But the book didn't say "Formal parameter group can not define width and other dimensions" ah

That is to say:

On the one hand, the formal parameter group can not define the length; on the other hand, the formal parameter group cannot forget to define the width and other dimensions

Or:

The formal parameter group does not need to define the length, but must define the width and other dimensions

2. Why only a can be filled in the brackets (inside) when calling the max function but not a[3][4]:

If you fill in a[3][4] in brackets

It is very likely (high probability) that a[3][4] identified by the system is the array element a[3][4] rather than the entire array of a[3][4]

Array element a[3][4]: the specific array element in the third row and fourth column in a[3][4]

What's more, even if a represents the whole array, for the system, it is just a string of numbers

What the system wants is an int variable or constant, not a string of numbers

And if you fill in a in brackets:

a satisfies "is an int constant", and is the first memory address of the first element of the array

There is no grammatical error, and the address can also be transmitted, which is naturally the correct choice

P88:

You can change the header of the max_value function to the following situations to observe the compilation situation:

Draw the following table according to the actual situation:

int max_value(int array[][])  0
int max_value(int array[3][]) 0
int max_value(int array[3][4]) 1
int max_value(int array[10][10]) 0
int max_value(int array[12]) 0

1: Successful operation; 0: Error reporting;

The above conclusion undoubtedly proves once again that:

The formal parameter group does not need to define the length, but must define the width and other dimensions

Additional extensions:

Why

int max_value(int array[10][4]) 1
int max_value(int array[3][10]) 0

?

The reasons are as follows:

For details, please refer to:

Questions about two-dimensional arrays as function parameters - shujuliu818's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/Zz_zzzzzzz__/article/details/127250180#comments_25884677