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

3. Report Structure

  • Title page

  • Task overview

  • Algorithm’s flowchart

  • Program code

  • Program running screenshots

  • Conclusions

4. Control Exercises

4.1. Given the following definition of a Swap function

void Swap (int x, int y) {

int temp = x;

x = y;

y = temp;

}

what will be the value of x and y after the following call:

x = 10;

y = 20;

Swap(x, y);

4.2. What will the following program output when executed?

char *str = "global";

void Print (char *str) {

cout << str << '\n';

{

char *str = "local";

cout << str << '\n';

cout << ::str << '\n';

}

cout << str << '\n';

}

int main () {

Print("Parameter");

return 0;

}

5. References

5.1. Juan Soulié. C++ Language Tutorial. – 2007. – p. 41-53.

5.2. Sharam Hekmat. C++ Essentials. – PragSoft Corporation 2005. – p. 45-64.

5.3. Prata S. C++ Primer Plus (5th Edition). – Sams, 2004. – p. 279-336.

Lab #4. Arrays, pointers, references and dynamic variables

Goal: Program problem solving using arrays, pointers and references. Learn how to use pointers to create dynamic variables

1. Theory

An array consists of a set of objects (called its elements), all of which are of the same type and are arranged contiguously in memory. In general, only the array itself has a symbolic name, not its elements. Each element is identified by an index which denotes the position of the element in the array. The number of elements in an array is called its dimension. The dimension of an array is fixed and predetermined; it cannot be changed during program execution. Arrays are suitable for representing composite data which consist of many similar, individual items. Examples include: a list of names, a table of world cities and their current temperatures, or the monthly transactions for a bank account.

A pointer is simply the address of an object in memory. Generally, objects can be accessed in two ways: directly by their symbolic name, or indirectly through a pointer. The act of getting to an object via a pointer to it, is called dereferencing the pointer. Pointer variables are defined to point to objects of a specific type so that when the pointer is dereferenced, a typed object is obtained. Pointers are useful for creating dynamic objects during program execution. Unlike normal (global and local) objects which are allocated storage on the runtime stack, a dynamic object is allocated memory from a different storage area called the heap. Dynamic objects do not obey the normal scope rules. Their scope is explicitly controlled by the programmer.

A reference provides an alternative symbolic name (alias) for an object. Accessing an object through a reference is exactly the same as accessing it through its original name. References offer the power of pointers and the convenience of direct access to objects. They are used to support the call-by-reference style of function parameters, especially when large objects are being passed to functions.

1.1 Arrays

An array variable is defined by specifying its dimension and the type of its elements. For example, an array representing 10 height measurements (each being an integer quantity) may be defined as:

int heights[10];

The individual elements of the array are accessed by indexing the array. The first array element always has the index 0. Therefore, heights[0] and heights[9] denote, respectively, the first and last element of heights. Each of heights elements can be treated as an integer variable. So, for example, to set the third element to 177, we may write:

heights[2] = 177;

Attempting to access a nonexistent array element (e.g., heights[-1] or heights[10]) leads to a serious runtime error (called ‘index out of bounds’ error).

Processing of an array usually involves a loop which goes through the array element by element. Listing 1 illustrates this using a function which takes an array of integers and returns the average of its elements.

Listing 1

const int size = 3;

double Average (int nums[size]) {

double average = 0;

for (register i = 0; i < size; ++i)

average += nums[i];

return average/size;

}

Like other variables, an array may have an initializer. Braces are used to specify a list of comma-separated initial values for array elements. For example,

int nums[3] = {5, 10, 15};

initializes the three elements of nums to 5, 10, and 15, respectively. When the number of values in the initializer is less than the number of elements, the remaining elements are initialized to zero:

int nums[3] = {5, 10}; // nums[2] initializes to 0

When a complete initializer is used, the array dimension becomes redundant, because the number of elements is implicit in the initializer. The first definition of nums can therefore be equivalently written as:

int nums[] = {5, 10, 15}; // no dimension needed

Another situation in which the dimension can be omitted is for an array function parameter, e.g.: double Average (int nums[], int size) {…}.

A C++ string is simply an array of characters. For example,

char str[] = "HELLO";

It is easy to calculate the dimension of an array using the sizeof operator. For example, given an array ar whose element type is Type, the dimension of ar is:

sizeof(ar) / sizeof(Type)