Solving of tasks using loop operators for array processing in C++. Tasks using the operators of the for, while, do … while loop
Contents
- Solving of tasks
- Related topics
Search other websites:
1. Calculation the sum of array items from n real numbers.
1.1. Solving using the for loop
// sum of array items of real numbers const int MaxN = 100; int n; float A[MaxN]; float sum; // result - the sum of array items // set the value n n = 10; // filling the array A with arbitrary values for (int i=0; i<n; i++) A[i] = 0.1f * i; // first reset the value of the sum sum = 0; // sum computation loop for (int i=0; i<n; i++) sum = sum + A[i];
1.2. Solving using the while loop
// the sum of the items of the array of real numbers - the while loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // array float sum; // result - the sum of array elements int i; // additional variable // set the value of n n = 10; // filling the array A an arbitrary values using the while loop i=0; while (i<n) { A[i] = 0.1f*i; i++; } // first, reset the value of sum and i sum = 0; i = 0; // the 'while' loop to calculate the sum while (i<n) { sum += A[i]; i++; }
1.3. The solving of the task. The loop ‘do … while’
// the sum of the items of the array of real numbers - the do..while loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // array float sum; // result - the sum of array elements int i; // additional variable // set the value of n n = 10; // filling the array A with arbitrary values using the do..while loop i=0; do { A[i] = 0.1f*i; i++; } while (i<n); // first, reset the value of sum and i sum = 0; i = 0; // the 'while' loop to calculate the sum do { sum += A[i]; i++; } while (i<n);
2. Calculation the average of an array of n real numbers
To find the average of the items of the array, you first need to find the sum of the items of the array, and then divide this sum by the number of items in the array.
2.1. Solving. The ‘for’ loop
In the example, the input of array and the number of items of the array are omitted.
// the arithmetic average of the items of the array of real numbers - the for loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // array float avg; // the result is the arithmetic average int i; // additional variable // set the value n n = 10; // input of array A and number of array items n // ... // the sum is calculated in the variable avg avg = 0; // the 'for' loop to calculate the sum for (i=0; i<n; i++) { avg += A[i]; } // calculation of the arithmetic average avg = avg / n;
2.2. Solving. The ‘while’ loop
In the example, the input of array and the number of items of the array are omitted.
// the arithmetic average of the array items of real numbers - the while loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // array float avg; // the result is the arithmetic average int i; // additional variable // input of array A and number of array items n // ... // the sum is calculated in the variable avg avg = 0; i = 0; // the 'while' loop to calculate the sum while (i<n) { avg += A[i]; i++; } // calculation of the arithmetic average avg = avg / n;
2.3. Solving. The do…while loop
In the example, the input of array and the number of items of the array are omitted.
// the arithmetic average of the array items of real numbers - the do..while loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // array float avg; // the result is the arithmetic average int i; // additional variable // input of array A and number of array items n // ... // the sum is calculated in the variable avg avg = 0; i = 0; // the 'while' loop to calculate the sum do { avg += A[i]; i++; } while (i<n); // calculation of the arithmetic average avg = avg / n;
3. Item-by-item copying of arrays
3.1. The ‘for’ loop
In this example, a code snippet is provided that copies an array A of 10 floating-point numbers (float) to array B.
// item-by-item copying of arrays - the 'for' loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // source array float B[Max]; // destination array int i; // additional variable // input of array A and number of array items n // ... // the loop of copying A => B for (i=0; i<n; i++) { B[i] = A[i]; }
3.2. The while loop
The snippet of copying array A to array B using the while loop
// item-by-item copying of arrays - the 'while' loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items float A[Max]; // source array float B[Max]; // destination array int i; // additional variable // input of array A and number of array items n // ... // the loop of copying A => B i=0; while (i<n) { B[i] = A[i]; i++; }
3.3. The do…while loop
Implementation of copying of arrays using the do … while loop
// item-by-item copying of arrays - the 'do..while' loop const int Max = 100; // maximum number of items in the array int n; // n = 1..Max - specified number of array items double A[Max]; // source array double B[Max]; // destination array int i; // additional variable // input of array A and number of array items n // ... // the loop of copying A => B i=0; do { B[i] = A[i]; i++; } while (i<n);
4. The reversing of array
Suppose there are two arrays with the names A and B. Array A is specified. Get the resulting array B, the inverse of the original array A. In this example the implementation of reversing the array using three loop operators is given.
// Getting the Reverse Array const int Max = 100; // maximum number of items in the array int n; // n = 1..Max double A[Max]; // source array double B[Max]; // destination array int i; // additional variable // input of array A and number of array items n // ... // solving the task using the do ... while loop i=0; do { B[i] = A[n-i-1]; i++; } while (i<n); // solving the task using the for loop for (i=0; i<n; i++) B[i] = A[n-i-1]; // solving the task using the while loop i=0; while (i<n) { B[i] = A[n-i-1]; ++i; }
5. Array reverse without using an additional array
An array A with n real numbers is specified. Implement the operation of array reversal without using an additional array. In the code below, the array is reversed using the for, while, do … while loops. To solve the task, an additional variable t is added.
Let the following description of data types be given
// Reversing of the array A const int Max = 100; // maximum number of items in the array int n; // n = 1..Max double A[Max]; // array int i; // additional variable double t; // additional variable // input of array A and number of array items n // ...
Then the solution of the task using the ‘do…while’ loop is as follows
// solving the task using the do ... while loop i=0; do { t = A[i]; A[i] = A[n-i-1]; A[n-i-1] = t; i++; } while (i < (n/2));
Solving the task using the ‘for’ loop
for (i=0; i<n/2; i++) { t = A[i]; // using the additional variable t A[i] = A[n-i-1]; A[n-i-1] = t; }
Solving the task using the ‘while’ loop.
i=0;
while (i<n/2)
{
t = A[i];
A[i] = A[n-i-1];
A[n-i-1] = t;
i++;
}