C#. Solving tasks for one-dimensional arrays. Combining the loop operator with the condition operator




C#. Solving tasks for one-dimensional arrays. Combining the loop operator with the condition operator


Contents


Search other websites:

1. Calculation of the sum of positive elements of an array of n integers

An array A of 10 integers is given. Calculate the sum of the positive elements of the array

1.1. Implementation using a for loop

In this example, the input of array A is omitted

// Calculating the sum of positive items of an array
const int MaxN = 10;
int[] A = new int[MaxN]; // specified array
int i;

// input of array A
// ...

// calculating the sum
int sum = 0;

for (i=0; i<MaxN; i++)
    if (A[i]>0)
        sum = sum + A[i];
1.2. Implementation using a while loop
// Calculating the sum of positive items of an array
const int MaxN = 10;
int[] A = new int[MaxN]; // specified array
int i; // additional variable
int sum = 0; // the result

// input of array A
// ...

// calculating the sum
i=0;
while (i<MaxN)
{
    if (A[i]>0)
        sum+=A[i];
    i++;
}
1.3. Implementation using a do…while loop
// Calculating the sum of positive items of an array
const int MaxN = 10;
int[] A = new int[MaxN]; // specified array
int i; // additional variable
int sum = 0; // result

// input of array A
// ...

// calculating the sum
i=0;

do
{
    if (A[i]>0)
        sum+=A[i];
    i++;
}
while (i<MaxN);

 

2. Calculate the sum of array items that are placed on paired indexes

In this example, the sums of the array A items are calculated, the indices of which are paired: 0, 2, 4, … To determine if a number (array index) is paired, you need to check

if ((i%2)==0)
{
    // actions to be performed if the number is paired
    // ...
}


Implementation of the solution of this task by three types of cycle (input of array A is omitted).

// Calculating the sum of array items that have paired indices (0, 2, 4, ...)
const int MaxN = 10;
int[] A = new int[MaxN]; // specified array
int i; // additional variable
int sum1, sum2, sum3; // results of calculations by different types of cycles

// input of array A
// ...

// calculating the sum, the for loop
sum1 = 0;
for (i=0; i<MaxN; i++)
    if ((i%2)==0) // Determining whether a even number
        sum1+=A[i];

// calculating the sum, the while loop
sum2 = 0;
i=0;
while (i<MaxN)
{
    if ((i%2)==0) sum2+=A[i];
    i++;
}

// calculating the sum, the do...while loop
sum3 = 0;
i=0;
do
{
    if ((i%2)==0)
        sum3+=A[i];
    i++;
}
while (i<MaxN);

 

3. Calculate the product of array items that are greater than a given number

In the example is implemented a multiplication of elements of array A that are larger than the number (variable number). Implementing a task using the for loop:

// the multiplication of array elements that are greater than a given number
const int MaxN = 10;
int[] A = new int[MaxN]; // specified array
int number; // specified number
int i; // additional variable
int res; // result - multiplication

// input of array A
for (i=0; i<MaxN; i++)
{
    A[i] = i;
}

// set number value
number = 5;

// multiplication calculation - for loop
res = 1;
for (i=0; i<MaxN; i++)
    if (A[i]>number)
        res = res * A[i];

// res = 3024

If the dimension of the array is large, then the result of the multiplication should be kept in a variable of type double (float). This is due to the fact that the result of multiplication can be very large or very small numbers. Overflow can occur when using integer types.

A snippet of the implementation of this task using the while loop

...

// multiplication calculation - while loop
res = 1;
i=0;
while (i<MaxN)
{
    if (A[i]>number)
        res = res * A[i];
    i++;
}
// res = 3024

Implementation using a loop do…while

// multiplication calculation - do...while loop
res = 1;
i=0;

do
{
    if (A[i]>number)
        res = res * A[i];
    i++;
}
while (i<MaxN);

// res = 3024

 

4. Determining the presence (absence) of a given symbol in a character array (char type)

Let the array M of symbols is given. It is needed to determine whether a given symbol sym is in the array M. In this example, the step of input the array M and the symbol sym is skipped. Implementation of the algorithm using the for loop.

char[] M = new char[10]; // a given array
char sym; // a given symbol
int i;
bool f;

// input of array M
// ...

// calculation
f = false;
for (i = 0; i < 10; i++)
    if (M[i] == sym)
        f = true;

 

5. Determining the presence (absence) of a given number in an array of numbers. The array has n integers

The array A of integers is specified. Determine if a given number num is in array A. Implementation using the while loop (entering of array A and variables n, num is omitted).

// determining the presence (absence) of a given number in an array of numbers
int[] A = new int[50]; // predetermined array A
int n; // the current dimension of the array is n = 1..50
int num; // required number
int i; // additional variable
bool f; // result: f = true - the number is presented in the array, otherwise f = false

// input A, n, num
// ...

// calculation
f = false;
i=0;

while (i<n)
{
    if (A[i] == num)
        f = true;
    i++;
}

 

6. Finding the position of the last occurrence of the item k in the array of n integers

Method 1 (slow).

In this algorithm, the resulting variable pos determines the position of the last occurrence of the item k in an array of n integers. If there is no such symbol in the array, then pos = -1. Implementation using a ‘do…while’ loop.

// searching for the position of the last occurrence of the item k in an array of n integers
int[] A = new int[50]; // predetermined array A
int n; // the current dimension of the array is n = 1..50
int k; // required number
int i; // additional variable
int pos; // the result is the position number, if pos = -1, the number k is not found in array A

// input A, n, k
// ...

// calculation, implementation using the do..while loop

pos = -1;
i=0;

do
{
    if (A[i] == k)
        pos = i;
    i++;
}
while (i<n);

Method 2 (fast).

If you browse the indices of the array from the end to the beginning, then the first element is equal to k is the position of the last occurrence. In this case, the implementation of the algorithm (the do..while loop) will be as follows

// searching for the position of the last occurrence of the item k in an array of n integers
int[] A = new int[50]; // predetermined array A
int n; // the current dimension of the array is n = 1..50
int k; // required number
int i; // additional variable
int pos; // the result is the position number, if pos = -1, the number k is not found in array A

// input A, n, k
// ...

// calculation, implementation using the do ... while loop

pos = -1;
i=n-1;

do
{
    if (A[i] == k)
    {
        pos = i;
        i=-1; // exit from loop
    }
    else
        i--;
}
while (i>=0);

 

7. Searching for the position of the first occurrence of the item k in an array of n integers

As was shown in the previous paragraph, this problem can be solved in different ways. In the following code, the array is reviewed from the beginning to the end. As soon as the item k take place, the position of the element k is stored and the output from the loop is occurred.

Implementation of the task using the for loop.

// // searching for the posiiton of the first occurence of the item k in an array of n integers
int[] A = new int[50]; // given array A
int n; // the current dimension of the array is n = 1..50
int k; // required number
int i; // additional variable
int pos; // the result is the position number, if pos = -1, the number k is not found in array A

// input A, n, k
// ...

// calculation, implementation with a for loop
pos = -1;
for (i=0; i<n; i++)
    if (A[i]==k)
    {
        pos = i;
        break; // exit from loop
    }

 

8. Counting the number of elements of k in an array of n integers

The following code omits the input of array A and the values of n, k.

// Counting the number of elements of k in array A
int[] A = new int[50]; // given array A
int n; // the current dimension of the array is n = 1..50
int k; // required number
int i; // additional variable
int count; // result - the number of found items

// input A, n, k
// ...

// calculation using 'while' loop
count=0;
i=0;
while (i<n)
    if (A[i++]==k)
        count++;

 

9. Counting the number of items in an array of n real numbers that are less than the specified value of k

Implementation using a loop do..while

// counting the number of items that are less than the given k
int[] A = new int[50]; // given array A
int n; // the current dimension of the array is n = 1..50
int k; // value that is compared
int i; // additional variable
int count; // result

// input A, n, k
// ...

// calculation, implementation using the while loop
count=0;
i=0;

do
{
    if (A[i]<k)
        count++;
    i++;
}
while (i<n);

 

10. Counting the number of elements in an array of n real numbers whose value is in the range [a .. b]
// Counting the number of elements that are in the specified range
double[] M = new double[50]; // specified array M
int n; // the current dimension of the array is n = 1..50
double a, b; // compared values
int i; // additional variable
int count; // result

// input A, n, a, b
// ...

// calculation
count=0;
for (i=0; i<n; i++)
    if ((M[i]>=a)&&(M[i]<=b))
        count++;

 

11. Counting the number of pair items in an array of n integers
// Counting the number of pair items in an array of n integers
int[] A = new int[50]; // a given array A
int n; // the current dimension of the array is n = 1..10
int i; // additional variable
int count; // result - the number of paired elements

// input A, n
// ...

// calculation using 'for' loop
count=0;
for (i=0; i<n; i++)
    if ((A[i]%2)==0)
        count++;

 


Related topics