C语言数组的简单以及一维数组的简单排序算法示例,二维数组简单示例

# include <stdio.h>

int main(void)
{
	
	int a[5] = {1, 2, 3, 4, 5};
		//a 是数组的名字 5是表示数组元素的个数,并且这五个元素分别用a[0], a[1]...a[4]
	
	int i;

	for (i=0; i<5; ++i)
		printf("%d\n", a[i]);

	return 0;
}

 

 

一维数组应用,排序代码:

# include <stdio.h>

int main(void)
{
	int a[7] = {1, 2, 3, 4, 5, 6, 7};
	int i, j;
	int t;

	i = 0;
	j = 6;
	while (i < j)
	{
		t = a[i];
		a[i] = a[j];
		a[j] = t;

		i++;
		--j;
	}

	for (i=0; i<7; i++)
	{
		printf("%d ", a[i]);
	}

	return 0;
}

 

 二维数组示例:

# include <stdio.h>

int main(void)
{
	int a[3][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};	//这样每4个当成一个组
/*	int a[3][4] = {
					{1, 2, 3, 4},
					{5, 6, 7, 8},
					{9, 10, 11, 12}
	};		//第二种赋值的方式
	*/
	int i, j;


	//输出数组内容
	for (i=0; i<3; i++)
	{
		for (j=0; j<4; j++)
			printf("-5%d ", a[i][j]);

		printf("\n");
	}
	
	return 0;
}

 

你可能感兴趣的:(c,array)