C language data processing organization and - an array (defined, input, output)

Note: For more introductory text of the array, you want to have, but to be patient to find! Early success! !

First, the idea and significance of the array:
Mid-Autumn Festival, there are guests came to the prairie, the owner of a sheep from the flock select dinner guests, of course, to choose the most fat person, so we must record the weight of each sheep. If there are thousands of sheep, it is impossible to record the general variable, use an indexed variable. The code below, you may also perform a bit, look at the effect

/* 找出最重的羊 */
#include "stdio.h"
void main()
{
	float sheep[10]; //数组,有10个浮点类型元素,用于存放10只着每只的重量
	float bigsheep=0.0;               //浮点型变量,存放最肥羊的重量
	int bigsheepno=0,i;           //整型变量记录最肥羊的编号,i计数循环
    //循环输入10只羊的数量
	for(i=0;i<10;i++)
	{
		printf("请输入羊的重量:");
		scanf("%f",&sheep[i]);		
	}
	//假设第0只是最肥的
	bigsheep=sheep[0];
	//依次比较一遍
	for(i=0;i<10;i++)
	{
		if(bigsheep<sheep[i])        //如果第i只羊比当前羊肥大
		{
			bigsheep=sheep[i];       //第i只羊为当前最肥羊
			bigsheepno=i;            //记录第i只着的编号
		}		
	}
	//输出最肥羊的重量
	printf("最肥羊的重量是:%f\n",bigsheep);
	//输出最肥羊的编号
	printf("最肥羊的编号为:%d\n",bigsheepno);    
}

An array of ideas that we can use when we need more variables can be defined once. Be used in turn to use the subject of the next method. Save code and easy troubleshooting.
Second, what is the array
array is an ordered collection of the same data type. Divided into one-dimensional array, two-dimensional arrays and multidimensional arrays.
1. one-dimensional array is an array subscript defined by a
2-dimensional array is an array with two index definitions
3. We have three or more of the array subscript and superscript three hereinafter referred to as multidimensional arrays.
We use a life examples to understand what is an array: an example cited when we borrow explain the variable "Memory Hotel" open house understanding of relevant attributes of variables, today, we continue to borrow this example, but today we are a tour 10 passengers together hotel stay. Similarly to the "first check" However, today, when the registration room we added two special requirements: 1, a room must be the same type of room; 2, which is next door room door must be "even number room ", more than two points is to" guide "is more convenient for team management. That's why we use an array of two attributes are most concerned about: the "same type" "orderly." Again, this is a room we also have a name, people stay there, then you can immediately check in, you can then check and so on, these cases correspond to our array associated syntax.
Third, an array of
arrays for use in real life, and the software development process is very flexible, as the use and application of two-dimensional arrays, multidimensional arrays with a one-dimensional array, here we mainly on one-dimensional array.
1. The definition of a one-dimensional array
(1) in the form of one-dimensional array defined by the beginning:
type specifier array name [Constant Expressions];
e.g.
a float Sheep [100];
int A2001 [1000];
(2) for one-dimensional defines an array described below:
① array names should follow the naming rules for identifiers, the first character in the formal application should be in English
② constant expression in square brackets enclose
③ constant expression defines the number of array elements
④ array index starts at 0. If the definition of five elements, from 0th element to the fourth element.
For example: int a [5]; defines five array elements a [0], a [1 ], a [2], a [3], a [4] which is 5 subscripted variables, which. type 5 is the same as the variable, the table below
a
subscript 01,234
constant expressions are not allowed in the variable. For example:
int = n-10, A [n-];
initialize array 2.
(1) can be initialized directly declaration, the table below. For example:
int A [. 5] = {3,5,4,1,2};
Note that statements separated by commas between the above elements and the elements above, the end of the sentence with a semicolon.
a 3 5 4 1 2
the subscript 01 234
(2), please make the following six machine experiment to see what programs are problematic

①初始程序
/* note:your choice is c ide */
#include "stdio.h"
void main()
{
    int a[5];
    printf("%d\n",a[0]);
    printf("%d\n",a[1]);
    printf("%d\n",a[2]);
    printf("%d\n",a[3]);
    printf("%d\n",a[4]);
}

② other but changing the declared item is
int A [. 5] = {2,3,5,8};
③ other but changing the declared item is
int A [. 5] = {2,3,5};
④ Other but changing the declared item is
int A [. 5] = {2,3,5,6,8,9};
⑤ other but changing the declared item is
int a [5] = {2,3,5 , d };
⑥ other but changing the declared item is
int. 6 = D;
int A [. 5] = {2,3,5,} D;
⑦ other but changing the declared item is
int. 5 n-=;
int A [n- ] = {2,3,5};
initialization process can you summarize what to do?
(3) forming the initialization of array elements can be implemented by the following methods
① array definition assigned to the initial value of the array elements. For example:
int A [. 5] = {5,4,5,2,1}; the value in braces in a sequence separated by a comma, corresponding to a [0] = 5, a [1] = 4, a [2] =. 5, A [. 3] = 2, A [. 4] =. 1;
② only to part of the element can be assigned: Example start
int a [10] = {1,2,3,4 }; only to the statement the initial value of the first 4 elements, 0 to 6 default back padded
③ to an array of all the elements of the value 0; can be written
int a [10] = {0 }; or int a [10] = {0 , 0,0,0,0,0,0,0,0,0};
④ when the array may not specify the length of the initial value, a default length is the number of elements behind: Example
int a [] = {1, 2,2,3,4}; 4 array of default length.
Fourth, the combined application of the array for loop
arrays we can see enumeration method is one of a tissue used; enumeration statement cyclic structure can be used for very easy to implement, let's look at them with an array how to use for input and output statements to complete and summing operations.
1. The input and output array element
analysis method of enumeration as follows array input
subscripts statement
input a [0] to 0 after the prompt
enter a [1] After a first prompt
after prompt for 2 to A [2]
... ...
when prompted to enter i A [i]
... ...
input A [9] prompted the 9 first
reference code as follows:

/*利用for循环给数组输入数值 */
#include "stdio.h"
void main()
{
	int i,a[10];
    for(i=0;i<10;i++)
    {
    	printf("请输入数组里的数值:");
    	scanf("%d",&a[i]);    	
    }    
}

Renderings follows
Here Insert Picture Descriptionthat if I want to phase the following renderings, how should we change the above code?
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/SqrsCbrOnly1/article/details/91375331