C Language Learning Tutorials Fourth-arrays (2)

Source: Internet
Author: User

The representation method of an array element

An array element is the base unit that makes up an array. An array element is also a variable that identifies a method with a subscript after the array name. The subscript represents the ordinal number of an element in an array. The general form of an array element is: An array name [subscript] where the subscript can only be an integer constant or an integer expression. If you are a decimal, C compilation is automatically rounded. For example, a[5],a[i+j],a[i++] is a legitimate array element. Array elements are often also called subscript variables. The array must be defined before the subscript variable can be used. The subscript variable can only be used individually in the C language, not the entire array at a time. For example, an array that outputs 10 elements must use a looping statement to output each subscript variable individually:
For (i=0 i<10; i++) printf ("%d", a[i]); Instead of outputting the entire array with one statement, the following is wrong: printf ("%d", a);
void Main ()
{
int i,a[10];
for (i=0;i<10;i++)
a[i++]=2*i+1;
for (i=9;i>=0;i--)
printf ("%d", a[i]);
printf ("\n%d%d\n", a[5.2],a[5.8]);
}
In this example, a loop statement is used to send the elements of a array to an odd number, and then a second loop statement is used to output the odd numbers from large to small. In the first for statement, Expression 3 is omitted. An expression i++ is used in the subscript variable to modify the loop variable. Of course, the second for statement can also do so, the C language allows expressions to represent the subscript. The last printf statement in your program outputs a value of two times a[5], and you can see that the current is automatically rounded when it is not an integer. The assignment of an array to an array is a method of initializing assignment and dynamic assignment, in addition to assigning values individually to an assignment statement. An array initialization assignment array initialization assignment refers to assigning an initial value to an array element when the array is described. Array initialization is done in the compile phase. This will reduce running time and improve efficiency.

The general form of the initialization assignment is: the static type descriptor array name [constant expression]={value, value ... Value} where static representation is a static storage type, and the C language stipulates that only static storage arrays and external storage arrays can be initialized to assign values (for static storage, the concept of external storage is described in chapter fifth). Each data value in {} is the initial value of each element, separated by a comma. For example: Static int a[10]={0,1,2,3,4,5,6,7,8,9}; equivalent to a[0]=0;a[1]=1...a[9]=9;

The initial assignment of the C-language array also has the following provisions:
1. You can assign an initial value to only some elements. When the number of values in {} is less than the number of elements, only the previous part of the element is assigned a value. For example: Static int a[10]={0,1,2,3,4}, which means that only a[0]~a[4]5 elements are assigned a value, and then 5 elements automatically assign 0 values.
2. You can only assign values to elements, and you cannot assign values to the array as a whole. For example, all 10 elements are assigned a value of 1, can only be written as: static int a[10]={1,1,1,1,1,1,1,1,1,1}, and cannot be written as: static int a[10]=1;
3. If you do not assign an initial value to an initialized array, all elements are 0 values.
4. If you assign a value to all the elements, in the array description, you can not give the number of groups of elements. For example: static int a[5]={1,2,3,4,5}; writable as: static int a[]={1,2,3,4,5}; Dynamic assignment can be dynamically assigned to an array during program execution. The scanf function is used to assign values to the array elements.
void Main ()
{
int i,max,a[10];
printf ("Input numbers:\n");
for (i=0;i<10;i++)
scanf ("%d", &a[i]);
MAX=A[0];
for (i=1;i<10;i++)
if (A[i]>max) max=a[i];
printf ("maxmum=%d\n", Max);
}
The first for statement in this example program enters 10 digits into array a. Then put the a[0] into Max. In the second for statement, from A[1] to a[9] One by one with the content in Max, if the value is larger than Max, the subscript variable is sent to Max, so Max is always the largest in the subscript variable that has been compared. The value of Max is output at the end of the comparison.
void Main ()
{
int i,j,p,q,s,a[10];
printf ("\ n input numbers:\n");
for (i=0;i<10;i++)
scanf ("%d", &a[i]);
for (i=0;i<10;i++) {
P=i;q=a[i];
for (j=i+1;j<10;j++)
if (Q<a[j]) {p=j;q=a[j];}
if (i!=p)
{S=a[i];
A[I]=A[P];
A[p]=s; }
printf ("%d", a[i]);
}
}
The example program uses two parallel for loop statements, and a loop statement is nested in the second for statement. The first for statement is used to enter the initial value of 10 elements. The second for statement is used for sorting. The sequencing of this procedure is carried out by comparison method. In the I cycle, the subscript I of the first element is assigned to P, and the subscript variable value a[i] is assigned to Q. Then go into the small cycle, from a[i+1] to the last element to be compared with the a[i], there is more than a[i] The big one to send it p, element value to send Q. At the end of a loop, p is the subscript for the largest element, and q is the element value. If i≠p at this time, it is stated that the P,Q value is not the value assigned before entering the small cycle, then the value of A[i] and A[p] is exchanged. At this point A[i] is the sorted element. Output the value to the next loop. Sorts the various elements after i+1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.