
- •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: _______________________________________________
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 |