**Void main()**

{ // Main program block

int n, mxA, mxB, mxC;

double srMin, srMax;

srMin=(double)(mm(a,&mxA)+mm(b,&mxB)+mm(c,&mxC))/3; // three

function calls

printf(“srMin = %gn”,srMin);

srMax=(double)(mxA+mxB+mxC)/3;

printf(“srMax = %gn”,srMax);

getch();

}

*Example 2.* Fill in the Pythagorean table and calculate the sum of its elements by passing a pointer to a pointer to the function.

#include

using namespace std;

double fill(int **M, int m, int n);

void print(int **M, int m, int n);

double S=0;

**int main()**

{

int m = 10; //Number of rows in the array

int n = 10; //Number of columns in the array

int **A; //Two-dimensional array

//Create a two-dimensional array

A = new int*[m]; //Create an array of pointers

for (int i=0; i

A[i] = new int[n];

double sum = fill(A, m, n); //Filling the array

print(A, m, n); //Output array

cout << "Summ = " << sum << endl;

return 0;

}

**double fill(int **M, int m, int n)**

{

for (int i=0; i

for (int j=0; j

{

M[i][j] = (i+1)*(j+1);

S+=M[i][j];

}

**return S;** //Return the sum of elements

}

**void print(int **M, int m, int n)**

{

for (int i=0; i

{

for (int j=0; j

{

cout width(5);

cout << M[i][j];

}

cout << endl;

}

}

**Job** (program 3_1)

Develop programs from multiple functions that return two or more values for the following tasks.

1. Write a subroutine for multiplying two square matrices,

with which to calculate the second, third and fourth powers of the matrix

M[1..5,1..5]. For each matrix, calculate the sum and arithmetic mean of the elements.

2. Given arrays A [1..6], B [1..6], C [1..6], calculate the sums of positive and negative elements. Get A*B, B*C, A*C.

3. Two matrices of integers V [1..2,1..3], W [1..3,1..2] are given. Find the sums of the elements of rows and columns in these matrices. Compare the total sums of the elements in the corresponding matrices, calculated through the sums of the rows and the sums of the columns.

4. Given real arrays A [1..6], B [1..6] and C [1..6] calculate

5. Two matrices of integers V [1..2,1..3], W [1..2,1..3] are given. Find the total sums of the elements of the strings, return two sums from the function.

6. Given arrays A [1..5], B [1..6]. Get new arrays by shifting the elements in the arrays by two digits to the right, zeroing the elements that have been released on the left. Arrange the shift of elements in the array by a subroutine, from the subroutine return the digits that have dropped out on the right.

7. Two integer square matrices of even order are given. Replace array elements with even row and column numbers with zero (delete). Print the original and resulting arrays. Count the number of even and odd numbers in each.

8. Calculate the value of the function Z = x1+e ^{x} ^{2} , where x1, x2 are the roots of the equation A _{i} x ^{2} + B _{i} x + C _{i} = 0, where i = 1, 2,.., N. The coefficients of the equation are given in arrays A [1..N], B [1..n], C [1..N]. Use a subroutine to calculate roots.

9. Two sixth-order integer square matrices are given. Print the elements of the main diagonals of each of them and calculate the sums of the elements of the main and secondary diagonals separately.

10. Two one-dimensional arrays A [1..6] and C [1..6] are given. Get A ^{2} , C ^{2} , A*C. Count the number of even and the number of odd numbers in the resulting arrays. Multiply arrays and count in a subroutine.

11. Given arrays A [4][4], B [4][4]. Get new arrays by shifting the elements in the arrays by two digits to the right, zeroing the elements that have been released on the left. Shift elements in the array to arrange a subroutine, from the subroutine to return the sum of the elements above the main diagonal and separately below.

12. Given three one-dimensional arrays of real numbers A [1..6], B [1..8] and

From [1..7]. Find the geometric mean of the positive elements and the numbers of the elements closest to them for each array.

13. Given two matrices of integers S [1..3,0..2], K [1..3,0..2], each of which contains two identical numbers. Print their values.

14. Given arrays A [1..6], B [1..6], C [1..6]. Transform them, assigning each element of the array the value of the one next to it on the right. Assign the value of the first element to the last element. Print the original, resulting arrays and separately the last two elements of the resulting array.

15. Given elements of real matrices A, B and C, calculate

16. Calculate (min A _{i} ) / max A _{i} + (max C _{i} ) / min (C _{i} ) + max (B + C) _{I} / min (B + C) _{i} .

17. Given two integer square matrices of even order. Print arrays, transposed matrices, number of even and number of odd numbers in each.

18. Given arrays A [1..6], B [1..8]. Choose positive elements from them and write them down into arrays A [1..k] and B [1..k], respectively, where k < 6, n < 8;

form arrays A2 [1..6-k], B2 [1..8-n] from negative elements. Print sums and products of elements for each.

19. Given square matrices A, C and B of size 4×4 each. Print the arithmetic mean values in them separately for positive and negative elements.

20. Given arrays of integers F [8], В [8]. Find and print the values and indices of the minimum elements, how many of the same in each array.

21. Given three one-dimensional arrays of real numbers A [1..6], B [1..8] and

From [1..7]. Find the geometric mean of the positive elements and the numbers of the elements closest to them for each array.

22. Two sixth-order integer square matrices are given. Print the elements of the main and side diagonals of each of them, calculate the sums of the elements of the main and side diagonals separately.

Lab #4

Creating projects from multiple files in Eclipse IDE C/C++

**Instructions for working in Eclipse IDE С/С++**

The main window (workplace) of the Eclipse IDE software development environment is shown in the following figure.

The workplace is divided into parts (perspectives):

1) project browser,

2) source code editor,

3) console browser,

4) source code file,

5) “Project” menu.

Projects are created in the Project Explorer. In the editing window, the source code is entered and edited. The Eclipse IDE uses the console to display various messages or output from console applications.

To create a project, you must run File > New > C Project. It is also possible to create a project using the context menu by right-clicking on the Project Explorer and selecting New > C Project in the pop-up window.

In the dialog box, you must specify the name of the project and click the “Finish” button. If the project is successfully created, it will be displayed in the Project Explorer.

Next, you need to create a C source file. To do this, right-click on the project in Project Explorer and select New > Source file.

In the dialog box that appears, you need to enter the name of the source file and give it the extension **.c** . The file will appear in the project browser (field 4). Next, enter the text of the program into the source code editor window.

To compile the program, you must select the “Build Project” option in the “Project” menu (field 5) or right-click on the project in the Project Explorer and select the same “Build Project” option. If there are no errors in the project, then you can run the application by pressing

In modular programming, the structure of the main program is designed, then the modules are developed and programmed.

*Example* . Find the ratio between the maximum elements for arrays A and B.

Let’s separate the calculation of the largest element into a subroutine, which we will take out in a separate module.

#include

#include

#include “r_pos.h” // connecting the module from the current directory

int i;

**Void main()**

{

int a[8], b[8];

int s;

printf(“Enter array A of 8 ints n”);

for ( i=0; i<8; ++i )

scanf(“%d”, &a[i]);

printf(“Enter array B of 8 intsn”);

for (i=0; i<8; ++i)

scanf(“%d”,&b[i]);

printf(“Message = “);

printf(” max(A)/max(B) = %6.3f n”, (float)max(a)/max(b));

getch();

}

// Interface part of the **r_pos** module

typedef int * mass;

int max(mass d);

**extern int i;**

// Executable part of the module

**int max( mass d )**

{

intmx;

mx = d[0];

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

if (mx < d[i]) mx = d[i];

printf(” %d: “, mx);

return mx;

}

**Job** (Program 4_1)

Develop programs by hiding subroutines in a separate file (module), while using the passing of parameters to functions and a global variable, to which, if possible, organize a link from another file (module).

1. For matrices of integers S [1..3,0..2], K [1..3,0..2], in which there are two identical numbers each, find and print their values and indices.

2. Two matrices of integers M [1..3,0..1], K [1..3,0..2] are given. Find the arithmetic averages for each of them and the indices of the numbers closest to them.

3. For arrays A [1..6], B [1..6], C [1..6], rearrange the elements in them in such a way that negative numbers are written on the left and positive numbers on the right. Count the number of positive, negative and zero elements in each array.

4. One-dimensional arrays A [1..6], B [1..8], C [1..10] are given. Write them in the form of matrices AA [1..3,1..2], BB [1..2,1..4], CC [1..5,1..2], find the smallest elements in new and print their numbers.

5. For the given matrices of integers V (-1..2,0..3), W [1..3,0..2], form one-dimensional arrays VV and WW, writing the elements line by line. Print the indices of the original matrices for the maximum values and the values themselves.

6. Given a matrix of numbers H [1..5,1..6]. Rearrange the elements in the rows of the matrix so that they are arranged in ascending order of their modules. Calculate how many positive, negative numbers and zeros are in the original matrices.

7. Rearrange the elements in the matrix of numbers G [1..3,1..6] so that the elements of each column of the matrix are shifted cyclically to the right. Permutation of elements in a column is implemented by a subroutine, print three “dropped out” elements.

8. Given arrays A [1..8], B [1..8], C [1..8]. Sort the elements in them in descending order of their modules, print the smallest and largest values and their numbers.

9. For the given arrays A [4][4], B [1][6], C [4][6], calculate the sums of positive and separately negative elements, the number of zeros.

10. Given two matrices of integers V [1..2,1..3], W [1..2,1..3]. Find the total sums of the elements of the columns, return three sums from the function.

11. By the elements of two arrays A [5][5] and C [5][5] get A ^{2} , C ^{2} , A*C. Count the number of positive, number of negative numbers and zeros in the resulting arrays. Multiply arrays and count in a subroutine.

12. Matrices of integers S [0..3,0..2], K [0..3,0..2] are given. Find and print the values and indices of the minimum elements.

13. For matrices consisting of integers S [3][3], C [3][3], B[3][3], find and print the values and indices of the maximum elements.

14. Two matrices of integers A [3][2], K [3][2] are given. Find the arithmetic averages for each of them and the indices of the numbers closest to them.

15. For matrices F [4][4], C [4][4], find and print the values and indices of the maximum negative elements and separately positive ones.

16. One-dimensional arrays K [1..8], C [1..10] are given. Write them in the form of matrices KK [1..2,1..4], CC [1..5,1..2], find the largest elements in each and print their indices.

17. For three one-dimensional arrays of integers A [1..9], B [1..8] and С [1..7], count the number of non-negative elements in each and find the two numbers closest to them.

18. Matrices of integers F [4][4], C [4][4] are given. Find and print the values and indices of the least positive non-zero elements.

19. For each of the three matrices of real numbers, find the geometric mean of the positive elements and the indices of the elements closest to them.

20. Given square matrices A and B of size 5×5. Select positive and negative elements from them and write them down respectively in the arrays Apol, Aotr and Bpol, Votr. Count the number of numbers in each resulting array. Find the arithmetic and geometric mean values of the elements for each array.

21. For 5×5 square matrices A and B, print the arithmetic mean and nearest to them in each matrix.

22. Given arrays of integers K [7], B [7], G [7]. Find and print the values and indices of the maximum elements, how many elements in each array that match the maximum.

Lab #5

Function Return Programming Structures

Structs are made up of a number of fields, so multiple values can be calculated in one function and placed in different fields of the structure. Since the structure is a single object, it can be returned in its entirety from the function with the return statement.

*Example.* Calculate the sum of the elements, the maximum and minimum values in an array using the return from the struct function.

#include

typedef struct array_info

{

int max, min, sum;

}

ARRAY_INFO;

ARRAY_INFO GetArrayInfo(int*, int);

ARRAY_INFO GetArrayInfo(int* array, int len)

{

ARRAY_INFO info = {array[0], array[0], array[0]};

int i = 1;

while(i < len)

{

if (array[i] > info.max) info.max = array[i];

if (array[i] < info.min) info.min = array[i];

info.sum += array[i];

i++;

}

**return info;**

}

**int main()**

{

int mass[6] = {1, 22, 3, -4, 5, 6};

int len = 6;

ARRAY_INFO s = GetArrayInfo(mass, len);

printf(“min=%d max=%d sum=%d”,s.min, s.max, s.sum);

return 0;

}

*Exercise*

For tasks from labs 3 and 4, develop programs that return multiple values through structure fields.

Lab #6

Basics of programming in the Code::Blocks system

**Instructions for working in Code::Blocks**

To implement the labs, you need the ** Code::Blocks** development environment, which supports the C and C++ languages. It is launched through the

*codeblocks.exe*file. This will open an edit window with a menu.

To create a new project, follow the ** Create a new project** or

**This will open the project creation window, in which you need to select the type of new project –**

*File-New-Project link.***(Console application) and click on the button**

*Console application**“*

*Go**“*(Go).

Further, upon successful creation of the application, the *Console application window will open,* in which you must select the language C or C ++ from the list (on the instructions of the teacher) and click on the ** “Next”** button.

In the next window, you need to enter the name of the project and the path for the project to be created and click on the ** “Next”** button (Further).

The fields of the next project creation window must be filled in as shown in the figure.

By pressing ** the “Finish”** button in the project manager in the

**tab, open the**

*Projects***folder, find the**

*Sources***file and open it. After that, an editing window will open with a short program “**

*main.c**Hello, world!”*“.

Next, the program code is typed, compiled and executed by pressing the ** “Build log”** window.

When compiling a program in the ** Code::Blocks** environment, the most common mistakes are the following.

1. ** Compiling the previous program instead of the current one.** You need to restart

**and compile the desired program again.**

*Code::Blocks* 2 *. “*

**must correctly specify the path to the compiler (to the**

*Program name is Debug” uses an invalid compiler. Skipping… You***folder) in the**

*CodeBlocks/MinGW***menu in the**

*Settings – Compiler and debugger***tab.**

*Toolchain executables*Development of programs from several functions.

Calculating the value of integrals

*Example.* Calculate the value of the integral using two methods: the method of rectangles and the method of trapezoids. Create a separate subroutine for each method.

The integrand **f = pow((x+1),(-1/3)).**

# include

# include

## Be First to Comment