Tutorial 8.3: Two-dimensional Arrays

Introduction
Up until now, we have been dealing with arrays with one index value as reference to its elements. These arrays are called one-dimensional arrays. In C we can declare arrays of two dimensions. Two-dimension arrays is illustrated in the following figure.



The array contains three rows and four columns (3-by-4 array). Two-dimensional arrays are commonly used to represent table of values that are arranged in rows and columns. For example, we could use a two-dimensional array to store information of student's course works for C programming course. What if we have several courses? We could use a three-dimensional array to store the information. One dimension would be for student's ids, one dimension for the scores and the third for the courses.

Defining and Initializing Two-dimensional Arrays
Now lets look at how we can declare a two-dimensional array. A two-dimensional array is declared as

int coursework[20][5];

The statement declares an array coursework containing twenty rows and five columns. As one-dimensional array, the index value of two-dimensional arrays with 0. Therefore, the reference for the first row and columns is coursework[0][0].

The basic structure of initializing a two-dimensional array much like a one-dimensional array. For example

int a[3][4] = { {2, 4, 6, 8}, {1, 3, 5, 7}, {2, 5, 3, 6} };

The values for each row are grouped in braces. The first group of values in the braces initializes the row 0. The next group initializes row 1 and the last group is for row 2. If we specify not enough initializing values for a given elements in a row, that elements will be initialized to zero.

Next: Passing Arrays to Functions

3 comments:

Unknown said...

Hello, sir i would like to ask that what is the scope of c programming, what all topics should be covered and it is kinda bothering me … and has anyone studied from this course http://www.wiziq.com/course/2118-learn-how-to-program-in-c-language of c programming language online ?? or tell me any other guidance...
would really appreciate help… and Also i would like to thank for all the information you are providing on c programming.

Junaid Tutorials said...

thanks for creating a nice post on c programming but did you know what is loop in c programming

Junaid Tutorials said...

thanks for creating a nice post on c programming but did you know what is conditional statement in c programming

Post a Comment