Lab #6.1. Arrays

  1. Open lab6.1.cpp, compile and run.

#include <iostream>

using namespace std;

const int arr_size = 5;

 

int main ()

{

    int arr[arr_size];

    for (int i=0; i<arr_size; i++) cout << arr[i] << endl;

    return 0;

}

Five integers are printed (elements of an uninitialized array).

Observe line

const int arr_size = 5;

which declares a constant variable arr_size.

  1. Change array declaration line to

int arr[arr_size]={11,22,33,44,55,66};

(i.e., add initialization list) and compile (lab6.1a.cpp).

Compilation error is obtained: too many elements in the initialization lit.

lab6.1a.cpp: In function `int main()':

lab6.1a.cpp:7: excess elements in aggregate initializer

3.     Delete last element from the initialization list (66), compile, run and get printed: 11 22 33 44 55

4.     After array declaration line insert following lines (lab6.1b.cpp):

int arr2[arr_size];

arr2 = arr;

5.     Compile and get compilation error:

lab6.1b.cpp: In function `int main()':

lab6.1b.cpp:9: ISO C++ forbids assignment of arrays

6.     Change declaration of arr2 from int arr2[arr_size]; to

int *arr2;

Variable arr2 now is a pointer, assignment to arr2 takes memory address of array arr.

7.     After line arr2 = arr; insert:

arr2[2] = 999;

(lab6.1c.cpp)

8.     Compile and run.

It can be deduced, that there is a single array in the program, referred by two variables arr and arr2: the element #2 was changed through variable arr2, but printed through arr.

9.     After array printing line, insert:

    arr2 = new int[arr_size];

    for (int i=0; i<arr_size; i++) arr2[i] = arr[i]*arr[i];

    for (int i=0; i<arr_size; i++) cout << arr2[i] << endl;

    delete[] arr2;

(lab6.1d.cpp)

These lines (1) create a new array (with new) – variable arr2 doesn’t point to array arr anymore; (2) the created array is filled with squares of elements of arr; (3) the new array is printed; (4) the new array is deleted.

10.  Change value of arr_size to 6.

11.  Change declaration line of arr to:

int arr[arr_size-1]={11,22,33,44,55};

(lab6.1e.cpp)

With this change, (1) the array keeps the previous size, but the program attempts to print out more elements than there exists; (2) the array arr2 is of length 6, and the last element of it is filled with square of value of a non-existant element of arr.

12.  Compile and run the program, get printed 12 numbers.

This example illustrates, that C++ doesn’t control array boundaries.

 

 

Lab #6.2. (C-style) character strings

1.     Open lab6.2.cpp, compile and run.

#include <iostream>

using namespace std;

 

int main ()

{

    char s[20]="Hello, World!";

    cout << s << endl;

    return 0;

}

The program creates and prints out the character string “Hello, World!”.

0

1

2

3

4

5

6

7

8

9

10

11

12

13

H

e

l

l

o

,

 

W

o

r

l

d

!

\0

 

  1. Before printing line, insert:

s[5] = '\0';

(lab6.2a.cpp)

This way, element #5 (the one with comma) get the null character (i.e., string termination character).

3.     Compile, run and get printed “Hello”.

The current character string starts at the beginning of the array and ends at the null character.

0

1

2

3

4

5

6

7

8

9

10

11

12

13

H

e

l

l

o

\0

 

W

o

r

l

d

!

\0

 

4.     Change printing line to cout << s[7] << endl;

(lab6.2b.cpp)

0

1

2

3

4

5

6

7

8

9

10

11

12

13

H

e

l

l

o

\0

 

W

o

r

l

d

!

\0

5.     Compile, run and get printed: ‘W’

6.     Change printing line to cout << &s[7] << endl;

(lab6.2c.cpp)

7.     Compile, run and get printed: ‘World!’

0

1

2

3

4

5

6

7

8

9

10

11

12

13

H

e

l

l

o

\0

 

W

o

r

l

d

!

\0

Any point of an array can serve for the beginning of a string, it is not required to be the beginning of the array. & - address-of operator (address taking operator). After this example, there can be deducted that in this context constructs s and &s[0] operate identically.

8.     IChange printing line to cout << &s[2] << endl;

(lab6.2d.cpp)

9.     Compile, run and get printed: ‘llo’

0

1

2

3

4

5

6

7

8

9

10

11

12

13

H

e

l

l

o

\0

 

W

o

r

l

d

!

\0

 

 

 

Lab #6.3. Exercises on arrays and character strings

Exercise 1. (lab6.3a.cpp)

Given character string s. Calculate whether s contains consecutively repeating characters (answer should be – yes or no).

E.g., in string „Hallo, World!”, two characters ‘l’ are placed together.

 

Exercise 2. (lab6.3b.cpp)

Given character string s. Calculate whether s contains repeating characters (answer should be – yes or no).

E.g., in string „Hallo, World!” characters ‘l’ and ‘o’ are repeating.

 

Exercise 3. (lab6.3c.cpp)

Given int array arr[n].

Calculate whether arr contains repeating numbers (answer should be – yes or no).

E.g., in array {1,2,1,5,3} number 1 is repeating.

 

 

Lab #6.4. Lists (Python)

1.     Run lab6.1.py.

arr = [11,22,33,44,55]

for a in arr:

    print(a)

Output: 11,22,33,44,55.

  1. Modify the program (lab6.1a.py):

arr = [11,22,33,44,55]

for i in range(0,len(arr)):

    print(arr[i])

Another way, how to print the same values (numbering of elements: 0..4).

  1. Add the following line (lab6.1b.py):

print(arr[5])

Error message – no such index 5.

Traceback                    

    <module>

      lab6.1b.py  4 IndexError: list index out of range           

  1. Addiotional code for index checking (lab6.1bb.py):

try:

    arr = [11,22,33,44,55]

    for i in range(0,len(arr)):

        print(arr[i])

    print(arr[5])

except IndexError as err:

    print(err)

Output:

11

22

33

44

55

list index out of range

5.     Create program to generate and print the contents of a list, using for loop for generating (lab6.1c.cpp):

ARR_SIZE = 5

arr = [11*(a+1) for a in range(0,ARR_SIZE)]

for i in range(0,len(arr)):

    print(arr[i])

Printed numbers: 11,22,33,44,55 (as in the beginning).

6.     Add following lines (lab6.1d.cpp):

arr2 = arr

arr[0] = 999

for i in range(0,len(arr2)):

    print(arr2[i])

Printed numbers: 11,22,33,44,55,999,22,33,44,55.

Observe, That the both variables arr and arr2 point to the same list (arr[0]=999 set value 999, but print(arr2[i]) printed it)