Lab #5.1. Logical expressions (C++, Python)

  1. Open lab5.1.cpp (lab5.1.py).
    1. C++

#include <iostream>

using namespace std;

int main ()

{

    int a;

    cin >> a;

    if (a >= 0 && a <= 9) cout << "GOOD" << endl;

    else cout << "bad" << endl;

    return 0;

}

    1. Python

a = int(input())

if a >= 0 and a <= 9: print("GOOD")

else: print("bad")

Run three times – with input values  5, 5, and 15 respectively.

Output: “bad”/“GOOD”/“bad”.

  1. Transform the program to a functionally identical one with the following requirements: (a) avoid using && (and) in the branching condition, (b) Only one branching condition should be used (don’t use else if)
  2. Run three times – with input values  5, 5, and 15 respectively. The outputs should be the same.

Two correct solutions: lab5.1a.cpp, lab5.1b.cpp (lab5.1a.py un lab5.1b.py)

 

 

Lab #5.2. Loops (C++)

1.      Open lab5.2.cpp.

#include <iostream>

using namespace std;

 

int main ()

{

    int sum = 0, i, n = 10;

    for (i=1; i<=10; i++)

    {

        sum += i;

    };

    cout << "Sum = " << sum << endl;

    return 0;

}

2.      Run program, the sum of numbers 1..10 is printed:

Sum = 55.

  1. Change line
    for (i=1; i<=10; i++)
    to
    for (i=0; i<10; i++)

So the counter now covers the scope 0..9 (lab5.2a.cpp)

4.      Run program and get 45.

  1. Modify the loop body to get sum of 1..10 calculated again (lab5.2b.cpp).

6.      Run program and get Sum = 55.

  1. According to the scheme below, transform for loop to while loop (lab5.2c.cpp).

for (<1>;<2>;<3>) <4>

<1> while(<2>) {<4><3>}

8.      Run program and get Sum = 55.

9.      Transform the program, preserving its functionality, replacing while by do-while (lab5.2d.cpp).

10.  Run program and get Sum = 55.

11.  Open file lab5.2.cpp again, run and get 55.

12.  Swap the following excerpts of code: sum += i and i++ (lab5.2e.cpp).

13.  Run program and get Sum = 65, i.e. sum of numbers 2..11.

14.  Reaplace
i=1; i<=10
by
i=0; i<10
(lab5.2f.cpp).

15.  Run and get Sum = 55, i.e., again sum of numbers 1..10.

 

Lab #5.3. Nested loop (*)

1.      Open lab5.3.cpp and run.

#include <iostream>

#include <iomanip>

using namespace std;

 

int main ()

{

    for (int i=1; i<=4; i++)

    {

        for (int k=1; k<=4; k++) cout << setw(4) << i*k;

        cout << endl;

    };

    return 0;

}

Multiplication table 4X4 is obtained, using nested loop (2-level loop).

2.      Try to do the same using just one-level loop (lab5.3a.cpp).

Guidance: to compensate for the inner loop, you should probably need an if statement and operations / and % (division, remainder).

 

 

Lab #5.4. Exercises with loops

Exercise 1. (lab5.4a.cpp)

Given natural number n.

Print tree-shaped figure of asterisks (n lines, each next line has one more asterisk).

Example. n=6; output:

*

**

***

****

*****

******

 

Exercise 2. (lab5.4b.cpp)

Given natural odd number n.

Print square n × n of symbols: asterisks (‘*’) on diagonals, and the rest with minuses (‘-’).

Example. n=11; output:

*---------*

-*-------*-

--*-----*--

---*---*---

----*-*----

-----*-----

----*-*----

---*---*---

--*-----*--

-*-------*-

*---------*

 

Exercise 3. (lab5.4c.cpp)

Given natural number n.

Print square n × n according the pattern below (with n=13):

*--*--*--*--*

-*--*--*--*--

--*--*--*--*-

*--*--*--*--*

-*--*--*--*--

--*--*--*--*-

*--*--*--*--*

-*--*--*--*--

--*--*--*--*-

*--*--*--*--*

-*--*--*--*--

--*--*--*--*-

*--*--*--*--*

 

Lab #5.5. Loops (Python)

1.      Open lab5.2.py.

sum = 0

for i in range(1,10):

    sum += i

print ("Sum =",sum)

2.      Run program – it calculates sum of numbers 1..10.

Prints: Sum = 45.

Note, that the construct range() ignores 10 (excludes it) – it is the valueafter’.

  1. Chanhe line

for i in range(1,10):

to

for i in range(0,9):

Thus calculation of sum of numbers 0..8 is carried out, and output is 36 (lab5.2a.py)

  1. Adjust loop body keeping the same loop heading, to calculate sum of 1..9, so printing 45 (lab5.2b.py).
  2. Basing on the scheme right below, change for loop

for i in range ({1},{2}):

{3}

  1. to while loop (lab5.2c.py):

i = {1}

while(i < {2}):

{3}

i += 1

7.      Run program, which should print Sum = 45.