Lab 3.1. Data types, represented values and data type conversion (C++)

  1. Run lab3.1.cpp.

The following information is printed:

         F

        50

     0.123

         1         0

Hallo, World!

Observe that if there is to little space (according to width or setw) to print out the text, the statement is ignored instead of trimming the text (see “Hallo, World!”).

Observe, that true and false are printed as 1 and 0 respectively.

123e-3 means 123*10-3.

2.      Before the outut lines insert:

    c2 = i1;

    i2 = c1;

3.      Out of 5 output lines, leave only the two, which print c2 and i2.

The listing of the main program now:

    char c1 = 'F', c2;

    int i1 = 50, i2;

    double d1 = 123e-3, d2;

    bool b1 = true, b2 = false;

    char *s1 = "Hallo, World!";

    c2 = i1;

    i2 = c1;

    cout << setw(10) << c2 << endl;

    cout << setw(10) << i2 << endl;

    return 0;

4.      Run program and get result.

         2

        70

See that (1) the digit ‘2’ as a character has numeric code 50, but ‘F’ –70. Observe that the value of a digit and its character code are two separate things.

5.      Change both output lines to:

    cout << c1 << " " << sizeof(c1) << endl;

    cout << i2 << " " << sizeof(i2) << endl;

6.      Run program.

F 1

70 4

Considering also line
c2 = i1

we can conclude from this output that symbol ‘F’ (or its code 70) is both printed differently depending from the data type, and takes different size of memory.

7.      Delete lines:

    c2 = i1;

    i2 = c1;

    cout << c1 << " " << sizeof(c1) << endl;

    cout << i2 << " " << sizeof(i2) << endl;

8.      Insert the following lines instead:

    d2 = d1 * 100;

    cout << d1 << " " << d2 << endl;

    b1 = (d1 < d2);

    b2 = !b1;

    cout << d1 << " less than " << d2 << ": " << b1 << endl;

    cout << d1 << " greater or equal than " << d2 << ": " << b2 << endl;

Listing of the program after changes:

    char c1 = 'F', c2;

    int i1 = 50, i2;

    double d1 = 123e-3, d2;

    bool b1 = true, b2 = false;

    char *s1 = "Hallo, World!";

    d2 = d1 * 100;

    cout << d1 << " " << d2 << endl;

    b1 = (d1 < d2);

    b2 = !b1;

    cout << d1 << " less than " << d2 << ": " << b1 << endl;

    cout << d1 << " greater or equal than " << d2 << ": " << b2 << endl;

    return 0;

9.      Run and get results.

0.123 12.3

0.123 less than 12.3: 1

0.123 greater or equal than 12.3: 0

Variable b1 stores value (true/false) with the meaning – “is it true that d1 is less than d2”;

b2 stores inverse value due to b2 = !b1; (exclamation mark ‘!’ denotes logical NOT, i.e., transformation to inverse logical value)

Logical values true and false, as before, print as 1 and 0.

10.  Delete lines:

    cout << d1 << " less than " << d2 << ": " << b1 << endl;

    cout << d1 << " greater or equal than " << d2 << ": " << b2 << endl;

11.  Insert the following lines instead:

cout << "d1<d2 " << b1 << " " << (b1 ? "true" : "false") << endl;

cout << "d1>=d2 " << (d1>=d2) << " " << (d1>=d2 ? "true" : "false") << endl;

12.  Run and get results.

0.123 12.3

d1<d2 1 true

d1>=d2 0 false

Here you see a way how to get printed true or false, or any other text from logical values 1 or 0. This can be done using the conditional operator (?:).

Listing of the program after changes:

#include <iostream>

#include <iomanip>

using namespace std;

 

int main()

{

    char c1 = 'F', c2;

    int i1 = 50, i2;

    double d1 = 123e-3, d2;

    bool b1 = true, b2 = false;

    char *s1 = "Hallo, World!";

    d2 = d1 * 100;

    cout << d1 << " " << d2 << endl;

    b1 = (d1 < d2);

    b2 = !b1;

    cout << "d1<d2 " << b1 << " " << (b1 ? "true" : "false") << endl;

    cout << "d1>=d2 " << (d1>=d2) << " " << (d1>=d2 ? "true" : "false") << endl;

    return 0;

}

13.  Remove all lines starting from
d2 = d1 * 100;
to both output lines inclusive.

14.  Insert the following lines instead:

    d2 = i1;

    cout << d2 << endl;

15.  Run and get result:

50

As you see, an int value can be assigned to a double variable without problems. In a variable of type double, the data type double will be attached to this value to potentially impact operations over this value (e.g., division).

16.  Before line
return 0;
insert:

    i2 = d1;

    cout << i2 << endl;

17.  Compile.

Compilator shows the warning that variable of type int is assigned value of type double, which can cause loss of information (digits after the decimal point).

C:/Darbi/prg/lab3.1b.cpp:14: warning: assignment to `int' from `double'

C:/Darbi/prg/lab3.1b.cpp:14: warning: argument to `int' from `double'

Execution terminated

Compilation successful

However the compilation is successful.

18.  Run and get result:

50

0

So, 0.123 is transferred to 0 by cutting away the mantissa (part after decimal point).

For controlled rounding in C++, the following functions are used: floor (rounds “down” to the closest integer), ceil (rounds “up” to the closest integer) and round (rounds mathematically).

19.  Instead of line
i2 = d1;
insert:

    i2 = ceil (d1);

20.  Add the following line in the beginning of the file:

#include <math.h>

21.  Compile.

Still compilation warning, because a double variable can theoretically store a bigger number than an int variable can.

22.  Run program and get result:

50

1

0.123 rounded up is 1

23.  Change line
i2 = ceil (d1);
to

    i2 = (int)ceil (d1);

(by adding extra (int))

24.  Compile and run.

Program works as without added (int), however compiles without warnings.

The essence of warnings is to take notice of places, which could potentially lead to unwanted side-effects, e.g., uncontrolled loss of information.

The final listing of the program:

#include <iostream>

#include <iomanip>

#include <math.h>

using namespace std;

 

int main()

{

    char c1 = 'F', c2;

    int i1 = 50, i2;

    double d1 = 123e-3, d2;

    bool b1 = true, b2 = false;

    char *s1 = "Hallo, World!";

    d2 = i1;

    cout << d2 << endl;

    i2 = (int)ceil (d1);

    cout << i2 << endl;

    return 0;

}

 

 

 

Lab #3.2. Numeric expressions (C++, Python)

1.      Run lab3.2.cpp (lab3.2.py).

Run and get result:

         14

because 14 = 2 + (3 * 4);

2.  Change line
d = a + b * c
to
d = (a + b) * c

Get result 20.

3.      Instead of
d = (a + b) * c
insert:

N.B. For division use operator ‘/’, for sinefunction sin(x) (C++) or math.sin(x) (Python), for cosine  function cos(x) (C++) or math.cos(x) (Python), for raising into power: pow(x,y) (C++) or x**y (Python). In C++, you should include mathematical library: #include <math.h>, in Python:: import math.

 

 

The output should be:

729.913

The correct solution see in lab3.2b.cpp (lab3.2b.py)

 

 

Lab #3.3. Data types, their representation and changing (Python)

1.      Open lab3.1.py and run:

c1 = 'F'

i1 = 50

d1 = 123e-3

b1 = True

b2 = False

s1 = "Hallo, World!"

print("{0:10s}".format(c1))

print("{0:10d}".format(i1))

print("{0:10.3f}".format(d1))

print("{0:10}{1:10}".format(b1,b2))

print("{0:10s}".format(s1))

Izdruka uz ekrâna:

F         

        50

     0.123

         1         0

Hallo, World!

The text automatically aligns to the left, but numbers to the right. (:10 means print area of 10 characters). If the emount of information is bigger than the area, the data is no cut, but the area extended instead. (see „Hallo, World!”)

Unlike C++, in Pythonthe two notions of char and charater string are not distinguished – a character is the same than character string of size 1. Observe, thar True and False are printed as 1 and 0 respectively. 123e-3 means 123*10-3.

  1. To align to the right/left explicitly, additional formatting symbols > and < can be used (now the text goes right, but numbers left) (lab3.1a.py):

c1 = 'F'

i1 = 50

d1 = 123e-3

b1 = True

b2 = False

s1 = "Hallo, World!"

print("{0:>10s}".format(c1))

print("{0:<10d}".format(i1))

print("{0:<10.3f}".format(d1))

print("{0:<10}{1:<10}".format(b1,b2))

print("{0:>20s}".format(s1))

Output to the console:

         F

50       

0.123    

1         0        

       Hallo, World!

3.      Before printing lines, insert:

    c2 = i1

    i2 = c1

4.      From the five printing lines, keep only two, the ones which print c2 and i2 respectively.

Now the main program looks like this (lab3.1b.py):

c1 = 'F'

i1 = 50

d1 = 123e-3

b1 = True

b2 = False

s1 = "Hallo, World!"

c2 = chr(i1)

i2 = ord(c1)

print("{0:10}".format(c2))

print("{0:10}".format(i2))

5.      Run program and get output.

2

        70

Function chr transforms character code to the character, but function ord vice versacharacter to the code. From the output, we can conclude: (1) the code of character ‘2’ is 50, but the code of character ‘F’ is 70. Note, that the value of a digit and the code of the digit are two different things, so ‘2’ as a character and 2 as a number are different values.