Use of one-dimensional arrays, two-dimensional arrays and multidimensional arrays

ctry L : clear screen

Three commands to enter folder directory: cd ~ cd home/hq/ cd

cat test.c without line number nl test.c with line number

An array is a collection of several variables with a certain order relationship, and each variable that makes up the array is called an element of the array.

The data type requirements of each element in the array are the same, determined by the array name and subscript.

1. One-dimensional array

1. Definition and use

One-dimensional array: <storage type> <data type> <array name> [<expression>];

#include<stdio.h>
int main(){
int a[6],i;
for (i=0;i<6;i++)    //只能逐个引用数组元素,不能一次性引用整个数组;
printf("%p",&a[i]); //打印数组各元素的地址
printf("%p",a);     //打印数组的起始地址,打印数组用%p
printf("%d\n",sizeof(a));     //打印数组占用的总空间
return 0;
}
int a[5]={1,2,3,4,5};    int a[]={1,2,3,4,5};
//数组不初始化,其元素值为随机值
//对static数组元素不赋初值,数组会自动赋以0值
//只给部分数组元素赋初值,剩下的默认为0;

2. Exercise: Bubble sort

#include<stdio.h>
int main(){
int a[]={1,3,4,2,45,9};      //冒泡排序
int t;
n=sizeof(a)/sizeof(int);
for (i=0;i<n-1;i++) {
   for(j=0;j<n-i-1;j++){
          if(a[j]>a[j+1]){
              t=a[j+1];
              A[j+1]=a[j];
              a[j]=t;
}
}
} 
for (i=0;i<n;i++)     
printf("%d\n",a[i]);  
puts("");
return 0;
}

Two, two-dimensional array

Definition method: array type array name [constant expression] [constant expression]

The number of rows can be omitted when declaring, but the number of columns cannot be omitted

The storage order of array elements is stored on the principle of row order first, because the memory is one-dimensional.
a[0] a[0][0] a[0][1] a[0][2]
a[1] a[1][0] a[1][1] a[1][2]
a[2] a[2][0] a[2][1] a[2][2]

Equivalent to multiple one-dimensional arrays

#include<stdio.h>
int main(){
int a[7][5];
int i,j;
for (i=0;i<7;i++) {   //只能逐个引用数组元素,不能一次性引用整个数组;
   for (j=0;i<5;i++)
   putchar('/n');
}
printf("%p",&a[i][j]); //打印数组各元素的地址
printf("%p",a);     //打印数组的起始地址
printf("%d\n",sizeof(a));     //打印数组的总长度
return 0;
}

Initialization of two-dimensional array elements:

int a[2]3]={ {1,2,3},{4,5,6}};

Partial initialization, other fill 0;

#include<stdio.h>
int main(){
int a[2][3]={
    
    {},{2}};
int i,j;
for (i=0;i<2;i++) {  
   for (j=0;i<3;i++)
   printf("%d",a[i][j]);
   putchar('/n');
}

return 0;
}

Three, multidimensional array

An array with two or more subscripts is called a multidimensional array.

int main(int argc,const char *argv[])    //杨辉三角
int a[10][10]={
    
    {0}};    //定义十行十列数组,并全部置为0
int i,j;
for(i=0;i<10;i++ ){
   a[i][0]=1;           //第一列全部置为1
   for(j=1;j<=i;j++)
      a[i][j]=a[i-1][j-1]+a[i-1][j];  //第i行i列的值置为第i-1行,j-1列和i-1行,j列的和
      }
for(i=0;i<10;i++ ){
    for(j=0; j<=i;j++)
      printf(”%8d" ,a[i][j]);
      printf("\n");
return 0:
#include <stdio.h>
int main(int argc, const char *argv[ ] )     //求数组中的最大值
int a[3][3]={
    
    {1,2,3},{4,5,6},{7,8,9}};
inti,j,row,cloumn;
roW= cloumn=0 ;
for(i=0;i<3;i++){
for(j=0;j<3; j++ ){
if(a[row][clounn]<a[i][j]){
row=i ;
cloumn=j;
}
}
}
printf("max-%d 最大值所在行为%d行%d列\n",a[ row][clounn], row,cloumn);
return 0;
}

Guess you like

Origin blog.csdn.net/qq_52049228/article/details/129619943