Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Material Arrays Definition Declaration Initialization

Similar presentations


Presentation on theme: "Today’s Material Arrays Definition Declaration Initialization"— Presentation transcript:

1 Today’s Material Arrays Definition Declaration Initialization
Manipulation Multi-dimensional arrays

2 Multiple Variables of Same Type
Sometimes, we need to use a number of variables of the same type, for the same purpose. For example, we may hold each student's grade (an integer value in range ) in variables grade1, grade2, grade3, ... This may require many int variables. Program would have to handle these variables one by one. There's a better way: Use "arrays"

3 Arrays Data structures that hold multiple variables of the same data type The simplest kind of array has just one dimension Elements of a one-dimensional array are conceptually arranged one after another in a single row Declaration: #define N 10 int A[N]; A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] First index = 0 Last index = N-1 = 9

4 Array Initialization You can initialize the elements of an array during declaration int A[10]={8, 4, 10, 2, 5, 6, 7, 8, 9, 4}; If the initializer is shorter than the array, remaining elements of the array are filled with 0 int A[10]={1, 2, 3, 4}; /* Initial value of A[10] is {1, 2, 3, 4, 0, 0, 0, 0, 0, 0} */ If an initializer is present, the length of the array may be omitted int A[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /* A has 10 elements A[0]..A[9] */

5 Array Manipulation Accessing elements of an array is called indexing or subscripting An index or subscript is the element’s position in the array The elements of an array are allocated contiguously (no gaps) Each individual element of an array is numbered consecutively, starting from 0

6 Array Manipulation Example: #define MAX_NUM_STUDENTS 5 …
int grades[MAX_NUM_STUDENTS]; grades[0] = 98; grades[1] = 87; grades[2] = 92; grades[3] = 79; grades[4] = 85;

7   Array Manipulation Warning!
C does not perform range-checking of indices (i.e., index values are not explicitly tested for validity with respect to the array size before applying the [ ] operator #define MAX_NUM_STUDENTS 5 int grades[MAX_NUM_STUDENTS]; grades[53] = 98; grades[5] = 98;

8 Array Manipulation It is common to process the elements of an array in a loop, with one element processed per loop iteration The most convenient iteration statement for this is the for statement, since the loop header clearly encapsulates both the initialization and the use of an index variable: grades[0] = 0; grades[1] = 0; grades[2] = 0; grades[3] = 0; grades[4] = 0; int i; for(i = 0; i < MAX_NUM_STUDENTS; i++) grades[i] = 0;

9 Array Example, 1/3: Read #include<stdio.h> #define SIZE 5 int main(void){ int i; double a[SIZE]; printf(“Enter the %d array elements: ", SIZE); /* Read the array elements */ for(i = 0; i < SIZE; i++) scanf("%lf", &a[i]); return 0; } Enter the 5 array elements:

10 Array Example, 2/3: Print a[0] = 1.20 a[1] = 3.40 a[2] = 5.60
#include<stdio.h> #define SIZE 5 int main(void) { int i; double a[SIZE] = { 1.2, 3.4, 5.6, 7.8, 9.0 }; /* Display array elements */ for(i = 0; i < SIZE; i++) printf("a[%d] = %.2lf\n", i, a[i]); return 0; } a[0] = 1.20 a[1] = 3.40 a[2] = 5.60 a[3] = 7.80 a[4] = 9.00

11 Array Example, 3/3: Max max = 9.00 #include<stdio.h>
#define SIZE 5 int main(void) { int i; double a[SIZE] = { 1.2, 3.4, 5.6, 7.8, 9.0 }; double max = 0.0; /* Find max value in array */ for(i = 0; i < SIZE; i++) if (a[i] > max) max = a[i]; printf("max = %.2lf\n", max); return 0; } max = 9.00

12 Array Example, Combined
#include<stdio.h> #define SIZE 5 int main(void) { int i; double a[SIZE]; double max = 0.0; printf("Enter the %d array elements\n", SIZE); for(i = 0; i < SIZE; i++) /* Read the array elements */ scanf("%lf", &a[i]); for(i = 0; i < SIZE; i++) /* Display array elements */ printf("a[%d] = %.2lf\n", i, a[i]); for(i = 0; i < SIZE; i++) /* Find max value in array */ if (a[i] > max) max = a[i]; printf("max = %.2lf\n", max); return 0; }

13 Arrays as Function Parameters(1)
A function can take an array as a parameter. We specify this as follows: int Sum(int A[], int n){ int i; int sum = 0; for (i=0; i<n; i++) sum += A[i]; return sum; } int main(){ int B[5]={3, 5, 1, 2, 3}; printf(“Total: %d\n”, Sum(B, 5)); return 0;

14 Arrays as Function Parameters(2)
Changing the elements of an array parameter changes the values of the array argument int Junk(int A[]){ A[0] = 8; A[1] = 9; A[2] = 10; } /* end-Junk */ int main(){ int B[4]={3, 5, 1, 2}; Junk(B); printf(“B[0]:%d, B[1]: %d, B[2]: %d, B[3]: %d\n”, B[0], B[1], B[2], B[3]); return 0; } /* end-main */

15 Multi-Dimensional Arrays
An array may have any number of dimensions E.g., 2-dimensional array (a matrix) is declared as follows int M[5][9]; /* Has 5 rows and 9 columns */ Conceptually, array M looks like this: 1 2 3 4 5 6 7 8 To access an element in row i, column j, we write M[i][j] M[i] selects ith row, M[i][j] selects jth column M[i,j] is pascal style. In C, M[i,j] is the same as M[j] Recall that comma(,) is an operator in C and evaluates to the last expression (j) in this example

16 Accessing 2-dimensional Arrays
/* Initialization */ for (i=0; i<5; i++){ for (j=0; j<9; j++){ M[i][j] = 0; } /* Summation */ sum = 0; for (i=0; i<5; i++){ for (j=0; j<9; j++){ sum += M[i][j]; } /* Find min & max */ min = M[0][0]; Max = M[0][0]; for (i=0; i<5; i++){ for (j=0; j<9; j++){ if (M[i][j]<min) min=M[i][j]; if (M[i][j]>max) max=M[i][j]; } printf(“min: %d, max: %d\n”, min, max);

17 Initializing Multi-Dimensional Arrays
You can create an initializer for a two-dimensional array by nesting one-dimensional initializers int M[5][9] = { {1, 1, 1, 1, 0, 1, 1, 1, 1}, {0, 1, 0, 1, 0, 1, 0, 1, 0}, {1, 0, 0, 1, 1, 1, 0, 0, 1}, {0, 0, 0, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 0, 0, 0, 1, 1}}; If the initializer is not long enough to fill a multi-dimensional array, the remaining elements are given the value 0 int M[5][9] = { {1, 1, 2, 1, 2, 0, 0, 1, 1}, {0, 0, 0, 1, 1, 1, 1, 2, 1}}; /* Rows 2, 3 and 4 will be set to 0 */

18 Initializing Multi-Dimensional Arrays
If an inner list is not long enough to fill a row, the remaining elements in the row are set to 0 int M[5][9] = { {1, 1, 0, 0, 1, 1, 1, 1, 1}, {0, 1, 1, 2, 1, 1}, {1, 1, 2, 2, 3}}; /* M[1][6], M[1][7], M[1][8] are set to 0 */ /* M[2][5], M[2][6], M[2][7], M[2][8] are set to 0 */ /* All elements of row 3 and 4 are set to 0 */

19 Higher-Dimensional Arrays
An array can have any number of dimensions int Cube[8][8][8]; /* A cube of size 8 */ int Prism[4][6][10]; /* A rectangular prism of size * 4x6x10 */ /* Can have initializer */ float A[4][6][8] = {{{1, 2, 3}, {3, 4}}, {{3, 4}}}; Cube[2][3][4] = 2; Prism[3][5][8] = 6; A[0][0][4] = 3.34;

20 Multi-Dimensional Arrays as Function Parameters
We can also have multi-dimensional arrays as function parameters /* Must specify the 2nd dimension size, i.e., 4 */ int Sum2(int A[][4], int n){ int i, j, sum = 0; for (i=0; i<n; i++) for (j=0; j<4; j++) sum += A[i][j]; return sum; } /* end-Sum2 */ This is very inconvenient though. All arguments to Sum2 must have 2nd dimension equal to 4!!


Download ppt "Today’s Material Arrays Definition Declaration Initialization"

Similar presentations


Ads by Google