Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лабораторні, на англійській.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
604.67 Кб
Скачать

1.6 References

A reference introduces an alias for an object. The notation for defining references is similar to that of pointers, except that & is used instead of *. For example,

double num1 = 3.14;

double &num2 = num1; // num is a reference to num1

defines num2 as a reference to num1. After this definition num1 and num2 both refer to the same object, as if they were the same variable. It should be emphasized that a reference does not create a copy of an object, but merely a symbolic alias for it. Hence, after

num1 = 0.16;

both num1 and num2 will denote the value 0.16.

A reference must always be initialized when it is defined: it should be an alias for something. It would be illegal to define a reference and initialize it later.

double &num3; // illegal: reference without an initializer

num3 = num1;

The most common use of references is for function parameters. Reference parameters facilitate the pass-by-reference style of arguments, as opposed to the pass-by-value style which we have used so far. To observe the differences, consider the three swap functions in Listing 5.

Listing 5

void Swap1 (int x, int y) { // pass-by-value (objects)

int temp = x;

x = y;

y = temp;

}

void Swap2 (int *x, int *y) { // pass-by-value (pointers)

int temp = *x;

*x = *y;

*y = temp;

}

void Swap3 (int &x, int &y) { // pass-by-reference

int temp = x;

x = y;

y = temp;

}

Although Swap1 swaps x and y, this has no effect on the arguments passed to the function, because Swap1 receives a copy of the arguments. What happens to the copy does not affect the original.

Swap2 overcomes the problem of Swap1 by using pointer parameters instead. By dereferencing the pointers, Swap2 gets to the original values and swaps them.

Swap3 overcomes the problem of Swap1 by using reference parameters instead. The parameters become aliases for the arguments passed to the function and therefore swap them as intended.

Swap3 has the added advantage that its call syntax is the same as Swap1 and involves no addressing or dereferencing. The following main function illustrates the differences:

int main (void) {

int i = 10, j = 20;

Swap1(i, j); cout << i << ", " << j << '\n';

Swap2(&i, &j); cout << i << ", " << j << '\n';

Swap3(i, j); cout << i << ", " << j << '\n';

}

When run, it will produce the following output:

10, 20

20, 10

10, 20

2. Lab Overview

2.1. Read the theory and try Control Exercises.

2.2. Develop the algorithm flowchart to solve a problem according to individual case from the Table below.

2.3. Write the program code according to the developed algorithm using dynamic arrays, user-defined functions and pointers/references.

2.4. Debug the program, run it and make screenshots.

2.5. Prepare the Lab Report according to the required structure.

#

Array type

Task

2D, float

Calculate the number of items greater than 0

2D, double

Calculate the sum of pair items

2D, int

Calculate the number of negative items

2D, unsigned int

Calculate the sum of diagonal items

2D, float

Substitute 0-items by 1 and output array in reverse order

2D, double

Find an average value of array items

2D, int

Find maximum and minimum values in an array

2D, unsigned int

Calculate an average of diagonal items

2D, float

Divide array items by 10 and output items greater than 0.5

2D, double

Calculate the sum of odd items

2D, int

Calculate the number of items greater than 5

2D, unsigned int

Substitute items greater than 5 by 1, all the rest – by 0

2D, float

Calculate a per-element sum of two arrays of the same type and size

2D, double

Output array with greater sum of elements (use two arrays for comparison)

2D, int

Build a transposed array for a given one

2D, unsigned int

Create 2D array of defined size from an inputted vector

2D, float

Calculate a per-element multiplication of two arrays of the same type and size

2D, double

Output array with greater average value of elements (use two arrays for comparison)

2D, int

Output main diagonal elements which may be divided by 2 without remainder

2D, unsigned int

Calculate a number of elements equal to 1, 2, …, 9 in an array