week6

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

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

CS Discussion Week 6

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

View Full Document
Overview 2 dimensional array C string I/O Midterm Problems Project 4
2 Dimensional Array 1. Declaring a 2D array 2. Initializing a 2D array 3. 2D arrays as parameters 4. Accesing array elements

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

View Full Document
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];
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

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

View Full Document
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]
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},

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

View Full Document
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]
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,

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

View Full Document
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
Ask a homework question - tutors are online