- •Vasyl Yatskiv,
- •Content
- •Introduction
- •1. Theory
- •1.1. Arithmetic Operators
- •1.2. Relational Operators
- •1.3. Logical Operators
- •1.4. Increment/Decrement Operators
- •1.6. Conditional Operator
- •1.7. Comma Operator
- •1.5. Assignment 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. 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)
