User: alciro    User
 Original    Translate to:  Deutsch  English  Français  中文  
 

Programación en C++ Builder

 Arrays (arrays)
 Pointers
3. Example of class in c + +
8. AnsiString class methods
 C + + projects
 Packages, distribute an application without installation
 Exchange or bubble sorting
 String string.h functions

Arrays (arrays)

An array or array is a collection of elements dice of the same type are identified by an index. the elements starting with index o. arrays can declare any data type in C

Declaration of arrays

NombreTipo NombreVariable [No. of elements];

a one-dimensional array is often called a vector. < br / > examples of one-dimensional arrays or vector:

 list int [5]; / / array of five integers char names [10]; / / array of 10 characters

memory allocation

the memory allocated for an array is determined by the type of data and the number of elements in the array.

< p style = "padding-left: 30px;""> < Em > Bytes = Bytes (type) x No." elements < br / >

to the previous example (list int [5];) number of elements e:

ele

INT (4bytes) x 5 = 20 Bytes

utilization < /strong >

for working with arrays have to apply the same concepts to the rest of variables.

 list [0] = 300; / / assignment de a constant to an element of the vector list [1] = 50; list [2] = list [0] + list [1]; / / sum of elements, the result is saved in another element of the array

Declaracion and asignacion

 int vector1 [] = {- 200, - 100, 0, 100, 200};	/ / Array vector1 of integer type with 5 items int a [3] = {5, 10, 15};				/ / Array of integer type with 5 items char cad [5] = {'a', 'b', 'c', ', 'e'}; / / array with 5 items whole char vector1

the following three definitions are equivalente:

 char [5] greeting = "Hello"; char [] greeting = "Hello";
char greeting [5] = {'h', ' o', 'l', 'a', '\0'};

when a statement is made and simultaneous allocation dimension can be omitted since the compiler can determine it automatically based on the number of items, whether the dimension has to match the number of elements in the Declaration.

<br / > Arrays multimensionales

NombreTipo NombreVariable [n1] [n2]... [nx]; < /em >

are those arrays of more than one dimension. an array can have multiple dimensions, but an array with more than 2 or 3 dimensions is not operational. A two-dimensional matrix is often called table.

 int table [3] [5]; / / Array of 15 elements (60 bytes) int ArrayMulti [4] [10] [3]; / / Array of integers 4 x 10 x 3 float wood [2] [3]; / / Array of 2 X 3 = 6 real elements

Utilizacion

 table [0] [0] = 100; / / assignment de is a constant element [0] [0] x = table [0] [0] + [0] table [1]; / / sum of elements, the result is stored in the variable x

statement and asignacion

 int table [2] [3] = {{1, 2, 3}, {3, 4, 5}}; / / Matrix table of two dimensions of type integer with 6 elements

< br / > considerations

 int t:Microsoft.WindowsMobile.DirectX.Vector2 [5]; / / Declaration of a vector of 5 of 5 items... t:Microsoft.WindowsMobile.DirectX.Vector2 [5] = 10; / / assignment tol item 6 (t:Microsoft.WindowsMobile.DirectX.Vector2 [5])

• in the previous case an failed to write in an element that does not exist, occurs when accessing the t:Microsoft.WindowsMobile.DirectX.Vector2 with index 5 (item 6) nonexistent. as a result of this action is about writes in an indeterminate place that is not reserved for the variable in question, This can produce errors indeterminate in the functioning of the program. even if the compiler does not report the error since the syntax is correct, it is causing an error that can have serious consequences.

• the first element of an array has the index 0 and the last element dimension-1.

• compiler has of always knowing the number of items to reserve memory necessary. cannot declare an array with a variable to indicate the dimension. in C++ in order to dimension an array at run time or having a dynamic array should be used access techniques dynamic to data.

< pre class ="sh_c" > int dimension = 10; int vertor3 [dimension]; / / Not allowed ERROR

• variables have the same treatment as the temporary variables, are created on the stack, so it is convenient to work with arrays of small dimensions. If it is necessary to work with large amounts of data and arrays are big has to work on the heap, using dynamic data access techniques, this way we can have arrays dimensionable.

arrays cannot be passed as parametRos functions, have to deal with pointers and passing parameters by reference.

• the functions may not return arrays.

assignment is not allowed (e)etween arrays. to assign one array to another, you must write code to perform the assignments element to element.

< br / > ejemplo:

1.-the natural way of working with arrays is to use variables in the indices. the following example creates a 10 element vector and fills them with numbers with management toascending from 5.

 int vector1 [10]; / / Declaration of an array of 10 elements int data = 5; / / Variable data with the initial value for(int n=0;) (n < 10; n ++) {/ / from the 1st iteration}index 0 to 9 vector1 [n] = data;	/ / Asignacio{n of the Datum to the element of the vector data += 1; / / increase data}

2.-fill a vector with ASCII lowercase letters sort Ascending with home in 'a'.

 char vector1 [5]; / / Declaration of a vector of 5 elements char letter = 'a';	/ / Variable data with the initial value for(int n=0;) n < 5; n ++) {/ / from the 1st iteration}index 0 through 4 letter += n; / / increase letter vector1 [n] =; / / Asignacion of the Datum to the element of the vector}

3.-make a copy of a vector of integer type in another by multiplying all elements by 5.

 int vector1 [10] = {2,15,30,45,56,-45,0,7,-23,150}; / / Vector of 10 elements with assignment int t:Microsoft.WindowsMobile.DirectX.Vector2 [10]; / / Declaration of an array of 10 elements for(int n=0; n<10; n++) {/ / iteration from index 0 to 9 t:Microsoft.WindowsMobile.DirectX.Vector2 [n] = vector1 [n] * 5; / / mapping of the data to the product of the vector1 t:Microsoft.WindowsMobile.DirectX.Vector2 element * 5}
Loading
copyright © 2007-2024  www.alciro.org  All rights reserved.         
Share |