Chapter 6: Arrays in C

        In my life, there are many things, friends with obsessive-compulsive disorder, like to classify them into one place and save them, so that when you use them, you can find what you need in the form of classification, and The same is true for the C language. When there are multiple integer numbers, they can be put together and placed in one memory, and this space is called an array.

 One: one-dimensional array

 ① Define a one-dimensional array:

②Refer to one-dimensional array

 ③Example of one-dimensional array

1. Realize the input of the number from 1 to 10, and print the number from 1 to 10.

#include<stdio.h>
int main()
{
	int i = 0;
	int arr[10] = { 0 };//初始化数组,并且初始化为0
	printf("请输入1到10的数字:");
	for (i = 0; i < 10; i++)
	{
		scanf("%d", &arr[i]);
	}

	printf("打印1到10的数字:");
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

//这里我们要讲一下sizeof这个关键字。
//sizeof是计算某个类型的大小
//看看如何计算数组元素的个数

Calculation of the number of array elements:

int ret = sizeof(arr)/sizeof(arr[0]) sizeof(arr[0]) is the size of the first element, sizeof(arr) is the size of the array.

2. Implement bubble sorting (emphasis) 

int main()
{
	int i = 0;
	int j = 0;
	int arr[10] = { 0 };//初始化数组
	printf("请输入十个整数:\n");
	//实现十个数字的输入
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		scanf("%d", &arr[i]);
	}
	//实现排序(有小到大)
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		for (j = 0; j < sizeof(arr) / sizeof(arr[0]) - i - 1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
		}
	}
	//实现十个数字的有小到大的输出
	printf("排序后的十个数字为:\n");
	for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

One: two-dimensional array

 ①Define a two-dimensional array:

 ② Reference two-dimensional array

 

 The initialization is written like this int arr[3][3] = { {1,2,3} , {4,5,6} , {7,8,9} };

Note that the size of the column cannot be omitted, and the size of the row can be omitted, but the row and column can be omitted together.

③Example of two-dimensional array

Print Yang Hui triangle (emphasis)

#include<stdio.h>
int main()
{
	int arr[10][10] = { 0 };//初始化二维数组
	int a = 1;
	int b = 1;
	for (a = 0; a < 10; a++)
	{
		//每行第一个元素都为 1
		arr[a][0] = 1;
		for (b = 0; b <= a; b++)
		{
			if (a == b)
			{
				//每行第最后一个元素都为 1
				arr[a][b] = 1;
			}

			if (a > 1 && b >= 1)        
			{
				//算法
				arr[a][b] = arr[a - 1][b - 1] + arr[a - 1][b];
			}
			//打印每个元素
			printf("%d ", arr[a][b]);
		}
		//每打印一行换行
		printf("\n");
	}
	return 0;
}

Three: Multidimensional arrays (interested friends can do their own research)

Here are the topics about arrays (three chess implementation, minesweeping can see the blogger's blog)

        Before you know it, it has come to the end. This is the end of our knowledge points in Chapter 6. As a novice, I may not write very well. If it is wrong, please give me some pointers.

Guess you like

Origin blog.csdn.net/AAlykk/article/details/130611863