Arrays. Part 3. Examples of solving the most common tasks with one-dimensional arrays

Arrays. Part 3. Examples of solving the most common tasks with one-dimensional arrays

This topic is based on the topic: Array definition. One-dimensional arrays. Initializing array


Content
  1. Finding the sums and products of array elements. Examples
  2. Finding the maximum (minimum) element of the array. Examples
  3. Sorting an array using the “bubble” method
  4. Search for an element in an array. Examples

1. Finding the sums and products of array elements. Examples

Example 1.  An array A containing 100 integers is specified. Find the sum of the elements of this array. A snippet of code that solves this problem

// Sum of elements of array A of 100 integers
int A[100];
int suma; // Variable containing the sum
int i; // Additional variable

// input of array A
// ...

// Calculating the sum
suma = 0; // zeroing the sum
for (i=0; i<100; i++)
    suma += A[i];

The search for all elements of the array is performed in the for loop. The variable sum stores the resulting value of the sum of the elements in the array. The variable i is the counter that determines the index of the element of the array A[i].

Example 2. An array B containing 20 real numbers is given. Find the sum of the elements of the array that lie on paired positions. Assume that the positions 0, 2, 4 etc. are paired.

// The sum of the elements of array B
// lying on paired positions
float B[20];
float sum; // Variable containing the sum
int i; // additional variable

// input of array
// ...

// calculating the sum
sum = 0; // zeroing the sum
for (i=0; i<20; i++)
    if ((i%2)==0)
        sum += B[i];

In this example the expression

 (i%2)==0

determines the paired index of array B. If you want to take odd positions, you need to write

(i%2)==1

Example 3. An array is specified that contains 50 integers. Find the sum of the positive elements of the array.

// the sum of positive items of the array
int A[50];
int sum; // Variable containing the sum
int i; // additional variable

// input of array
// ...

// calculating the sum
sum = 0; // zeroing the sum
for (i=0; i<50; i++)
    if (A[i]>0)
        sum = sum + A[i];

Example 4. An array of 50 integers is specified. Find the product of array items that are odd numbers.

// the product of odd elements of an array
int A[50];
int d; // the variable containing the product
int i; // additional variable

// input of array
// ...

// calculation of multiplication
d = 1; // initial setting of variable d
for (i=0; i<50; i++)
    if ((A[i]%2)==1)
        d = d * A[i];

To determine whether an element of the array A[i] is odd, we need to check condition

 (A[i]%2)==1

If the condition is true, then the array element is an odd number.

 

2. Finding the maximum (minimum) element of the array. Examples

Example 1. An array of 30 real numbers is specified. Find the element (index) that has the maximum value in the array.

// search for a position (index) containing the maximum value
float B[30];
float max; // variable that contains the maximum
int index; // the position of the element containing the maximum value
int i; // additional variable

// input of array
// ...

// search for a maximum
// set the maximum as the 1st element of the array
index = 0;
max = B[0];
for (i=1; i<30; i++)
    if (max<B[i])
    {
        max = B[i]; // save the maximum
        index = i;  // save the position of the maximum element
    }

In the above example, the variable max contains the maximum value. The variable index contains the position of the element that has the maximum value. At the beginning of the variable max, the value of the first element of the array is assigned. Then, starting with the second element, the whole array passes through the for loop. At the same time, condition is checked

 if (max<B[i])

If the condition is true (another maximum is found), then the new maximum value is fixed in the variables max and index.
The above example finds only one maximum. However, there may be several maximum values in arrays. In this case, to save the positions (indexes) of the maximum values, you need to use an additional array as shown in the following example.

Example 2. An array containing 50 integers is given. Find the position (positions) of the element that takes the minimum value. If there are several such elements, create an additional array of indexes.

// search for items (indexes) that contain the minimum value
int A[50];
int min; // variable containing the minimum value
int INDEXES[50]; // indexes of elements containing the minimum value
int n; // number of identical minimum values
int i; // additional variable

// input of array
// ...

// 1. Finding the minimum value
// set the minimum value in the first element of the array
min = A[0];
for (i=1; i<50; i++)
    if (min>A[i])
        min = A[i]; // save the minimum value

// 2. Forming an array
n = 0; // zeroing the counter in the array INDEXES
for (i=0; i<50; i++)
    if (min == A[i])
    {
        n++; // Increase the number of elements in INDEXES array
        INDEXES[n-1] = i; // save the indexes
    }

listBox1->Items->Clear();

// 3. Displaying an INDEXES array in listBox1
for (i=0; i<n; i++)
    listBox1->Items->Add(INDEXES[i].ToString());

In the above listing, the minimum value min is first searched. In the second step, an INDEXES array is formed, in which the number of elements is written to the variable n. The minimum value in array A is searched for, while the INDEXES array is being created at the same time. The third step is an example of how to display an INDEXES array in the listBox1 (ListBox) control.

 

3. Sorting an array using the “bubble” method.

Example. Let the array A containing 10 integers be given. You need to sort the array elements in descending order using the “bubble” method.

// Array sorting by "bubble" method
int A[10];
int i, j; // additional variables are counters
int t; // additional variable

// input of array A
// ...

// sorting
for (i=0; i<9; i++)
    for (j=i; j>=0; j--)
        if (A[j]<A[j+1])
        {
            // swap A[j] and A[j+1]
            t = A[j];
            A[j] = A[j+1];
            A[j+1] = t;
        }
4.  Search for an element in an array. Examples

Example 1. Determine, whether the number k in the array M, which consists of 50 integers.

// Determining the presence of a given number in an array of numbers
int M[50];
int i;
int k; // the required value
bool f_is; // Search result, true - the number k is in the array, otherwise false

// input of array M
// ...
// input k
// ...

// searching for a number in an array
f_is = false;
for (i=0; i<50; i++)
    if (k==M[i])
    {
        f_is = true; // Number is found
        break; // exit from the loop, further search does not make sense
    }

// result output
if (f_is)
label1->Text = "Number " + k.ToString() + " is in array M.";
 else
label1->Text = "Number " + k.ToString() + " is not in the array M.";

Example 2. Find all the positions of the number k in the array M consisting of 50 integers.

// definition of all positions of a given number in an array of numbers
int M[50]; // array of numbers
int i; // additional variable
int k; // the desired value
int INDEXES[50]; // the required array of occurrences of the number k
int n; // the number of found positions or the number of elements in the array INDEXES

// input of array M
// ...
// input of number k
// ...

// searching for the number k in the array M and the simultaneous formation of the array INDEXES
n = 0;
for (i=0; i<50; i++)
    if (k==M[i])
    {
        // number is found
        n++;
        INDEXES[n-1] = i;
    }

// output the result in listBox1
listBox1->Items->Clear();

for (i=0; i<n; i++)
    listBox1->Items->Add(INDEXES[i].ToString());

 


Related topics: