16 - ICS103 Programming in C Lecture 16: 2-Dimensional...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Introduction to 2-D Arrays Declaration of 2-D Arrays Accessing 2-D Array elements Initialization of 2-D Arrays Processing 2-D Arrays 2-D Arrays as parameters to functions
Background image of page 2
3 Introduction to 2-D Arrays A 2-D array is a contiguous collection of variables of the same type, that may be viewed as a table consisting of rows and columns. The same reason that necessitated the use of 1-D arrays can be extended to 2-D and other multi-D Arrays. For example, to store the grades of 30 students, in 5 courses require multiple 1-D arrays. A 2-D array allows all these grades to be handled using a single variable. This idea can be easily extended to other higher dimensions. Thus, we shall focus only on 2-D arrays.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Declaration of 2-D Arrays A 2-D array variable is declared by specifying the type of elements, the name of the variable, followed by the number of rows and number of columns – each is a separate bracket: The following declares a 2-D array, table , having 3 rows and 4 columns. int table[3][4]; Both rows and columns are indexed from zero. So the three rows have indexes 0, 1 and 2 and four the columns have 0, 1, 2, 3. As we saw in 1-D array, it is a good practice to declare the sizes as constants. So a better declaration for the above is: #define ROWS 3 #define COLS 4 int table[ROWS][COLS];
Background image of page 4
5 Accessing 2-D Array elements A particular element of a 2-D array, table, is referenced by specifying its row and column indexes: table[RowIndex][columnIndex] For example, given the declaration: int table[3][4];
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/09/2010 for the course ICS 103 taught by Professor Baleh during the Spring '10 term at Abilene Christian University.

Page1 / 12

16 - ICS103 Programming in C Lecture 16: 2-Dimensional...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online