The so-called array: is a collection, which store the same types of data elements.
Features : Data stored inside the same data type; array is composed of consecutive memory locations.
1. The one-dimensional array defined
Three ways:
- Array type array name [array length];
- Array type array name [array size] = {value 1, value 2, ...}
- Array type array name [] = {value 1, value 2, ...}
int a[3]; a[0] = 1; a[1] = 2; a[2] = 3; int b[] = { 1,2,3 }; int c[4] = { 1,2,3,4 };
Check the address and the address of the array of elements in the array:
#include <the iostream> the using namespace STD; int main () { int c [ . 4 ] = { . 1 , 2 , . 3 , . 4 }; COUT << " array c address: " << (int) c << endl ; for ( int I = 0 ; I < . 3 ; I ++ ) { COUT << " array " << C [I] << " address is " << ( int ) & C [I] << endl; } system("pause"); return 0; }
Output:
Did not get the function array length in c ++, you need to define your own:
int length = sizeof(arr)/sizeof(arr[0]);
Bubble sort an array of applications:
#include <iostream> using namespace std; int main() { int c[] = {4,2,8,0,5,7,3,1,9}; int length = sizeof(c) / sizeof(c[0]); for (int i = length-1; i>=0; i--) { for (int j = i - 1; j >= 0; j--) { if (c[i] < c[j]) { int tmp = c[i]; c[i] = c[j]; c[j] = tmp; } } } for (int i = 0; i < length; i++) { cout << c[i]; } cout << "\n"; system("pause"); return 0; }
Output:
2. Definition of two-dimensional array
Four Definitions way:
- Data type array name [row number] [column number]
- Data type array name [line number] [column number] = {{data 1, data 2}, {data 3, data 4}}
- Data type array name [line number] [column number] data = {1, data 2, data 3, data 4}
- Data type array name [] [column number] data = {1, data 2, data 3, data 4}
you're a [ 2 ] [ 3 ]; you b [ 2 ] [ 3 ] = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }}; you c [ 2 ] [ 3 ] = { 1 , 2 , 3 , 4 , 5 , 6 }; you d [] [ 3 ] = { 1 , 2 , 3 , 4 , 5 , 6}; // int D [2] [] = {1,2,3,4,5,6}; illegal
The second is more intuitive to use.