week6

week6 - CS Discussion Week 6 Overview 2 dimensional array C...

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

View Full Document Right Arrow Icon
CS Discussion Week 6
Background image of page 1

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

View Full DocumentRight Arrow Icon
Overview 2 dimensional array C string I/O Midterm Problems Project 4
Background image of page 2
2 Dimensional Array 1. Declaring a 2D array 2. Initializing a 2D array 3. 2D arrays as parameters 4. Accesing array elements
Background image of page 3

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

View Full DocumentRight Arrow Icon
Declaring 2D Arrays Declaring an array by specifying columns and rows int scores[4][5]; // declares an int array of 4 rows and 5 columns. char a[3[3]; // three rows and three columns of chars . Use constant integer to declare an array const int MAX_STUDENTS=4; const int MAX_CLASSES=5; int scores [MAX_STUDENTS][MAX_CLASSES];
Background image of page 4
Declaring 2D Arrays Use #define #define MAX_STUDENTS 4 #define MAX_CLASSES] 5 int scores[MAX_STUDENTS][MAX_CLASSES]; In case that we decided to enlarge the array to a array of 40 rows and 50 columns, it could be done simply by changing the line #define
Background image of page 5

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

View Full DocumentRight Arrow Icon
Declaring 2D Arrays Example of a 2D array int scores[4][5]; 1 2 3 4 5 1 scores[0][0] scores[0][1] scores[0][2] scores[0][3] scores[0][4] 2 scores[1][0] scores[1][1] scores[1][2] scores[1][3] scores[1][4] 3 4 scores[0] scores[1] scores[2] scores[3]
Background image of page 6
Initializing 2D Arrays Initializing an array Unless specified, all initial values of arrays are garbage . Initializing using only a list of values int scores[4][5]={90,100, 75, 88, 65,83, 99, 85, 100, …} Specify initial values by enclosing each row in curly braces like this: int scores[4][5]={{90,100, 75, 88, 65}, {83, 99, 85, 100},
Background image of page 7

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

View Full DocumentRight Arrow Icon
Initializing 2D Arrays Initializing an array If some elements are omitted in the initialization list, they are set to zero. int array[2][3] = { 1, 2, 3, 4, 5 }; int array[2][3] = { { 1, 2 }, { 4 } }; // what about this? 1 2 3 4 5 0 array[0] array[0]
Background image of page 8
2D Arrays as Parameters As with 1D arrays, a 2D array is automatically passed as a pass-by-reference parameter void ReadScores (int scores[][MAX_CLASSES], //OUT: student labs int& numStudents,
Background image of page 9

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

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

This note was uploaded on 11/10/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 35

week6 - CS Discussion Week 6 Overview 2 dimensional array C...

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

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