C Language Learning Tutorials fourth-arrays (3)

Source: Internet
Author: User
Tags arrays constant expression printf

Two-dimensional array

The array described earlier has only one subscript, called a one-dimensional array, and its array element is also called a single subscript variable. Many of the actual problems are two-dimensional or multidimensional, so the C language allows you to construct multidimensional arrays. A multidimensional array element has multiple subscripts to identify its position in the array, so it is also called a multiple subscript variable. This section only describes two-dimensional arrays, which can be obtained by analogy to two-dimensional arrays. Two-dimensional array type description The general form of two-dimensional array type descriptions is:
Type descriptor array name [constant expression 1][constant expression 2] ... ;
Where the constant expression 1 represents the length of the subscript in the first dimension, the constant expression 2 represents the length of the subscript in the second dimension. For example:
int a[3][4]; Describes an array of three rows and four columns with an array named A, with the type of the subscript variable being an integral type. The subscript variable for the array has a total of 3x4, namely: A[0][0],a[0][1],a[0][2],a[0][3]
A[1][0],A[1][1],A[1][2],A[1][3]
A[2][0],A[2][1],A[2][2],A[2][3]
Two-dimensional arrays are conceptually two-dimensional, meaning that their subscripts change in two directions, and the position of the subscript variable in the array is also in a plane, rather than as a one-dimensional array is just a vector. However, the actual hardware memory is continuously addressable, that is, the memory unit is arranged in one-dimensional linear order. There are two ways to store a two-dimensional array in one-dimensional memory: one is arranged in rows, that is, after a row is placed, and then placed in the second row. The other is in columns, that is, after one column is placed and then placed in the second column in sequence. In the C language, two-dimensional arrays are arranged in rows. In Figure 4.1, store by line, store A[0] line, then store a[1] line, and then store a[2] line. Four elements in each row are also stored sequentially. Because the array a describes
int type, which occupies two bytes of memory space, so each element occupies two bytes (each cell in the graph is one byte).

A representation method for two-dimensional array elements

The elements of a two-dimensional array are also called double subscript variables, which are represented by the array name [subscript] [subscript] where the subscript should be an integer constant or an integral type expression. For example: A[3][4] represents an array of three rows, four columns of elements. Subscript variables and array descriptions are somewhat similar in form, but they have a completely different meaning. The square brackets in the array description give the length of a dimension to remove the maximum value of the object, and the subscript in the array element is the position identifier of the element in the array. The former can only be a constant, and the latter may be a constant, a variable, or an expression.
There are 5 people in a study group, each with three classes of exam results. The average score of the component section and the overall average score of each section.
Course Results name Math C DBASE
Zhang 80 75 92
Wang 61 65 71
Lee 59 63 70
Zhao 85 87 90
Week 76 77 85
You can set up a two-dimensional array a[5][3] to store five of people three classes of results. Then set up a one-dimensional array v[3] store the average score of each branch, and set the variable L as the total average score of all subjects. Programming as follows:
void Main ()
{
int i,j,s=0,l,v[3],a[5][3];
printf ("Input score\n");
for (i=0;i<3;i++) {
for (j=0;j<5;j++)
{scanf ("%d", &a[j][i]);
S=s+a[j][i];}
V[I]=S/5;
s=0;
}
L= (V[0]+v[1]+v[2])/3;
printf ("Math:%d\nc languag:%d\ndbase:%d\n", v[0],v[1],v[2]);
printf ("total:%d\n", L);
}
A double loop is first used in the program. In the internal cycle, read the results of each of the students in a course, and add these scores, exit the inner loop and then divide the cumulative score by 5 into V[i], which is the average score of the course. The external circulation was three times, and the average scores of the three classes were obtained and stored in the V array. After exiting the outer loop, V[0],v[1],v[2] is added divided by 3 to get the average score of each section. Finally, according to the test output of various grades.

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.