Lab #8.1. Array/list processing

Given one-dimensional array/list

a)     C++ (lab8.1.cpp):

#include <iostream>

using namespace std;

const int arr_size = 11;

 

int main ()

{

    int arr[arr_size]={0,-2,0,-10,2,-1,0,0,3,2,-3};

    return 0;

}

b)    Python (lab8.1.py):

arr=[0,-2,0,-10,2,-1,0,0,3,2,-3]

 

Calculate the number of consecutive sign changes (from + to – or vice versa, assuming that 0 has no sign).

The algorithm should traverse the array and compare neighbouring numbers – if signs are different, the occasion is counted.

E.g.,

0

-2

0

-10

2

-1

0

0

3

2

-3

should return 3, because (-10->2; 2->-1; 2->-3)

 (lab8.1a.cpp, lab8.1a.py).

 

 

Lab #8.2. Array processing

Given one-dimensional array m (lab8.2.cpp).

#include <iostream>

using namespace std;

const int arr_size = 6;

 

int main ()

{

    double arr[arr_size]={1, 2, 3.5, -5, 6.2, 0};

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

    return 0;

}

Starting from beginning of array, change the value of mi to the average of first i elements of the original array.

E.g.,

1

2

3.5

-5

6.2

0

should be changed to

1

1.5

2.1667

0.375

1.54

1.2833

 (lab8.2a.cpp).

because

1.5 = (1+2)/2

2.1667 = (1+2+3.5)/3

...

1.2833 = (1+2+3.5-5+6.2+0)/6

 

 

Lab #8.3. 2D-array processing

Given matrix A(m,n) as a 2D-array of integers (lab8.3.cpp).

#include <iostream>

#include <iomanip>

using namespace std;

const int m = 3;

const int n = 4;

 

int main ()

{

    int arr[m][n]={{3, 8, 4, 6},

                   {2, 7, 1, 5},

                   {0, 9, -1, 2}};

    for (int i=0; i<m; i++, cout<<endl)

    {

        for (int k=0; k<n; k++) cout << setw(4) << arr[i][k];

    };

    return 0;

}

3

8

4

6

2

7

1

5

0

9

-1

2

Starting from the top-left position, change each element aij to the minimum value of top-left submatrix A’(i,j).

3

3

3

3

2

2

1

1

0

0

-1

-1

 (lab8.3a.cpp).