Theory
Note about for loop
The for
loop allows you to use several counters: the initialization and increment sections can contain several operators separated by the «,» (comma) operator
int a = 0, b = 0; for (a = 1, b = 8; a < b; ++a, --b) { // empty body } cout << a << " " << b << endl; // output 5 4 |
References
References are not objects; they do not necessarily occupy storage; references are the other name of the variable:
sample 1:
int i = 5; int &pi = i; // reference to i pi = 3; cout << i; // i == 3 |
sample 2:
void double_numb(int &numb) { numb *= numb; // 'numb' is the same as main()'s 'x' } void main(){ int x = 2; double_numb(x); cout << x << '\n'; system("pause"); } |
Sample 3. Passing an argument to a function by reference:
void calcSquareAndPerimeter(double a, double b, double &S, double &P) { S = a * b; P = 2 * (a + b); } int main() { double SS = 0.0, PP = 0.0; calcSquareAndPerimeter(3.0, 4.0, SS, PP); cout << "Perimeter: " << PP << ", Square: " << SS << "\n"; } |
Labs and tasks
- Create an empty application with a name
Lesson_3
. All the tasks and labs must be done within this application. - It means that all tasks must be done within the same header and source files. But you should put comments with the task number and explanation of what to do.
- Every task has to be done using user functions. For example, if the task is to find the sum of a sequence of 5 entered numbers, you must create a function within
.cpp
file to do this task: - Try to pass arguments of the functions by reference (in the tasks where it is needed).
header.h:
1 2 3 4 5 6 | #ifndef HEADER_H #define HEADER_H // task 1 // function to calculate the sum of 5 enterered numbers int sumSeq(); #endif |
imp.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> #include "header.h" using namespace std; // function to calculate the sum of 5 enterered numbers int sumSeq() { cout << "enter a sequence"<<endl; int numb; int sum = 0; for (int i = 0; i < 5; i++) { cin >> numb; sum += numb; } return sum; } |
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 | #include "header.h" #include <iostream> using namespace std; void main(){ // task # 1 // craeate a function to calculate a sum of 5 entered numbers int result = sumSeq(); cout << "task 1" << endl; cout << result; system("pause"); } |
To do: Create a function to output the sequence: -3 0 3 6 9 12 15 18 21 24
. Make the task with FOR
loop. An iterator of the loop has a step equaled 3.
Note 1: To make a step equal to 3 see syntax:
for ([initializers]; [condition]; [iterator])
instead of the iterator you’ll have the variable counter += 3
Note 2: The signature of the function must be void printSeq(int & a, int & b)
, where a = -3 and b = 24.
Note 3: Don’t forget to use assert
statement to check if the boundary b
is greater than a
.
#include <cassert> |
Expected output:
The sequence: -3 0 3 6 9 12 15 18 21 24
[Solution and Project name: Lesson_3
files: header.h
, imp.cpp
, main.cpp
]
// task 1
L3Task1header.h
, L3Task1imp.cpp
, L3Task1main.cpp
To do: 10 integers are entered. Create a function (findPosNeg
) to output the quantity of positive and negative among them.
Note 1: Create for
loop to input the numbers (they must be entered). Within the loop, check each number whether it is positive or negative. Use two counters for calculations.
Note 2: The signature of the function must be:
void findPosNeg(int & pos, int & neg){ //TODO } |
Expected output:
enter 10 numbers: 1 -5 -12 2 3 9 -1 9 5 -8 counter_positive = 6, counter_negative = 4
[Solution and Project name: Lesson_3
]
// Task 2
To do: Create a function to calculate the addition of the following sequence: 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19
(numbers are NOT entered, they must be created using a loop). The first number (1
) and last number (19
) of the sequence must be parameters of the function passed by reference.
Note: To calculate the addition, use a variable named sum
.
Expected output:
the sequence: 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 sum = 100
[Solution and Project name: Lesson_3
]
// Task 3
To do: For each x
changing in the interval [
(x1
;x2
]x1
and x2
values are entered), calculate the value of the function z(x,y) = xy
. The variable y
changes in the interval [y1;y2]
(y1
and y2
values are entered). You must create a function.
Note 1: Create two for
loops (nested loop): one loop within the other. Variable x
has to be modified in the outer loop; variable y
has to be modified in the inner loop.
Note 2: To output the power of a number, use pow
function and #include<cmath>
directive:
f = pow(base number, exponent); |
Note 3: Don’t forget to use the assert
statement to check the boundaries (x2>x1
and y2>y1
).
Expected output:
enter x1 and x2: >>>2 >>>8 enter y1 and y2: >>>2 >>>4 z(x,y) = 2^2 = 4 z(x,y) = 2^3 = 8 z(x,y) = 2^4 = 16 z(x,y) = 3^2 = 9 z(x,y) = 3^3 = 27 z(x,y) = 3^4 = 81 z(x,y) = 4^2 = 16 z(x,y) = 4^3 = 64 z(x,y) = 4^4 = 256 z(x,y) = 5^2 = 25 z(x,y) = 5^3 = 125 z(x,y) = 5^4 = 625 ... etc.
[Solution and Project name: Lesson_3
]
// task 4
To do: Create a function to output the sequence 15 12 9 6 3 0
(from 15 down to 0 with a step = -3). Use while
loop.
Note: The signature of the function must be:
void printSeqTask5(int & a, int & b){ … } |
Where a = 15
and b = 0
Expected output:
the sequence: 15 12 9 6 3 0
[Solution and Project name: Lesson_3
]
// task 5
To do: Create a function to calculate a multiplication of 2-digit even integers in the interval [10;20]
(10 * 12 * 14 * 16 * 18 * 20
). You have to do the task using a while
loop.
Note 1: To calculate a multiplication you should use a variable with the name product. Don’t forget to inisialize the product
variable with 1.
Note 2: The signature of the function must be:
void findMult(int & a, int & b, int & prod){ … } |
where a = 10
and b = 20
Expected output:
10 * 12 * 14 * 16 * 18 * 20 the product: 9676800
[Solution and Project name: Lesson_3
]
// task 6
To do: Two two-digit integers are entered. Create a BitwiseSum()
function that computes their bitwise sum modulo 10. For example, the bitwise sum of the numbers 34
and 59
is the number 83
(3 + 5 = 8; 4 + 9 = 13; 13%10 = 3).
Note: The BitwiseSum()
method must accept three integers as arguments passed by reference (two numbers and sum
).
Expected output:
Please enter two two-digit numbers: >>> 34 >>> 59 The bitwise sum of 34 and 59 is: 83
[Solution and Project name: Lesson_3
]
// task 7
To do: A sequence of integers is given (it is entered). The last element of the sequence is 0. Calculate the addition of all positive elements of this sequence and the number of its negative elements. While
loop must be used.
Note 1: In the task, you should use the endless loop with an exit from the middle:
while (true) { // TODO something if (num == 0) break; // TODO something } |
Note 2: To return two values from the function you should use function parameters by reference (&
):
void sumPosCountNeg(int & pos_sum, int & neg) {} |
Expected output:
Please enter the sequence, the last element is 0: >>> 3 5 -1 2 -3 0 The sum of positive numbers is: 10 The number of negatives is: 2
[Solution and Project name: Lesson_3
]
// task 8
To do: A sequence of integers is given (it is entered). The last element of the sequence is 0. Find minimum and maximum elements. A While
loop must be used.
Note 1: In the task, you should use the endless loop with an exit from the middle:
while (true) { // TODO something if (num == 0) break; // TODO something // } |
Note 2: To return two values from the function you should use function parameters by reference (&
):
void minMax(int &min, int &max) {} |
Note 3: You should use INT32_MAX
and INT32_MIN
initial values for min
and max
variables.
Expected output:
Please enter the sequence, the last element is 0: >>> 3 5 -1 2 -3 0 The minimum is: -3 The maximum is: 5
[Solution and Project name: Lesson_3
]
// task 9
EXTRA TASKS
To do: An integer is given. Find the number of its digits and their sum.
Note: The function signature should look like this:
void digitsCountAndSum (int n, int& count, int& sum) |
Expected output:
Enter number: >>> 12345 The number of digits = 5, the sum = 15
[Solution and Project name: Lesson_3
, files: L3ExTask1header.h
, L3ExTask1main.cpp
, L3ExTask1imp.cpp
]
To do: An integer N
and a set of N
positive real numbers are given. Calculate the sum of all integer parts, as well as the sum of all fractional parts of the sequence. Some of the standard
header functions may be helpful.
Expected output:
Enter N: >>> 4 12,4 11,7 8,5 1,1 Sum of integer parts is: 32 Sum of fractional parts is: 1.7
[Solution and Project name: Lesson_3
, files: L3ExTask2header.h
, L3ExTask2main.cpp
, L3ExTask2imp.cpp
]
Hometasks
To do: 10 real numbers are input. Create a function to output the multiplication of the entered numbers.
Note 1: Create for
loop to input the numbers. To calculate the multiplication, use a variable named product
.
Note 2: Don’t forget to initialize the product
variable with a double type value.
double product = 1.0;
Expected output:
1,1 2,4 5,1 7,2 6,4 8,1 6,7 3,2 3,3 2,4 product = 853338,921998746
[Solution and Project name: Lesson_3
, files: L3HomeTask1header.h
, L3HomeTask1main.cpp
, L3HomeTask1imp.cpp
]
To do: For each x
changing in the interval [30;33]
, calculate the value of the function z(x,y) = x - y
. The variable y
changes in the interval [1;5]
. You must create a function.
Note: Create two for
loops (nested loop): one loop within the other. The variable x
has to be modified in the outer loop; the variable y
has to be modified in the inner loop.
Expected output:
z(x,y) = 30-1=29 z(x,y) = 30-2=28 z(x,y) = 30-3=27 z(x,y) = 30-4=26 z(x,y) = 30-5=25 z(x,y) = 31-1=30 ... etc.
[Solution and Project name: Lesson_3
, files: L3HomeTask2header.h
, L3HomeTask2main.cpp
, L3HomeTask2imp.cpp
]
To do: Create a function to output the sequence 3 5 7 9 ... 21
(from 3 to 21 with a step = 2).
Note: the signature of the function must be void seq(int & a, int & b)
, where a = 3
and b = 21
.
Expected output:
3 5 7 9 11 13 15 17 19 21
[Solution and Project name: Lesson_3
, files: L3HomeTask3header.h
, L3HomeTask3main.cpp
, L3HomeTask3imp.cpp
]
To do: 5 real numbers are input. Create a function to calculate an addition (sum) of entered numbers. Make the task using a while
loop.
Note: Double type must be used for real numbers and for variable sum:
double sum = 0; double numb;
Please enter 5 numbers and press Enter
1.1 4.2 2.1 9.3 2.5 The sum of inputted numbers = 19.2
[Solution and Project name: Lesson_3
, files: L3HomeTask4header.h
, L3HomeTask4main.cpp
, L3HomeTask4imp.cpp
]