# 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:

1 2 | for (S=0, i=0; i<N; S+=X[i], i++); cout<<"S="<<"\n"; |

## 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:

1 2 3 4 5 6 7 8 | for (Max=X[0], Nmax=0, i=0; i<n; i++) if (Max<X[i]) { Max=X[i]; Nmax=i; } cout<<"Max="<<Max<<"\n"; cout<<"Nmax="<<Nmax<<"\n"; |

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:*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include "stdafx.h" #include <iostream> using namespace std; int main() { int y[10], n, i, b, j; cout<<"\n N="; cin>>n; for (i=0; i<n; i++) //array output { cout<<"\nY["<<i<<"]="; cin>>y[i]; } //ordering the elements in the array in ascending order of their values for (j=1; j<n; j++) for (i=0; i<n-j; i++) if (y[i]>y[i+1]) //if the current element is greater than the next, then { b=y[i]; //save the value of the current element; y[i]=y[i+1]; //replace the current element with the next one; y[i+1]=b; //replace the next element with the current one. } for (i=0; i<n; i++) cout<<y[i]<<"\t"; //output ordered array system("pause"); return 0; } |

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:*

1 2 3 4 | for (j=1; j<n; b=y[n-j], y[n-j]=y[nom], y[nom]=b; j++) for (max=y[0], nom=0, i=1; i<=n-j; i++) if (y[i]>max) {max=y[i]; nom=i;} for (i=0; i<n; i++) cout<<y[i]<<"\t"; //output ordered array |

### 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:**

1 2 3 4 | cout<<"\n m="; cin>>m; //input the number of the element to be deleted for (i=m; i<n-1; X[i+1],i++); //delete the m-th element for (i=0; i<n-1; i++) cout<<X[i]<<"\t"; // output of the modified array n--; //reduce the number of elements in the array |

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*.