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

3. Report Structure

  • Title page

  • Task overview

  • Algorithm’s flowchart

  • Program code

  • Program running screenshots

  • Conclusions

4. Control Exercises

4.1. Assuming that n is 20, what will the following code fragment output when executed?

if (n >= 0)

if (n < 10)

cout << "n is small\n";

else

cout << "n is negative\n";

4.2. Assuming that n is 1, what will the following code fragment output when executed?

switch(n) {

case 0:

cout << “Zero”;

case 1:

cout << “One”;

case 2:

cout << “Two”;

}

4.3. Assuming that n is 2, what will the following code fragment output when executed?

switch(n) {

case 0:

cout << “Zero”;

break;

case 1:

cout << “One”;

break;

case 2:

cout << “Two”;

break;

default:

cout << “Other”;

}

4.4. Assuming that n is 3, what will the following code fragment output when executed?

switch(n) {

case 0:

cout << “Zero”;

break;

case 1:

cout << “One”;

break;

case 2:

cout << “Two”;

break;

default:

cout << “Other”;

}

4.5. What will the following code fragment output when executed?

for (i=5; i>=-5; i--) {

if (i == 0) continue;

cout << i;

}

4.6. What will the following code fragment output when executed?

n = 3;

while (n=5)

cout << n;

4.7. How many ‘*’ will appear on the screen when executed?

i=1;

do {

cout << “*”;

i++;

} while (i<5);

5. References

5.1. Juan Soulié. C++ Language Tutorial. – 2007. – p. 34-40.

5.2. Sharam Hekmat. C++ Essentials. – PragSoft Corporation 2005. – p. 30-44.

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

Lab #3. C++ USER-DEFINED FUNCTIONS

Goal: program numerical integration with different integration methods using C++ user-defined functions

1. Theory

A function provides a convenient way of packaging a computational recipe, so that it can be used as often as required. A function definition consists of two parts: interface and body. The interface of a function (also called its prototype) specifies how it may be used. It consists of three entities:

  • The function name. This is simply a unique identifier.

  • The function parameters (also called its signature). This is a set of zero or more typed identifiers used for passing values to and from the function.

  • The function return type. This specifies the type of value the function returns. A function which returns nothing should have the return type void.

The body of a function contains the computational steps (statements) that comprise the function. Using a function involves ‘calling’ it. A function call consists of the function name followed by the call operator brackets ‘()’, inside which zero or more comma-separated arguments appear. The number of arguments should match the number of function parameters. Each argument is an expression whose type should match the type of the corresponding parameter in the function interface.

When a function call is executed, the arguments are first evaluated and their resulting values are assigned to the corresponding parameters. The function body is then executed. Finally, the function return value (if any) is passed to the caller.

Since a call to a function whose return type is non- void yields a return value, the call is an expression and may be used in other expressions. By contrast, a call to a function whose return type is void is a statement.