Learning C ++-> one-dimensional array
1. One-dimensional array
An array is a set of similar data elements arranged in order. The purpose of an array can be understood as defining a large number of variables at a time. For example, we need to store 100 int integers, if we declare one by one, we need to define 100 int-type variables, for example:
int a1, b1, c1, d1, e1, f1, g1, ... ;
In this case, it is very complex and not practical. In this case, we need to use a one-dimensional array to define an array that can put down 100 int integers:
int a[100] ;
This is equivalent to completing the definition of 100 int variables, which is indeed very convenient. How can we use it? We can think that defining an array of int a [100] is equivalent to defining:
int a[0], a[1], a[2], a[3], ..., a[99] ;
In this case, you only need to regard a [0] And a [1] as common variable names. For example, you can assign values to the first number a [0] And a [1:
a[0] = 10 ; a[1] = 20 ;
Code Demonstration:
#include<iostream>using namespace std ;int main(){ int a[100] ; a[0] = 10 ; a[1] = 20 ; a[99] = 30 ; cout<< "a[0] = " << a[0] << endl ; cout<< "a[1] = " << a[1] << endl ; cout<< "a[99] = " << a[99] << endl ; return 0 ;}
The output is as follows:
a[0] = 10a[1] = 20a[99] = 30Process returned 0 (0x0) execution time : 0.047 sPress any key to continue.
It can be seen that an int a [100] is defined. From a [0] to a [99], it can be used as a common variable name, the number between brackets [] is called the subscript of an array. Note that the subscript range of the one-dimensional array defining a [100] is 0-99, because the subscript starts from 0, not from 1, and starts from 0 to 99 is exactly the number of one hundred. That is to say, if you define a one-dimensional array a [100] with a size of 100, the range of subscripts that can be used is 0-99. If the subscripts are not in the range of 0-99, unexpected errors may occur, which are called subscripts out of bounds, when the subscript is out of bounds, the compiler will not report an error, but a serious error will occur during the running, so it is necessary to avoid the occurrence of this error.
1>. Definition of one-dimensional array:
In the above example, an array of the int type has been defined. The array type can not only be of the int type, it can also be basic data types such as unsigned int, long int, float, and double, or custom data types we will learn later, once the data type of the array is declared, all elements in the array are the data type at the time of declaration, for example:
Int a [100]; // float B [100] from a [0]-a [99]; // data from B [0]-B [99] is float type data double c [100]; // data from c [0]-c [99] is double type data
The general format of array definition:
Data Type array name [number of elements];
The number of elements is declared as follows:
①.The number of elements can be defined to directly specify an integer, such as int a [10], B [20]; the Values 10 and 20 here are the size (also called length) of the specified array. They can also contain operators to define the size of the array, such:
int a[10*6] ;
The length of the array is 60.
②.The number of elements can also be defined by character constants. See the following code:
#define N 10 int main() { int a[N] ; return 0 ; }
In the code above, we define a one-dimensional array a and specify its size (length) as 10. Note:# Define N 10In this line of code, the meaning of this sentence indicates that the character N is defined as a constant value 10. In the following code, where N appears, it means that it will be replaced by a value 10, so int a [N]; is equivalent to int a [10];.
③.Before introducing the number of elements declared in the array, you need to know that this definition method is not suitable for all compilers. This definition method is supported only by compilers that support the C99 standard, otherwise, an error will be reported during compilation. This declaration method is:
Use a variable with an integer value to declare the size of an array, for example:
Int n = 100; float a [n]; // defines a float array with a size of 100.
2>. initialize a one-dimensional array:
Array initialization refers to array initialization when defining an array and assigning values to elements in the array.
Initialization example:
int a[5] = { 1, 2, 3, 4, 5 } ;
In this way, the array is initialized during definition. The value in a [0] is 1, and the value in a [1] is 2 ,... the value in a [4] is 5. We declare an array a with a length of 5 and provide five initial values in braces separated by commas, in this way, all elements in the array are initialized.
You can also initialize some elements in an array:
int a[5] = { 1, 2, 3 } ;
In this way, only the first three elements of the array are initialized, a [0], a [1], and a [2]. The uninitialized parts are filled with 0 by default, in this case, the values in a [3] and a [4] are 0.
The general format of array initialization:
①. Form 1:
Data Type array name [number of elements] = {value 1, value 2, value 3,..., value n };
For this form, the default value 0 is assigned to the uninitialized part.
②. Form 2:
Data Type array name [] = {value 1, value 2, value 3,..., value n };
The statement of the number of elements in the array is omitted in this form. The Compiler automatically calculates the length of the array based on the number of initial values. For example:
int a[] = { 1, 2, 3, 4, 5 } ;
It is equivalent to declaring an array a with a length of 5 and initialization. We can see that this initialization method can also be used to declare the number of array elements.
Note that if you do not initialize an array and define only one array, the values of these elements in the array are uncertain and are random values in the system.
3>. Use of one-dimensional arrays:
When using an array, you must specify the value of the lower mark. The common usage of one-dimensional arrays is as follows:
Array name [subscript]
For example:
Int a [10] ={}; // define and initialize all of them to 0 int I = 3; // define a common integer variable with an initial value of 3 a [1] = 10; // assign the array a [1] to 10 a [1 + 1] = 20; // assign the array a [2] to 20 a [I] = 30; // assign array a [3] to 30
The subscript must be an integer, which can be a constant, variable, or expression. For example, a [1], a [I], and a [I + 1] are valid.
Because the compiler does not check the subscripts out of bounds, you must pay attention to the subscripts out of bounds.
4>. Example of using a one-dimensional array:
Enter 5 numbers and output them in reverse order
Input: 15 13 11 8 26
Output: 26 8 11 13 15
The numbers are separated by spaces during input.
Code:
# Include <iostream> using namespace std; int main () {int a [5]; // defines an array int I with 5 elements; // used to control the for loop cout <"Please input 5 numbers:"; // The system prompts you to enter 5 numbers for (I = 0; I <5; I ++) // enter cin> a [I]; for (I = 4; I> = 0; I --) // control the output of cout from 4 to 0 in reverse order by subscript <a [I] <""; return 0 ;}
Ii. Further Understanding
1>. space occupied by arrays in memory
We already know that an int-type element occupies 4 bytes of memory (taking the newer GCC compiler as an example), if you define an int a [10]; how much memory will this array occupy?
The sizeof operator can be used for actual measurement,
#include<iostream>using namespace std ;int main(){ int a[10] ; cout<<sizeof(a)<<endl ; return 0 ;}
The output value is 40, which indicates 40 bytes. An int type is 4 bytes, the array size is 10, and 4*10 is exactly 40, if you are not sure, you can also measure the size of the storage units occupied by them from a [0] to a [9], and then add them together. The Code will not be pasted here.
Summary:
Memory unit size occupied by the array = size of the array data type * Number of Elements
For example, double B [10]; The memory size is a double data size. 8x10 elements, that is, 80 bytes.
2>. Storage status of one-dimensional arrays in memory
A one-dimensional array is actually a continuous memory unit in the memory. When an array is defined, a continuous memory unit is opened up to store the values of elements in the array, if we define an array of int a [10], then its memory state can be imagined:
We can see that each element in the array actually has its own address, and the addresses corresponding to these elements in the figure are all assumed for convenient representation, the address used by the array in actual operation depends on the memory status of the current system. Therefore, you can run the same program in real time, the memory unit addresses used by each running hours group are also very likely to be different.
We can also see that the memory addresses from a [0] to a [9] are not 1000, 1001, 1002... in this continuous manner, the sorting is 1000, 1004, 1008... in this way, there is one number every four. The reason is that an int type is 4 bytes, and the size of a storage unit is one byte. Therefore, an int type data requires 4 storage units, that is, four bytes. Here, an element is represented in a square to be intuitively expressed.
In the memory storage status, we only need to first have a relevant impression, a more detailed introduction will be carried out in the future.
--------------------
Wid, 2012.12.03
Previous Article: learning C ++> circular statements and circular Control