Lab #4.1. Comparison operators and if statement
I
(C++, Python)
a. C++:
#include
<iostream>
using namespace std;
int main ()
{
int a;
cin >> a;
if (a > 0) cout
<< "BIG" << endl;
else cout <<
"small" << endl;
return 0;
}
b. Python:
a = int(input())
if a>0:
print("BIG")
else:
print("small")
2. Run 3 times
with input values 5, 0, and -5 respectively.
Output BIG,
small and small is obtained
A right
solution can be found in lab4.1a.cpp (lab4.1a.py).
Lab #4.2. Comparison operators and if statement
II
(C++, Python)
1. Open lab4.2.cpp (lab4.2.py) and run:
a. C++:
#include
<iostream>
using namespace std;
int main ()
{
int a;
cin >> a;
if (a > 0) cout
<< "BIG" << endl;
else if (a == 0)
cout << "z-e-r-o" << endl;
else cout <<
"small" << endl;
return 0;
}
b. Python:
a = int(input())
if a>0: print("BIG")
elif a==0: print("z-e-r-o ")
else: print("small")
Two comparison
operators were used to obtain the result (> and ==).
A correct
solution can be found here: lab4.2a.cpp (lab4.2a.py).
A correct
solution can be found here: lab4.2b.cpp (lab4.2b.py).
A correct
solution can be found here: lab4.2c.cpp (lab4.2c.py).
A correct
solution can be found here: lab4.2d.cpp (lab4.2d.py).
Lab #4.3. Comparison operators and if statement
III
(C++, Python)
1. Open lab4.3.cpp and run:
a. C++:
#include
<iostream>
using namespace std;
int main ()
{
int a;
cin >> a;
if (a > 0)
{
if (a > 9)
cout << "BIG PLUS" << endl;
else cout
<< "small plus" << endl;
}
else
{
if (a < -9)
cout << "BIG -" << endl;
else if (a
< 0) cout << "small -" << endl;
else cout
<< "z-e-r-o" << endl;
};
return 0;
}
b. Python:
a = int(input())
if a>0:
if a>9: print("BIG PLUS")
else: print("small plus")
else:
if a<-9: print("BIG -")
elif a<0: print("small -")
else: print("z-e-r-o")
a. C++:
if
...
else if ...
else if ...
else if ...
else
b. Python:
if ...
elif ...
elif ...
elif ...
else
A correct
solution can be found here: lab4.3a.cpp (lab4.3a.py).
Lab #4.4. Looping statement: for (C++, Python)
1. Run lab4.4.cpp (lab4.4.py)
a. C++:
#include
<iostream>
using namespace std;
int main ()
{
int i,a;
for (i=0; i<5;
i++)
{
a = i * i;
cout <<
a << endl;
};
cout << i
<< endl;
return 0;
}
b. Python:
for i in range(0,5):
a = i * i
print(a)
print(i)
The program
prints squares of numbers.
Observe that
the value if i after the loop is 5.
for (i=0; i<5; i++)
to
for (int i=0; i<5; i++)
Declaration if
variable i is moved inside the loop (lab4.4a.cpp).
3. (only C++) compile.
Compiling
fails, because variable i (as declared within loop) does not exist after loop.
4. Remove line
a. C++ (lab4.4b.cpp):
cout << i << endl;
b. Python (lab4.4b.py):
print(i)
5. Run.
6. After line
a = i * i;
insert
a. C++ (lab4.4c.cpp):
if (i == 3) continue;
b. Python (lab4.4c.py):
if i == 3: continue
7. Run.
Square of the
number 3 is not printed. The operator continue ignores all statements until the
end of the loop body and jumps to the next iteration.
8. After line
a = i * i;
insert
a. C++ (lab4.4d.cpp):
if (i == 2) break;
b. Python (lab4.4d.py):
if i == 2: break
Only 0 and 1 are printed.
The operator break breaks the execution of the loop
and jumps to the next line after the loop. In that context, the operator continue for this particular program has no
more sense. The operator break is an alternative for the loop condition to exit loop.
Lab #4.5. Exercises with if statement (C++, Python)
Exercise 1. (lab4.5a.cpp/lab4.5a.py)
Given 2 integers a, b.
If both
numbers are less than 0, then print out product of a and b,
otherwise
print out sum of these numbers.
Example #1.
a=-7, b=-5; output 35.
Example #2. a=7, b=-5; output 2.
Exercise 2. (lab4.5b.cpp/lab4.5b.py)
Given 3 integers a, b, c.
Find the
middle number by value and find out is it closer to the smallest number or
the biggest number.
Example #1. a=1, b=2, c=3; output: 2, same distance.
Example #2.
a=-7, b=2, c=-5; output: -5, closer to the smallest.
Example #3.
a=-7, b=2, c=5; output: 2, closer to the biggest.
Exercise 3. (lab4.5c.cpp/lab4.5c.py)
Given 3 integers a, b, c.
Find out if
there are negative numbers among input values, and, if yes, print out the
biggest of the negative numbers.
Example #1. a=1, b=2, c=3; no negative numbers.
Example #2. a=7, b=-2, b=-5; output -2.
Lab #4.6. Exercises with for statement (C++, Python)
Exercise 1. (lab4.6a.cpp/lab4.6a.py)
Given natural
number n.
Print a 2D
figure from asterisks *, consisting of n rows, with number of asterisks in
each row 1,2,3,1,2,3, and so on.
Example. n=10; output:
*
**
***
*
**
***
*
**
***
*
Exercise 2. (lab4.6b.cpp/lab4.6b.py)
Given natural
number n.
With using
for loop and counting, calculate the sum of the first n odd numbers.
Example. n=10; output: 100
(because 1+3+5+7+9+11+13+15+17+19=100)