- •Vasyl Yatskiv,
- •Content
- •Introduction
- •1. Theory
- •1.1. Arithmetic Operators
- •1.2. Relational Operators
- •1.3. Logical Operators
- •1.4. Increment/Decrement Operators
- •1.5. Assignment Operator
- •1.6. Conditional Operator
- •1.7. Comma Operator
- •1.8. The sizeof Operator
- •1.9. Operator Precedence
- •1.10. Simple Type Conversion
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #2. Control structures
- •1. Theory
- •1.1. Simple and Compound Statements
- •1.2. The if Statement
- •1.3. The switch Statement
- •1.4. The while Statement
- •1.5. The do Statement
- •1.6. The for Statement
- •1.7. The continue Statement
- •1.8. The break Statement
- •1.9. The goto Statement
- •1.10. The return Statement
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •1. Theory
- •1.1. A Simple Function
- •1.2. Parameters and Arguments
- •1.3. Global and Local Scope
- •1.4. Scope Operator
- •1.5. Symbolic Constants
- •1.6. Inline Functions
- •1.7. Recursion
- •1.8. Default Arguments
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #4. Arrays, pointers, references and dynamic variables
- •1. Theory
- •1.1 Arrays
- •1.2 Multidimensional Arrays
- •1.3 Pointers
- •1.4 Dynamic Memory
- •1.5 Pointer Arithmetic
- •1.6 References
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #5. Structures
- •1. Theory
- •1.1 Introducing Structures
- •1.2 Using a Structure in a Program
- •1.3 Program Notes
- •1.4 Other Structure Properties
- •1.5 Arrays of Structures
- •2. Lab Overview
- •3. Report Structure
- •4. Control Exercises
- •5. References
- •Lab #6. Strings
- •1. Theory
- •1.1. Introduction to Strings
- •1.2. Concatenating String Constants
- •1.3. Using Strings in an Array
- •1.4. Reading String Input a Line at a Time
- •1.5. Line-Oriented Input with gets()
- •1.6. Introducing the string Class
- •1.7. Assignment, Concatenation, and Appending
- •1.8. More string Class Operations
- •1.9. More on string Class I/o
- •2. Lab Overview
- •3. Report Structure
- •4. Control Questions
- •5. References
- •Annex a Report’s Title Page
- •From discipline “Algorithmization and Programming” Topic: _______________________________________________
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.
