5.1 one dimensional array
-
One dimensional array
- Array is one of the construction data types.
- An array is a collection of several variables with a certain order. Each variable that constitutes an array is called an element of the array.
- The data type of each element in the array is required to be the same. It is determined by the array name and subscript. The array can be one-dimensional or multi-dimensional.
- In scientific computing, many important applications are based on arrays. Arrays are called matrices in science.
-
One dimensional array definition
-
The so-called one-dimensional array refers to an array with only one subscript.
-
Arrays are stored continuously in the memory of the computer.
-
In C language, the description form of one-dimensional array is as follows:
-
< storage type > < data type > < array name > [< expression >];
- Array name: Legal identifier
- []: array operator
- Expression: indicates the number of elements, and the subscript starts from 0
-
For example: int a[6];
- The array name represents the first address of the memory and is an address constant
- Allocate contiguous memory at compile time
Memory bytes = array dimension * sizeof (element data type)
-
be careful:
- c99 allows the use of variables to define array dimensions
int i=15; int data[i]; //Can define array dimensions with variables
- The C language does not check the array out of bounds. You should pay attention when using it
int data[5]; data[5]=10; // Out of range access may cause segment error, memory access error, and the program ends directly
-
-
Reference to one-dimensional array
- The array must be defined first and then used. Only the array elements can be referenced one by one, and the entire array cannot be referenced at one time.
- Array element representation: array name [subscript], subscript starts from 0.
- Where: subscript can be a constant or an integer expression.
- For example:
int a[10]; printf("%d",a); // Wrong usage for(j=0;j<10;j++) { printf("%d\t",a[j]); // Correct usage }
-
Initialization of one-dimensional array
- The array is uninitialized and its element value is a random number
- Only some array elements are given initial values, and the rest are 0
- For example: int a[5]={0,1,2}; The system automatically assigns an initial value of 0 to the last two elements.
- The static array element is not assigned an initial value, and the system will automatically assign a value of 0
- For example: static int a[5]; Equivalent to: a[0]=0; a[1]=0; a[2]=0; a[3]=0; a[4]=0.
- When all array elements are given initial values, the array length may not be specified
- For example: int a[5]={0,1,2,3,4}; At this time, 5 in [] may not be written.
- For example:
- Source file:
01-cbase\49-array.c
- Source code:
#include <stdio.h> int main(int argc, char const *argv[]) { int a1[5]; // Define an array, do not initialize it, and its value is a random value for (int i = 0; i < 5; i++) { printf("a1[%d]=%d\n", i, a1[i]); } /* a1[0]=8 a1[1]=0 a1[2]=56 a1[3]=0 a1[4]=1577936 */ printf("***************************\n"); int a2[5] = {1, 2, 3}; // Only some array elements are given initial values, and other elements are 0 for (int i = 0; i < 5; i++) { printf("a2[%d]=%d\n", i, a2[i]); } /* a2[0]=1 a2[1]=2 a2[2]=3 a2[3]=0 a2[4]=0 */ printf("***************************\n"); static int a3[5] ; // The static array element is not assigned an initial value, and the system will automatically assign a value of 0 for(int i=0;i<5;i++) { printf("a3[%d]=%d\n",i,a3[i]); } /* a3[0]=0 a3[1]=0 a3[2]=0 a3[3]=0 a3[4]=0 */ printf("***************************\n"); // When all array elements are given initial values, the array length can not be specified, and the length will be automatically determined according to the number of initializations int a4[] ={1,2,3,4,5} ; for(int i=0;i<5;i++) { printf("a4[%d]=%d\n",i,a4[i]); } /* a4[0]=1 a4[1]=2 a4[2]=3 a4[3]=4 a4[4]=5 */ return 0; }
- Operation results:
a1[0]=8 a1[1]=0 a1[2]=4199705 a1[3]=0 a1[4]=8 ********************************************* a2[0]=1 a2[1]=2 a2[2]=3 a2[3]=0 a2[4]=0 ********************************************* a3[0]=0 a3[1]=0 a3[2]=0 a3[3]=0 a3[4]=0 ********************************************* a4[0]=1 a4[1]=2 a4[2]=3 a4[3]=4 a4[4]=5
-
Example 51
Input 10 numbers (int type parameters) from the keyboard and store them in a one-dimensional array, and output them in reverse order.- Source file:
01-cbase\51-array.c
- source code
#include <stdio.h> int main(int argc, char const *argv[]) { int a[10] = {0} ; for(int i=0;i<10;i++) { printf("Please enter page%d Integer>:",i+1); scanf("%d",&a[i]) ; // Input of array } printf("The contents of the array are::"); for(int i=0;i<10;i++) { printf("%d ",a[i]); // Output of array } printf("\n"); for(int i=0,t=0;i<10/2;i++) { t = a[i]; a[i] = a[10-1-i]; a[10-1-i] = t; } printf("The contents of the array are::"); for(int i=0;i<10;i++) { printf("%d ",a[i]); } return 0; }
- Operation results
Please enter the first integer>:12 Please enter the second integer>:34 Please enter the 3rd integer>:9 Please enter the fourth integer>:23 Please enter the 5th integer>:27 Please enter the 6th integer>:87 Please enter the 7th integer>:23 Please enter the 8th integer>:76 Please enter the 9th integer>:2 Please enter the 10th integer>:876 The contents of the array are::12 34 9 23 27 87 23 76 2 876 The contents of the array are::876 2 76 23 87 27 23 9 34 12
-
Example 52
- Input 10 numbers (int type parameters) from the keyboard and store them in the one-dimensional array. Find the largest number of the 10 numbers and output it.
- Solution idea: you can compare the first and the second. If the first is greater than the second, exchange two elements, so that the second is the largest. Next, compare the second and the third, and so on to find the maximum value.
- source file
01-cbase\52-array.c
- source code
#include <stdio.h> int main(int argc, char const *argv[]) { int a[10]={0}; for(int i=0;i<10;i++) { printf("Please enter page%d number>:",i+1); scanf("%d",&a[i]); } printf("The contents of the array are:>:"); for(int i=0;i<10;i++) { printf("%d ",a[i]); } printf("\n"); // If the three numbers find the maximum value, it needs to be compared twice for(int i=0,t;i<10-1;i++) // The maximum value of 10 numbers needs to be compared 9 times { if(a[i] > a[i+1]) // If the i-th number is larger than the i+1-th number, it is swapped, and the rightmost is the maximum value { t = a[i]; a[i] = a[i+1]; a[i+1] = t; } } printf("max=%d\n",a[10-1]) ; // The rightmost is the maximum return 0; }
- Operation results
Please enter the first number>:12 Please enter the second number>:3 Please enter the third number>:24 Please enter the fourth number>:674 Please enter the 5th number>:123 Please enter the 6th number>:370 Please enter the 7th number>:862 Please enter the 8th number>:87 Please enter the 9th number>:20 Please enter the 10th number>:12 The contents of the array are:>:12 3 24 674 123 370 862 87 20 12 max=862