# Tasks solving. Functions and arrays. Function prototype. Passing an array to a function

The topic provides tasks solutions that demonstrate:

• using a function prototype;
• ways to pass an array to a function.

### Contents

Search other websites:

#### 1. Using a function prototype. An example

##### 1.1. Task. Calculating the length of a line segment between points

The coordinates of a point on the coordinate plane x1, y1, x2, y2 are given. Develop a Length() function that calculates the length of the line segment formed by these points.

### ⇑

##### 1.2. Solution without using a function prototype

Case 1: The Length() function is implemented before the main() function. In this case, the function prototype does not need to be specified.

```#include <iostream>
using namespace std;

// Case 1. Declaring the Length() function before calling it from the main() function
// Function Length() gets 4 parameters: x1, y1, x2, y2
double Length(double x1, double y1, double x2, double y2)
{
// 1. Declare local variable len
double len;

// 2. Calculate distance by Pythagorean theorem
// sqrt() - the function of taking the square root of the number of
len = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

// 3. Return the result from function Length()
return len;
}

void main()
{
// 1. Declare variables
double x1, y1, x2, y2;
double len; // result

// 2. Input variable values from the keyboard
cout << "Enter data: " << endl;
cout << "x1 = "; cin >> x1;
cout << "y1 = "; cin >> y1;
cout << "x2 = "; cin >> x2;
cout << "y2 = "; cin >> y2;

// 3. Invoke function Length() from function main()
// 4 arguments are passed to the Length() function: x1, y1, x2, y2
len = Length(x1, y1, x2, y2);

// 4. Display the result
cout << "length = " << len << endl;
}```

The result of the program

```Enter data:
x1 = 2.5
y1 = 3.8
x2 = 8.7
y2 = -1.4
length = 8.09197```

### ⇑

##### 1.3. Solution using function prototype

Case 2: the implementation of the Length() function is placed after the main() function. The abbreviated text of the program is as follows. The compiler throws an error

```#include <iostream>
using namespace std;

// Case 2. The Length() function is declared and implemented after the main() function
// As a result, the compiler generates an error: Length identifier not found
// Here it is necessary to declare the prototype of the Length() function

void main()
{
...
...
...
}

// Function Length() gets 4 parameters: x1, y1, x2, y2
double Length(double x1, double y1, double x2, double y2)
{
...
}```

To prevent the compiler from throwing an error, you need to declare the Length() function prototype before the main() function. After declaring the prototype, the abbreviated C++ code will look like this:

```#include <iostream>
using namespace std;

// Case 2. Implementing the Length() function after the main() function
// Here it is necessary to declare the prototype of the Length() function

// This is the prototype of the Length() function - information for the compiler
// that the Length() function is implemented somewhere in the program.
// After this declaration, the compiler knows for sure that after
// the main() function there is somewhere a function named Length()
// and the corresponding parameters
double Length(double, double, double, double);

void main()
{
...
}

// Function Length() gets 4 parameters: x1, y1, x2, y2
double Length(double x1, double y1, double x2, double y2)
{
...
}```

### ⇑

#### 2. Passing a one-dimensional array to a function. Example

##### 2.1. The task

Demonstrate passing a one-dimensional array to a function. Implement a function that takes a one-dimensional array of integers and returns the maximum item of the array.

##### 2.2. Solution

```#include <iostream>
using namespace std;

// Passing a one dimensional array to a function

// A function declaration that receives an array of integers.
// The function calculates the maximum element in the array.
// Funcion parameters:
// - count - number of items in the array;
// - A - parameter, which is an array
int MaxItem(int count, int A[])
{
// Calculating the maximum element in an array

// 1. Check whether the value of count correctly
if (count <= 0)
return 0;

// 2. Declaration internal variables max, i
int max, i;

max = A[0]; // first max is equal to the first item of the array

// loop for calculating the max value
for (i = 0; i < count; i++)
if (max < A[i]) max = A[i];

// 3. Return the result
return max;
}

void main()
{
// Passing array to a function
// 1. Declare internal variables
const int MAX_ITEMS = 10; // maximum possible number of elements in the array
int M[MAX_ITEMS]; // array
int n; // the current number of items in the array (1 <= n <= MAX_ITEMS)

// 2. Input the number of elements in the array
cout << "Enter data:" << ::endl;
cout << "n = ";
cin >> n;

// 3. Checking the correctness of the entered value n
if ((n < 1) || (n > MAX_ITEMS))
{
cout << "Incorrect input." << endl;
return;
}

// 4. Input of array
cout << "Enter array:" << endl;
for (int i = 0; i < n; i++)
{
cout << "M[" << i << "] = ";
cin >> M[i];
}

// 5. Invoke the function MaxItem()
// An array M is passed to the MaxItem() function
int max = MaxItem(n, M); // in variable max - result

// 6. Displaying the result on the screen
cout << endl << "max = " << max << endl;
}```

The result of the program

```Enter data:
n = 5
Enter array:
M[0] = 4
M[1] = -5
M[2] = 8
M[3] = 7
M[4] = 1

max = 8```

### ⇑

#### 3. Passing a two-dimensional array to a function

##### 3.1. The task

A two-dimensional array M of size m×n is given. Develop a function that calculates the sum of the elements of an array M. The array M is passed to the function.

##### 3.2. Solution
```#include <iostream>
using namespace std;

// Two-dimensional array A of size m*n is specified.
// Develop a function that calculates the sum of the array elements

// Declare array dimension - outside of main() function
const int MAX_M = 10;
const int MAX_N = 10;

// The implementation of function
// Two-dimensional array A[][] is passed using the MAX_N constant
double SumArray(int m, int n, double A[][MAX_N])
{
// 1. Declare internal variable
double sum = 0; // the result sum

// 2. Sum calculation loop
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
sum += A[i][j];

// 3. Return from the function
return sum;
}

void main()
{
// 1. Declare variables
double A[MAX_M][MAX_N]; // Two-dimensional array
int m, n; // array dimension
int i, j; // additional variables
double summ; // the result of the Sum() function

// 2. Input the size of array
cout << "Enter data:" << endl;
cout << "m = "; cin >> m;
cout << "n = "; cin >> n;

// 3. Checking the correctness of the values m, n
if ((n < 1) || (n > MAX_N) || (m < 1) || (m > MAX_M))
{
cout << "Error. Incorrect input." << endl;
return;
}

// 4. Input the array A
cout << "Enter array A:\n";
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
{
cout << "A[" << i << "][" << j << "] = ";
cin >> A[i][j];
}

// 5. Input the array A for checking
cout << "\nEntered array A:\n";

for (i = 0; i < m; i++)
{
// display one line of the array
for (j = 0; j < n; j++)
{
cout << A[i][j] << "\t";
}
cout << endl;
}

// 6. Invoke the function SumArray()
summ = SumArray(m, n, A);

// 7. Display the result on the screen
cout << endl << "Result: summ = " << summ << endl;
}```

The result of the program

```Enter data:
m = 3
n = 4
Enter array A:
A[0][0] = 0.5
A[0][1] = 2.1
A[0][2] = 1.8
A[0][3] = 3.7
A[1][0] = 2.2
A[1][1] = 0.8
A[1][2] = 0.9
A[1][3] = 1.7
A[2][0] = 3.6
A[2][1] = 4.1
A[2][2] = 1.1
A[2][3] = 3.2

Entered array A:
0.5 2.1 1.8 3.7
2.2 0.8 0.9 1.7
3.6 4.1 1.1 3.2

Result: summ = 25.7```