Definition and initialization of one-dimensional array
Array definition:
data type array name [array length];
The length must be an integer constant greater than 0 (C99 supports variables, but the VS series compilers do not support them), such as 10, 4+6.
int main()
{
int arr[10]; //未初始化,其值为随机值
int brr[10] = {
1,2,3,4,5,6,7,8,9,10};//刚刚好
int crr[] = {
1,2,3,4,5,6,7,8,9,10}; //crr 长度为 10,常用
int drr[10] = {
1,2,3,4,5}; //只初始化一部分则剩余部分为 0
//int err[5] = {12,3,4,5,6,7,8,9,10};//错误,数组长度不够,数据太多
return 0;
}
If only a part of the array is initialized, the remaining part is 0. Use of
a one-dimensional array
If you need to change the data of the red grid to 100, write it as brr[3] = 100;
the subscript can be an integer variable
int arr[10]; //数组定义时,[ ]中必须是常量,表示长度
arr[0] = 100;//数组使用时,[ ]中可以使用常量和变量,表示下标
Tips: Variables and functions have data types in front of them when they are defined, and there are no data types when they are used. By this you can distinguish between definition and use.
Example: Loop through each element in the array through the subscript
//输出 brr 数组的内容
for(i=0;i<10;i++)
{
printf("%d ",brr[i]);
}
printf("\n");
Example: Reverse the brr array, that is, the first element is exchanged with the last element, and the second element is exchanged with the penultimate element
int main()
{
int brr[10] = {
1,2,3,4,5,6,7,8,9,10};
int tmp; //用于交换数据
int len = sizeof(brr)/sizeof(brr[0]);//求数组长度的标准公式
for(int i=0;i<len/2;i++) //只需要交换一半
{
tmp = brr[i];
brr[i] = brr[len-i-1];
brr[len-i-1] = tmp;
}
for(int i=0;i<len;i++)
{
printf("%d ",brr[i]);
}
return 0;
}
One-dimensional array array name arr indicates that the entire array has the following conditions:
1. In the same function that defines the array, find sizeof(arr), which means the number of bytes of the entire array;
2. In the same function that defines the array, find &arr +1 means adding the byte size of the entire array; in
other cases, the array name means the address of the first element of the array.
In the same function that defines the array, you can use sizeof(arr)/sizeof(arr[0]) to indicate the
length of the array.
Example: Find in half. Search for the key in the array from small to large, and return its subscript when found, and return -1 if it fails.
The form is such as: int BinSearch(int arr[],int len,int key);
algorithm description: each time the intermediate value of the ordered area to be checked is compared with the key, if it is equal, it will be found. If the key value is greater than the intermediate value, It means that the key is in the right half of the sequence, then continue to compare on the right, otherwise continue to search on the left
#include <stdio.h>
//在长度为 len 的升序数组 arr 中查找关键字 key
//如果找到返回下标,失败返回-1
int BinSearch(int arr[],int len,int key)
{
int low = 0; //起始下标
int high = len-1; //结束下标
int mid; //中间下标
while(low <= high)
{
mid = (low+high)/2;
if(key == arr[mid]) //查找成功
return mid;
else if(key > arr[mid]) //在 mid 右边查找
low = mid+1;
else //在 mid 左边查找
high = mid - 1;
}
return -1; //查找失败
}
int main()
{
int arr[] = {
1,2,3,4,5,6,7,8,9,10};
int index;
for(int i=0;i<12;i++)
{
index = BinSearch(arr,sizeof(arr)/sizeof(arr[0]),i);
if(index < 0)
printf("%d 不在数组中\n",i);
else
printf("%d 在数组的%d 号下标\n",i,index);
}
return 0;
}
Due to its superior time complexity O(logn), binary search is very good in the search field
Other types of one-dimensional arrays
#include<stdio.h>
int main()
{
char arr[] = {
'a','b','c','d','e'}; //长度为 5 个的字符数组
short brr[] = {
1,2,3,4,5}; //长度为 5 个的 short 数组
int crr[] = {
10,20,30,40,50}; //长度为 5 个的 int 数组
float drr[4] = {
12.5,23.5,45.0}; //长度为 4 个的 float 数组
double err[] = {
12.3,45.6,67.8}; //长度为 3 个的 double 数组
int i;
for(i=0;i<sizeof(arr)/sizeof(arr[0]);i++)//输出 arr 的内容
{
printf("%c ",arr[i]);
}
printf("\n");
for(i=0;i<sizeof(brr)/sizeof(brr[0]);i++)//输出 brr 的内容
{
printf("%d ",brr[i]);
}
printf("\n");
for(i=0;i<sizeof(crr)/sizeof(crr[0]);i++)//输出 crr 的内容
{
printf("%d ",crr[i]);
}
printf("\n");
for(i=0;i<sizeof(drr)/sizeof(drr[0]);i++)//输出 drr 的内容
{
printf("%f ",drr[i]);
}
printf("\n");
for(i=0;i<sizeof(err)/sizeof(err[0]);i++)//输出 err 的内容
{
printf("%f ",err[i]);
}
printf("\n");
return 0;
}
Definition and initialization of two-dimensional array
The elements of a two-dimensional array are also accessed through subscripts, and the subscripts start from 0. For example, to modify the value of the red grid to 100, it should be written as: crr[1][2] = 100; the
following code will output the entire crr content:
#include<stdio.h>
int main()
{
int crr[3][4] = {
{
1,2,3,4},{
5,6,7,8},{
9,10,11,12}};
for(int i=0;i<3;i++) //输出每一行的元素
{
for(int j=0;j<4;j++) //输出每一行中的每一列元素
{
printf("%-3d",crr[i][j]);//-3d 表示每个数字输出占 3 格,左对齐
}
printf("\n");
}
return 0;
}
A two-dimensional array can be regarded as a special one-dimensional array: its elements are also a one-
dimensional array.
Example of a two-dimensional array : There is a 3×4 matrix, and the program is required to find the one with the largest value. The value of the element, and the row number and column number where it is located.
Analysis: Traverse the entire array, use one variable to store the maximum value, and the other two variables respectively store the row and column subscripts corresponding to the maximum value.
#include<stdio.h>
int main()
{
int arr[3][4] = {
1,2,3,4,5,6,70,18,9,10,11,12};
int max = arr[0][0]; //保存最大值
int row = 0; //保存最大值的行下标
int col = 0; //保存最大值的列下标
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
if(max < arr[i][j]) //max 已经不是最大值,需要更新
{
max = arr[i][j];
row = i;
col = j;
}
}
}
printf("最大值是%d,位于数组的第%d 行,第%d 列\n",max,row+1,col+1);
return 0;
}