How to solve one-dimensional massifs

How to solve one-dimensional massifs

In computer science work with massifs is given great value. It is possible to present a set of elements of one type in the form of the massif. United in one structural group, these data have one name and indexes of placement by means of which access to each element is provided. Massifs can contain symbols, arithmetic data, structures, indexes, etc. The simplest consecutive set of elements is called the one-dimensional massif.

Instruction

1. Any solution of the one-dimensional massif has to consist in the appeal to its elements and any given their processing. At the same time cycles are usually used (for, while, etc.). As a rule, numbering of the index goes from the first element of the massif (i = 0) to the last (i

Announce one-dimensional array M of numerical type (int, float, etc.) with the set dimension of N where, for example, N is equal to 20. At the initial stage of work with the massif nullify all values of its elements. For this purpose appropriate to each of them value zero. The example of the corresponding code of the program in language C ++ will look as follows: int M[20]; for (int i = 0; i

Appropriate to massif element k a preset value, for example, number 255. In this case it is not required to set a cycle and to run each element, increasing index counter i. Will enough address element k by means of the following design of M[k] = 255.

Increase value of a penultimate element of the massif by 10. For this purpose at first it is necessary to calculate the index of this element. As the general dimension of the massif is known, and it will be equal to N, therefore, a penultimate element to have N-1 index. However here it is necessary to consider features of various programming languages. So, in With ++ indexation of elements of any massif begins not with the first, and with zero value, thus, the code of the program on With ++ with the solution of this task will look so: M [N-2] + = 10. The += operator makes addition of number 10 to already available value in a massif cell.

Appropriate to all nonzero elements of the massif value of their index. Here again it is necessary to use a cyclic design, but besides it it will be necessary to set a condition (if). Consistently in a cycle check each element of the one-dimensional massif whether its value is other than zero. If the condition is satisfied, then the data which are contained in an element are replaced with value of its index in the massif. An example of the code of the program in language C ++: for (int i = 0; i

2. Announce one-dimensional array M of numerical type (int, float, etc.) with the set dimension of N where, for example, N is equal to 20. At the initial stage of work with the massif nullify all values of its elements. For this purpose appropriate to each of them value zero. The example of the corresponding code of the program in language C ++ will look as follows: int M[20]; for (int i = 0; i

Appropriate to massif element k a preset value, for example, number 255. In this case it is not required to set a cycle and to run each element, increasing index counter i. Will enough address element k by means of the following design of M[k] = 255.

Increase value of a penultimate element of the massif by 10. For this purpose at first it is necessary to calculate the index of this element. As the general dimension of the massif is known, and it will be equal to N, therefore, a penultimate element to have N-1 index. However here it is necessary to consider features of various programming languages. So, in With ++ indexation of elements of any massif begins not with the first, and with zero value, thus, the code of the program on With ++ with the solution of this task will look so: M [N-2] + = 10. The += operator makes addition of number 10 to already available value in a massif cell.

Appropriate to all nonzero elements of the massif value of their index. Here again it is necessary to use a cyclic design, but besides it it will be necessary to set a condition (if). Consistently in a cycle check each element of the one-dimensional massif whether its value is other than zero. If the condition is satisfied, then the data which are contained in an element are replaced with value of its index in the massif. An example of the code of the program in language C ++: for (int i = 0; i

3. Appropriate to massif element k a preset value, for example, number 255. In this case it is not required to set a cycle and to run each element, increasing index counter i. Will enough address element k by means of the following design of M[k] = 255.

4. Increase value of a penultimate element of the massif by 10. For this purpose at first it is necessary to calculate the index of this element. As the general dimension of the massif is known, and it will be equal to N, therefore, a penultimate element to have N-1 index. However here it is necessary to consider features of various programming languages. So, in With ++ indexation of elements of any massif begins not with the first, and with zero value, thus, the code of the program on With ++ with the solution of this task will look so: M [N-2] + = 10. The += operator makes addition of number 10 to already available value in a massif cell.

5. Appropriate to all nonzero elements of the massif value of their index. Here again it is necessary to use a cyclic design, but besides it it will be necessary to set a condition (if). Consistently in a cycle check each element of the one-dimensional massif whether its value is other than zero. If the condition is satisfied, then the data which are contained in an element are replaced with value of its index in the massif. An example of the code of the program in language C ++: for (int i = 0; i

Author: «MirrorInfo» Dream Team


Print