16 - ICS103 Programming in C Lecture 16 2-Dimensional Arrays 1 Outline Introduction to 2-D Arrays Declaration of 2-D Arrays Accessing 2-D Array elements

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

This preview shows page 1 - 6 out of 12 pages.

1 ICS103 Programming in C Lecture 16: 2-Dimensional Arrays
Image of page 1
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
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.
Image of page 3
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];
Image of page 4
5 Accessing 2-D Array elements
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 12 pages?

  • Spring '10
  • baleh
  • Array, #include, #define, The Elements, actual size, 2-d array

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes