Two-Dimentional Arrays in c
Posted by
Ravi Kumar at Sunday, September 18, 2011
Share this post:
|
So far we have discussed the array variable that can
store a list of values .There will be situation where a table
of values will have to be stored.Consider the following data
table, which shows the value of the sales of three items by
four sales mens:
item 1 item 2 item 3
salesmen 1 310 275 365
salesmen 2 210 190 325
salesmen 3 405 325 240
salesmen 4 260 300 290
the table contains a total of 12 values, three in each
line.We think of this table as a matrix consisting of four
rows and tree columns. Each row represents the values of
sales by a particular salesmen and each column represents the
values of sales of a particular item.In mathematics, we
represent a particular value in a matrix by using two sub
scripts such as Vij. Here V denotes the entire matrix and Vij
refers to the value in the ith row and jth column. For example,
in the above table V23 refers to the value 325.
C allows us to define such tables of items by using
two-dimensional arrays. The table discussed above can be
defined in c as v[2][3].
Two-Dimensional arrays are declared as follows:
type array-name [row-size][column-size];
note that unlike most other languages which we use one
pair of parenthesis with commas to separate array sizes.C
places each size in the own set of brackets.Two dimensional
arrays are stored in memory as follows:
[0][0] [0][1] [0][2]
310 275 365
[1][0] [1][1] [1][2]
210 190 325
[2][0] [2][1] [2][2]
405 325 240
[3][0] [3][1] [3][2]
260 300 290