Arrays in C #. Part 1. One-dimensional arrays. Examples of solving problems on one-dimensional arrays

Arrays in C#. Part 1. One-dimensional arrays. Examples of solving tasks with one-dimensional arrays


Content

1. What is an array in a programming language?

Array is an integrated group of variables of the same type that can be accessed by a single name. The use of arrays in the program allows you to conveniently organize the data sets and make easy access to this data with the name of the array and index.

 

2. What types of arrays can be represented in C#?

C# programming language allows you to represent two types of arrays:

  • one-dimensional arrays;
  • two-dimensional arrays.

In multidimensional arrays are different two-dimensional and multidimensional rectangular arrays. In addition, the programming language C # has the means to organize work with so-called step arrays.

 

3. What are the advantages of using arrays in programs?

The use of arrays in programs has such basic advantages:

  • convenience and ease of organization of the same type of data sets;
  • convenience of data processing (sorting, searching, performing calculations) using C# language cycle operators.

 

4. How is organized the representation of arrays in C#?

In C#, arrays are organized as objects. This means that for an array necessarily be allocated memory using operator new.

 

5. What is the general form of declaring a one-dimensional array?

The general form of declaring a one-dimensional array is as follows

type name_of_array = new type[size];

where

  • type – type of array items;
  • name_of_array – directly the name of the declared array;
  • size – number of items in the array. Numbering of array items starts from zero.

 

6. Examples of declaring one-dimensional arrays of different types and dimensions

Example 1. Declaring a one-dimensional array of 100 integers (int type).

// example of declaring an array of 100 integers - method 1
int[] A; // declare a variable of type "array of integers"
A = new int[100]; // allocate memory for variable A

Example 2. Declaration of a one-dimensional array of 50 real numbers (double type).

// declaration of an array of 50 real numbers - method 2
float[] B = new float[50];

 

7. How is access to the elements of the one-dimensional array? Example

Access to the elements of a one-dimensional array is done using an indexes. The index determines the position of the element in the array. The first array index is an index that has a value of 0. To access an array item using an index, you need to take the item index in square brackets.

Example 1. Access to the items of an array of integers (type long).

// declaration of an array of integers of type long
long[] M = new long[20]; // there are 20 items in array

// write arbitrary values to array items
M[2] = 23990; // in the item with index 2 save the value 23990
M[0] = 10000; // in the item with index 0 save the value 10000
M[19] = -2039;

// Error!
// M[20] = 1000; // Error! The index is out of range

Example 2. Declaring and filling with arbitrary values of an array of 10 float real numbers.

// declaration of an array of 10 real numbers
float[] B = new float[10];

// filling with arbitrary values
int i;
for (i = 0; i < 10; i++)
    B[i] = i + 2 * i; // B[i] - element in position i of array B

// outputting array items to a ListBox control
listBox1.Items.Clear();
for (i = 0; i < 10; i++)
    listBox1.Items.Add(B[i].ToString());

 

8. Example of zeroing a one-dimensional array of 100 real numbers
// zeroing a one-dimensional array of 100 floating point numbers
// declare an array
double[] D = new double[100];

// zeroing an array
for (int i = 0; i < D.Length; i++) // D.Length - number of items in the array
D[i] = 0;

 

9. Example of searching for a given value in a one-dimensional array
// searching for a given item in a one-dimensional array
int[] A = new int[10];
bool f_is; // result: if f_is = true, then the value is present in the array

// Array filling with arbitrary values
for (int i = 0; i < 10; i++)
    A[i] = i + i * i;

int num;
num = 42;

// search
f_is = false;
for (int i = 0; i < 10; i++)
    if (num == A[i])
f_is = true;

 

10. Example of counting the number of occurrences of a given value in a one-dimensional array of integers

In the example, the number of occurrences of the specified value is counted (stored in the variable n) in the array M. The result is written to the variable k.

// counting the number of occurrences of a given element in a one-dimensional array
int[] M;
M = new int[20];

// Array filling with arbitrary values
for (int i = 0; i < 20; i++)
    M[i] = i * 2 - i * i;

int n; // given item
int k; // result - the number of occurrences

n = 0;
k = 0;
for (int i = 0; i < 20; i++)
    if (n == M[i])
        k++;
// k = 2

 

11. An example of sorting elements of a one-dimensional array by the “bubble” method
// sort by bubble method in ascending order
// declaration of an array of 10 real numbers
float[] A = new float[10];

// filling of array items with arbitrary values
// ...

// sorting
for (int i=0; i<9; i++)
    for (int j=i; j>=0; j--)
        if (A[j] > A[j + 1])
        {
            float t = A[j];
            A[j] = A[j + 1];
            A[j + 1] = t;
        }

 

12. What is the general form of initializing a one-dimensional array?

In C#, an array can be initialized with values when it is created (declaration). The general form of array initialization is:

type[] array_name = { value1, value2, ..., valueN };

where

  • type – type of array items;
  • array_name – directly the name of the array;
  • value1, value2, valueN – The values by which the array items are initialized in the order of the indexing. A value1 will be assigned to an array cell with index 0. A value2 will be assigned to an array cell with index 1. A valueN will be assigned to an array cell with index N-1.

In the case of initializing an array, it is no longer necessary to use the new operator. The system automatically allocates the necessary amount of memory for the array.

 

13. Example of initializing one-dimensional arrays when they are declared
// initializing an array consisting of 6 items of type uint
uint[] UI = { 5, 2, 100, 50, 35, 64 };

// initializing an array of 5 items of 'bool' type
bool[] B = { true, false, false, false, true };

// initializing an array of 10 float items
float[] F = { 0.2f, 1.03f, -3.2f, -4.3f, 2.88f, 0.001f, 1.1f, 2.34f, 0.2f, 0f };

// initializing an array of 5 items of type char
char[] C = { '0', 'A', ';', '\\', 'z' };

 

14. Example of declaring and using a one-dimensional array of structures

For more information on working with arrays of structures, see the topic:

Let the type of the BOOK structure, describing information about the book, be given

// structure describing the book
struct BOOK
{
    public string title; // book title
    public string author; // author name of the book
    public int year; // the year of publishing
    public float price; // price
}

Then the code that declares an array of structures of type BOOK will have approximately the following form

// declaration and use of a one-dimensional array of structures
BOOK[] B; // declaration of a variable of type "array of BOOK structures"
B = new BOOK[5]; // memory allocation for 5 structures of type BOOK

// filling with values of array B
B[0].title = "Beginning. Microsoft. Visual C# 2008.";
B[0].author = "Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D.Reid";
B[0].year = 2008;
B[0].price = 9.99f;

B[1].title = "Pro C# 2010 and the .Net Planform. Fifth edition";
B[1].author = "Andrew Troelsen";
B[1].year = 2010;
B[1].price = 9.99f;

 

15. What happens if the program does not adhere to the boundaries of the array?

The C# programming language strictly adheres to the valid array bounds that are specified when it is declared. If, when accessing array elements, the index value is outside the array, then the IndexOutOfRangeException exception occurs. In this case, the program will end prematurely.

 

16. Example of declaring and using a one-dimensional array of classes

Let there be given the description of class MyPoint which describes a point on a coordinate plane

// a class that defines a point in the coordinate plane
class MyPoint
{
    // coordinates of a point
    int x;
    int y;

    // properties
    public int X
    {
        get { return x; }
        set { x = value; }
    }

    public int Y
    {
        get { return y; }
        set { y = value; }
    }
}

Then, the declaration and use of an array of 5 objects of type MyPoint will have approximately the following form

// declaration of an array of 5 objects of the class type "MyPoint"
// allocating memory for an array
MyPoint[] MP = new MyPoint[5];

// allocate memory for any object - a must!
for (int i = 0; i < 5; i++)
    MP[i] = new MyPoint();

// using an array of objects in the program
// Array filling with arbitrary values
for (int i = 0; i < 5; i++)
{
    MP[i].X = i * 3;
    MP[i].Y = i * i - 2;
}

 


Related topics