Vectors in C language

V

When we refer to the notion of a vector, we mean a linear and homogeneous collection of data. A vector is linear because data (elements) can be accessed uniquely through an index.
A vector is also homogeneous because all elements are of the same type. In C, the index is a positive integer, and the indexing is from 0.

The statement of a vector variable is as follows:
<element_type> <vector_name> [<size>];

It is noteworthy that the vector is a static structure: its size must be a compilation constant and can not be changed during program execution. Thus, the programmer has to estimate the maximum size for the vector, and this will be a limitation of the program.

Typically, symbolic constants (as in the last example) are used for these maximum dimensions so that they can be easily adjusted when needed. Also, in a statement, vector components can be initialized with constant values, and in this case the vector size may remain undefined, the compiler will determine it from the number of elements in the list.

In the particular case where we specify the size and only one element at initialization, the first element will be the one specified, and all other elements of the vector will be initialized to 0:
It is important to note that uninitialized elements can have some value. When allocating a vector, the compiler does not perform any initialization and provides no error message if an item is used before it is initialized.

A correct program will initialize, in any case, each element before using it. Elements are accessed by phrases of <vector_name> [<index>].

Although the way in which program errors are made may be surprising and unpredictable, the causes of these errors are quite common and can be grouped into several categories. Some of these are listed below:

1. Index out of bounds is a frequent error that can result in blocking the program or system and can be avoided by checking the range within the valid range.

2. Indicated misspellings (cross-talk index). There are many cases where a loop level is used, for example, vect [i], and on the nested level vect [j], when it was actually intended to use i.

Define the dimensions by constants and use them instead of explicitly typing the values ​​in the source code. This will avoid code inconsistencies if you later want to change the dimensions and forget to change them all over the code.

Check that the indices fall between the upper and lower edges of the valid range. This should generally be done if the data comes from an external source: read from keyboard or pasted as effective parameters for a function, for example.

Use comments to explain what different variables are. This will help you not to mess with indexes, for example, and others who use or expand your code.

When dealing with an unsorted vector (and not only in this case), the simplest approach to finding value is sequential searching. In other words, each value in the vector is compared with the quoted value. If the value has been found, the search may stop, it does not make sense to parachute the vector until it is explicitly required.

If the search vector is sorted, the more efficient algorithm to use in this case is a binary search. Suppose the vector is sorted upward, for downgraded vectors, the reasoning is similar.

The value searched, x compares with the value of the vector N/2 index, where N is the number of elements. If x is less than the vector value, it is searched for the first half of the vector and, if larger, for the second half. The same algorithm does the search in one of the two halves.

Conceptually, binary search is a recursive algorithm, but it can be implemented equally well in an iterative way, using the indexes corresponding to the pieces of the vector in which the search is made. These indexes change over the algorithm, in a loop, according to the comparisons made.
We prefer to calculate the mean of the [low, high] formula using the formula x = low + (high – low) /2 because the perfect analogue formula x = (low + high)/2 can give overflow for high and low values.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories