Last Updated:

C++. Operations on arrays

Calculating the Sum of Array Elements

Given an array X consisting of n elements. Find the sum of the elements in this array. The process of accumulating the sum of the elements of an array is quite simple and practically no different from summing the values of some numeric sequence. The variable S is set to zero, and then the elements of array X are summed sequentially. Flowchart of the algorithm for calculating the amount in front of you:

The corresponding fragment of the program algorithm will have the form:


Find the maximum element in an array

Given an array X consisting of n elements. Find the maximum element of the array and the number at which it is stored in the array.

The algorithm for solving the problem is as follows. Let the variable named Max store the value of the maximum element, and the variable Nmax store its number. Suppose that the zero element of an array is the maximum and write it to the max variable, and in Nmax - its number (zero). Then all the elements starting from the first one are compared in the loop with the maximum. If the current element of the array is greater than the maximum, then write it to the Max variable, and to the Nmax variable - the current value of the index i.

All of the above can be represented by the following code snippet:

The algorithm for finding the minimum element in the array will differ only in that in the if construct the sign will change from < to >.

Sort elements in an array

Sorting is the process of arranging the elements in an array in ascending or descending order. There are a large number of sorting algorithms, but they are all based on three main ones:

  • sorting by exchange;
  • sorting by selection;
  • sort by insertion.

Let's imagine that we need to arrange the cards in the deck. To sort cards by exchange, you can lay out the cards on the table face up and swap those cards that are in the wrong order, doing so until the deck of cards becomes ordered.

For sorting by selecting from the cards laid out on the table, select the youngest (senior) card and hold it in your hands. then, from the remaining cards, the smallest (largest) is again selected and placed behind the card that was selected first. This process is repeated until the entire deck is in hand. Since each time the smallest (largest) card from the remaining cards on the table is selected, the cards will be sorted in ascending (descending) at the end of this process.

For sorting by insertion, two cards are taken from the deck and placed in the required order in relation to each other. Each subsequent card taken from the deck must be placed in an appropriate place in relation to the already ordered cards.

Bubble sorting

This method is the most popular. Perhaps this is due to the fact that the name (bubble method) is well remembered and the algorithm itself is quite simple. Here we use the method of exchange sorting, which we will consider in more detail.

Compare the zero element of the array with the first, if the zero is greater than the first, then we will swap them. The same steps are performed for the first and second, second and third, i-th and (i+1)-th, penultimate and last elements. As a result of these actions, the largest element will be in the (n-1) place. Now let's repeat this algorithm first, but we will not consider the last element. After this operation, the largest element of the remaining array will be in the (n-2) place. This should be repeated until we have ordered the entire array. Flowchart of the algorithm:

Note that the buffer variable b is used to rearrange the elements, which temporarily stores the value of the element to be replaced.

Also, I think it is worth demonstrating the text of the program that sorts the elements of the array in ascending order by the "bubble" method:

The result of the program:


To rearrange the elements in the array in descending order of their values, you must replace the > character with < when comparing.

Sort by selection

The idea behind the algorithm is as follows. In an array Y consisting of n elements, look for the largest element and swap it with the last element. We repeat the algorithm for finding the maximum element, but we do not consider the last (n-1) element, since it has already taken its position. We put the found maximum on the (n-2) position. The search operation described above is performed n-1 times until the elements in the array are completely ordered.

Part of the code of the program that sorts the array in ascending order by the selection method:


Sort by inserting

Insert sorting is that two elements of the array are ordered first. Then the third is inserted in the appropriate place in relation to the first two elements. The fourth element is placed in a list of already ordered three elements. This process is repeated until all the elements are ordered.

Remove an element from an array

Suppose you want to remove the m-th element from the array X, consisting of n elements. To do this, it is enough to write (m + 1) element in place of the element m, (m + 2) to the place (m + 1), etc., n-1 to the place (n-2) and in further work with this array use the n-1 element:


Fragment of a C++ program:

This concludes the topic about one-dimensional arrays, then we will analyze pointers and dynamic arrays, as for two-dimensional arrays, I will remind you once again that they will be discussed in detail in the lessons about matrices.