notes5

notes5 - Arrays New topic ARRAYS Reading for this week...

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

View Full Document Right Arrow Icon
New topic: ARRAYS Reading for this week: CHAPTER 6
Background image of page 1

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

View Full Document Right Arrow Icon
Arrays Structures of related data items Array Group of consecutive memory locations Same name and type When defining arrays, specify –N a m e Type of array Number of elements –E x a m p l e s : int b[ 3 ]; float myArray[ 27 ]; Defining multiple arrays of same type int c[ 5 ], x[ 12 ]; a[0] -12 a[1] 23 a[2] 4 a[3] 15 a[4] -1 a[5] 53 a[6] 0 a[7] -7
Background image of page 2
Array elements Consider the array defined by: int c[ n ]; The elements of c are: c[ 0 ], c[ 1 ],…, c[ n-2 ], c[ n-1 ] First element is c[0] , 2 nd element is c[1] , nth element is c[ n-1 ] Defining and referring to array elements Specify array name and position number Array elements can be treated like normal variables –E x a m p l e : c[ 0 ] = 3; c[ 1 ] = 5; printf( “%d %d”, c[ 0 ], c[ 1 ] );
Background image of page 3

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

View Full Document Right Arrow Icon
Defining array elements Initializers int n[ 5 ] = { 1, 0, -2, 5, 7 }; If not enough elements, rightmost elements become 0 int m[ 5 ] = { 1, 2 }; is the same as int m[ 5 ] = { 1, 2, 0, 0, 0 }; int p[ 3 ] = { 0 }; all elements 0 If too many elements are defined, a syntax error is produced If size is omitted, initializers determine it int r[ ] = { 1, 0, 3 }; 3 initializers, therefore 3 elements
Background image of page 4
/* initializing an array */ #include <stdio.h> int main() { int n[ 10 ]; /* n is an array of 10 integers */ int m[ 10 ]; /* m is an array of 10 integers */ int i; /* counter */ /* initialize elements of the array */ for( i = 0 ; i <= 9 ; i++ ) { n[ i ] = 2*i; m[ i ] = 3*i + 1; } /* end for */
Background image of page 5

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

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

{[ snackBarMessage ]}

Page1 / 21

notes5 - Arrays New topic ARRAYS Reading for this week...

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