[C language (self-study record) from 0] one dimensional array

catalogue

1, One dimensional array:

(1) Definition of one-dimensional array:

1. Format:

2. Note:

(II) reference of one-dimensional array elements:

1. Format:

2. Note:

(3) Initialization of one-dimensional array:

1. Format:

2. Note:

(4) Example:

1. Compare the size of array elements and output the maximum value:

2. Array inversion:

3. Output array elements in reverse order

4. Bubble sorting:

  • Array is a continuous variable space of the same type in memory. It is a set of ordered data.
  • An array element is actually a variable (variable with subscript). The elements in the array are uniquely determined by an array name and subscript.
  • ※ the array name is an address constant pointing to the first address of the array, and cannot be assigned.
  • Size of array in memory (sizeof (array name)) = size of array elements * number of array elements

1, One dimensional array:

(1) Definition of one-dimensional array:

1. Format:

Type name array name[Constant expression];

2. Note:

① The naming rules of array names are the same as variable names, and follow the naming rules of identifiers

② The value of a constant expression indicates the total number of elements in the array, that is, the length of the array or the size of the array

③ Expressions can contain constants and symbolic constants, but cannot contain variables (dynamic definition of array size is not allowed in C language)

For example:

int N=9;

int a[N]; Illegal. In this case, n is a variable.

and

#define N

  int a[N]; Correct, because n is a symbolic constant.

④ Arrays are contiguous when allocating space in memory

#include <stdio.h>
//Prints the memory address of the array element
int main()
{
	int a[5]={0,1,2,3,4};
	printf("%p\n", a[0]);
	printf("%p\n", a[1]);
	printf("%p\n", a[2]);
	printf("%p\n", a[3]);
	printf("%p\n", a[4]);
	return 0;
}

                      a[0]                  a[1]                 a[2]                a[3]                a[4]

01234

(II) reference of one-dimensional array elements:

1. Format:

Array name[subscript]

2. Note:

① Defined before reference

② Only array elements can be referenced, and the values of all elements of the entire array cannot be called at once

③ Subscripts can be expressions, variables

④ The subscript value of the array element is between 0 and n-1

(3) Initialization of one-dimensional array:

1. Format:

Arrays can only be initialized at the same time as they are defined

Type name array name[Constant expression]={Initial value 1,Initial value 2,......};

(1) initial values can be assigned to all array elements, such as:

int a[10]={0,1,2,3,4,5,6,7,8,9};

Many errors (no place to save)

(2) You can assign values to only some elements in the array, such as:

int a[10]={0,1,2,3,4};

Make up 0 where there is less, which is equivalent to:

int a[10]={0,1,2,3,4,0,0,0,0,0};

(3) If you want all the elements in an array to have values of 0, you can write:

int a[10]={0,0,0,0,0,0,0,0,0,0};
or
int a[10]={0};

But if it is written as: int a[10]={1}; Then only the value of a[0] is 1, and the values of a[1]~a[9] are 0

(4) When assigning initial values to all array elements, since the number of data has been determined, the array length can not be specified, such as:

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

It can be written as:

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

2. Note:

  • If the length of the array is specified and initialized when defining a numeric array, the system will automatically initialize the array elements that are not initialized by the initialization list to 0;
  • If it is a character array, it is initialized to '\ 0';
  • If it is a pointer array, it is initialized to NULL.
  • If not initialized, the array elements are random values.

(4) Example:

1. Compare the size of array elements and output the maximum value:

#include <stdio.h>
//Compare the size of array elements and output the maximum value (while loop)
int main()
{
	int i=0,max,a[5]={10,1,21,13,4};
	max = a[0];
	while (i < 5)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
		i++;
	}
	printf("The maximum value is:%d\n", max);
	return 0;
}

or

#include <stdio.h>
//Compare the size of array elements and output the maximum value (for loop)
int main()
{
	int i,max,a[5]={10,1,21,13,4};
	max = a[0];
	for(i=0;i<5;i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
		i++;
	}
	printf("The maximum value is:%d\n", max);
	return 0;
}

2. Array inversion:

#include <stdio.h>
//Invert array elements
int main()
{
	int a[10]={1,2,3,4,5,6,7,8,9,10};
	int i,j,temp;
	i = 0;
	j = sizeof(a) / sizeof(a[0]) - 1;
	while(i<j)
	{
		//Exchange data through temporary variables
		temp = a[i];
		a[i] = a[j];
		a[j] = temp;
		i++;
		j--;
	}
	for(i=0;i<10;i++)
	printf("%d\n", a[i]);
	return 0;
}

3. Output array elements in reverse order

#include <stdio.h>
//Output array elements in reverse order
int main()
{
	int a[10]={1,2,3,4,5,6,7,8,9,10};
	int i= sizeof(a) / sizeof(a[0]) - 1;
	for (i; i >= 0; i--)
	{
		printf("%d\n", a[i]);
	}
	return 0;
}

.

4. Bubble sorting:

#include <stdio.h>
//bubble sort 
int main()
{
	int a[10]={6,2,9,5,7,1,4,8,3,10};
	int i,j,temp;
    //Outer layer execution (number of elements - 1) times    
	for (i=0; i <10-1; i++)
	{
        //Inner layer execution (number of elements - 1 - outer layer execution times) times
		for (j = 0; j < 10-1-i; j++)
		{
			if (a[j] > a[j + 1])
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
	}
	for (i = 0; i < 10; i++)
	{
		printf("%d\n", a[i]);
	}
	return 0;
}

  

Keywords: C Back-end

Added by khanuja.sunpreet on Fri, 14 Jan 2022 17:54:25 +0200